⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chap3_6.htm

📁 着重介绍基于C++的web编程技术
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<html>

<head>
<title>3.6 使用菜单</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
</head>

<body link="#3973DE" alink="#3973DE">
<font SIZE="5"><b><div align="center"><center>

<table border="0" width="615" cellspacing="0" cellpadding="0">
  <tr>
    </b><td><div align="center"><center><table border="0" width="615" cellpadding="0" cellspacing="0" height="20">
      <tr>
        <td width="377" bgcolor="#15397D" height="20"></td>
        <td width="238" bgcolor="#000000" height="20"><p align="right"></font><span style="text-decoration: none"><a href="../../index.htm"><font color="#FFFFFF">电脑报Visual 
        C++网络教程</font></a></span></td>
      </tr>
    </table>
    </center></div><p align="center"><font color="#3973DE" FACE="Times New Roman" size="4">3.6 
    </font><font color="#3973DE" size="4">使用菜单</font></p>
    <font SIZE="3"><font FACE="Times New Roman"><p></font>现在我们要在主窗口中加入自己的菜单。菜单编程一般分三步:</font><font FACE="Times New Roman" SIZE="3"></p>
    <p>1.</font><font SIZE="3">编辑菜单资源,设置菜单属性(包括菜单名和</font><font FACE="Times New Roman" SIZE="3">ID</font><font SIZE="3">);</font><font FACE="Times New Roman" SIZE="3"></p>
    <p>2.</font><font SIZE="3">用</font><font FACE="Times New Roman" SIZE="3">ClassWizard</font><font SIZE="3">自动映射菜单消息和成员函数;</font><font FACE="Times New Roman" SIZE="3"></p>
    <p>3.</font><font SIZE="3">手工编辑成员函数,加入菜单消息处理代码。</font><font FACE="Times New Roman" SIZE="3"><b></p>
    <p></b></font><font color="#3973DE" FACE="Times New Roman" SIZE="3">3.6.1 </font><font SIZE="3" color="#3973DE">编辑菜单资源</font><font FACE="Times New Roman" SIZE="3"></p>
    <p></font><font SIZE="3">仍然使用我们前面生成的</font><font FACE="Times New Roman" SIZE="3">Hello</font><font SIZE="3">程序,编辑由</font><font FACE="Times New Roman" SIZE="3">AppWizard</font><font SIZE="3">自动生成的菜单资源。要编辑菜单资源:</font><font FACE="Times New Roman" SIZE="3"></p>
    <p>(1) </font><font SIZE="3">选择项目工作区的</font><font FACE="Times New Roman" SIZE="3">ResourceView</font><font SIZE="3">标签,切换到资源视图。</font><font FACE="Times New Roman" SIZE="3"></p>
    <p>(2) </font><font SIZE="3">选择菜单资源类型。</font><font FACE="Times New Roman" SIZE="3"></p>
    <p>(3) </font><font SIZE="3">选定菜单资源</font><font FACE="Times New Roman" SIZE="3">IDR_MAINFRAME</font><font SIZE="3">,双击该项或单击鼠标右键然后在弹出菜单中选择</font><font FACE="Times New Roman" SIZE="3">Open</font><font SIZE="3">选项。</font><font FACE="Times New Roman" SIZE="3">Visual Studio</font><font SIZE="3">将弹出菜单编辑窗口,显示菜单资源</font><font FACE="Times New Roman" SIZE="3">IDR_MAINFRAME</font><font SIZE="3">,其中</font><font FACE="Times New Roman" SIZE="3">IDR_MAINFRAME</font><font SIZE="3">是由</font><font FACE="Times New Roman" SIZE="3">AppWizard</font><font SIZE="3">在创建该程序时自动生成的。</font><font FACE="Times New Roman" SIZE="3"></p>
    <p>(4) </font><font SIZE="3">编辑当前菜单</font><font FACE="Times New Roman" SIZE="3"></p>
    <p></font><font SIZE="3">要删除某个菜单项或弹出菜单,可用鼠标单击该菜单或用上下光标键来回选择,然后按</font><font FACE="Times New Roman" SIZE="3">Del</font><font SIZE="3">键删除;要插入新菜单项,可选定窗口中的空白菜单框后按回车(或直接用鼠标双击该空白框),</font><font FACE="Times New Roman" SIZE="3">Visual Studio</font><font SIZE="3">弹出</font><font FACE="Times New Roman" SIZE="3">Properties</font><font SIZE="3">(属性)对话框,如图</font><font FACE="Times New Roman" SIZE="3">3-11</font><font SIZE="3">所示。属性对话框用于输入菜单项的标题、标识符、菜单项在状态栏上显示的提示(</font><font FACE="Times New Roman" SIZE="3">Prompt</font><font SIZE="3">),并为该菜单提供属性调整。也可以在选择一个已有的菜单项时按</font><font FACE="Times New Roman" SIZE="3">Ins</font><font SIZE="3">键,以在该菜单项上方插入一个空白菜单项,然后双击该菜单项进行编辑。要插入一个分隔线,只需将菜单项的</font><font FACE="Times New Roman" SIZE="3">Seperator</font><font SIZE="3">属性打开即可。</font><font FACE="Times New Roman" SIZE="3"></p>
    <p>Visual Studio</font><font SIZE="3">支持鼠标拖曳调整菜单项位置。要调整菜单项位置,只需要选中某菜单项并将其拖至适当位置即可。</font><font FACE="Times New Roman" SIZE="3"></p>
    <p align="center"></font><img src="T3_11.gif" alt="T3_11.tif (250480 bytes)" WIDTH="548" HEIGHT="308"><font FACE="Times New Roman" SIZE="3"></p>
    <p align="center"></font><font size="2">图</font><small><font FACE="Times New Roman" SIZE="3">3.11 </font></small><font size="2">菜单编辑器和属性对话框</font><font FACE="Times New Roman" SIZE="3"></p>
    <p></font><font SIZE="3">如上图,我们首先在</font><font FACE="Times New Roman" SIZE="3">Edit</font><font SIZE="3">和</font><font FACE="Times New Roman" SIZE="3">View</font><font SIZE="3">之间加入一个弹出菜单:用鼠标单击</font><font FACE="Times New Roman" SIZE="3">View</font><font SIZE="3">菜单,按</font><font FACE="Times New Roman" SIZE="3">Ins</font><font SIZE="3">键插入一个空白菜单项,双击该空白框弹出其属性对话框。在菜单属性栏输入“</font><font FACE="Times New Roman" SIZE="3">&amp;Test</font><font SIZE="3">”字样。然后在</font><font FACE="Times New Roman" SIZE="3">Test</font><font SIZE="3">弹出菜单下加入以下四个菜单项,并在</font><font FACE="Times New Roman" SIZE="3">&amp;Say Hello</font><font SIZE="3">菜单下加入一个分隔符。菜单项属性设置如下表:</font></p>
    <div align="center"><center><table BORDER="1" CELLSPACING="2" BORDERCOLOR="#7F7F7F" CELLPADDING="1" WIDTH="346" height="177">
      <tr>
        <td WIDTH="22%" height="35"><font SIZE="3">菜单名</font></td>
        <td WIDTH="43%" height="35"><font SIZE="3">菜单</font><font FACE="Times New Roman" SIZE="3">ID</font></td>
        <td WIDTH="35%" height="35"><font SIZE="3">菜单提示</font><font FACE="Times New Roman" SIZE="3">(Prompt)</font></td>
      </tr>
      <tr>
        <td WIDTH="22%" height="38"><font FACE="Times New Roman" SIZE="3">Say &amp;Hello</font></td>
        <td WIDTH="43%" height="38"><p align="left"><font FACE="Times New Roman" SIZE="3">ID_SAY_HELLO</font></td>
        <td WIDTH="35%" height="38"><font FACE="Times New Roman" SIZE="3">Say hello to you!</font></td>
      </tr>
      <tr>
        <td WIDTH="22%" height="17"><font FACE="Times New Roman" SIZE="3">&amp;Red</font></td>
        <td WIDTH="43%" height="17"><font FACE="Times New Roman" SIZE="3">ID_SELECT_RED</font></td>
        <td WIDTH="35%" height="17"><font FACE="Times New Roman" SIZE="3">The color is red.</font></td>
      </tr>
      <tr>
        <td WIDTH="22%" height="19"><font FACE="Times New Roman" SIZE="3">&amp;Blue</font></td>
        <td WIDTH="43%" height="19"><font FACE="Times New Roman" SIZE="3">ID_SELECT_BLUE</font></td>
        <td WIDTH="35%" height="19"><font FACE="Times New Roman" SIZE="3">The color is blue.</font></td>
      </tr>
      <tr>
        <td WIDTH="22%" height="38"><font FACE="Times New Roman" SIZE="3">&amp;Yellow</font></td>
        <td WIDTH="43%" height="38"><font FACE="Times New Roman" SIZE="3">ID_SELECT_YELLOW</font></td>
        <td WIDTH="35%" height="38"><font FACE="Times New Roman" SIZE="3">The color is yellow.</font></td>
      </tr>
    </table>
    </center></div><p> </p>
    <font SIZE="3"><b><div align="center"><center><table border="2" cellpadding="2" cellspacing="0" width="90%" bgcolor="#D0D0D0">
      <tr>
        <td width="100%"></b>提示:如果菜单中要使用中文,则除了在菜单名一项中输入中文外,还要将菜单资源的语言属性设置为中文。方法是:鼠标右键单击资源视图的菜单资源</font><font FACE="Times New Roman" SIZE="3">IDR_MAINFRAME</font><font SIZE="3">,弹出快捷菜单,选择</font><font FACE="Times New Roman" SIZE="3">Properties</font><font SIZE="3">,弹出整个菜单资源的属性对话框,如图</font><font FACE="Times New Roman" SIZE="3">3-12</font><font SIZE="3">所示。在</font><font FACE="Times New Roman" SIZE="3">Languages</font><font SIZE="3">下拉列表框中选择</font><font FACE="Times New Roman" SIZE="3">Chinese (P.R.C.)</font><font SIZE="3">。这样以后菜单就可以正确使用和显示中文了。如果其他资源如对话框或字符串要使用中文,也要将该资源的语言属性改为</font><font FACE="Times New Roman" SIZE="3">Chinese(P.R.C)</font><font SIZE="3">。</font></td>
      </tr>
    </table>
    </center></div><p ALIGN="JUSTIFY"><font SIZE="3"> </font><font FACE="Times New Roman" SIZE="3"></p>
    <p align="center"></font><img src="T3_12.gif" alt="T3_12.tif (107259 bytes)" WIDTH="526" HEIGHT="186"><font FACE="Times New Roman" SIZE="3"></p>
    <p align="center"></font><font size="2">图</font><small><font FACE="Times New Roman" SIZE="3">3.12 </font></small><font size="2">设置菜单语言属性</font><font FACE="Times New Roman" SIZE="3"></p>
    <p></font><font SIZE="3">现在关闭菜单编辑器窗口。我们要为</font><font FACE="Times New Roman" SIZE="3">Say Hello</font><font SIZE="3">菜单增加一个加速键</font><font FACE="Times New Roman" SIZE="3">CTRL+H</font><font SIZE="3">。要编辑加速键,选择</font><font FACE="Times New Roman" SIZE="3">Accelerator</font><font SIZE="3">资源类型,双击打开</font><font FACE="Times New Roman" SIZE="3">IDR_MAINFRAME</font><font SIZE="3">加速键资源。要删除加速键,可以直接按</font><font FACE="Times New Roman" SIZE="3">Del</font><font SIZE="3">键。要增加加速键,可以按</font><font FACE="Times New Roman" SIZE="3">Ins</font><font SIZE="3">键,弹出加速键属性对话框。在</font><font FACE="Times New Roman" SIZE="3">ID</font><font SIZE="3">下拉列表框中选择</font><font FACE="Times New Roman" SIZE="3">ID_SAY_HELLO</font><font SIZE="3">,在</font><font FACE="Times New Roman" SIZE="3">Key</font><font SIZE="3">一栏中输入</font><font FACE="Times New Roman" SIZE="3">H</font><font SIZE="3">,完成加速键设置。关闭加速键编辑窗口。</font><font FACE="Times New Roman" SIZE="3"><b></p>
    <p></b></font><font color="#3973DE" FACE="Times New Roman" SIZE="3">3.6.2 </font><font color="#3973DE"><font SIZE="3">用</font><font FACE="Times New Roman" SIZE="3">ClassWizard</font><font SIZE="3">自动映射菜单消息和成员函数</font></font><font FACE="Times New Roman" SIZE="3"></p>
    <p></font><font SIZE="3">现在我们用</font><font FACE="Times New Roman" SIZE="3">ClassWizard</font><font SIZE="3">为上面创建的几个菜单生成和映射消息处理成员函数。在此之前我们首先介绍一下</font><font FACE="Times New Roman" SIZE="3">ClassWizard</font><font SIZE="3">的用法。<b></p>
    <p></b></font><font color="#3973DE" SIZE="3">用</font><font color="#3973DE"><font FACE="Times New Roman" SIZE="3">ClassWizard</font><font SIZE="3">管理类和</font><font FACE="Times New Roman" SIZE="3">Windows</font><font SIZE="3">消息</font></font><font FACE="Times New Roman" SIZE="3"></p>
    <p>ClassWizard</font><font SIZE="3">有助于创建</font><font FACE="Times New Roman" SIZE="3">Windows</font><font SIZE="3">消息和命令处理函数、创建和管理类、创建类成员变量、创建</font><font FACE="Times New Roman" SIZE="3">OLE Automation</font><font SIZE="3">的方法和属性、创建数据库类以及其他一些工作。</font><font FACE="Times New Roman" SIZE="3"></p>
    <p>ClassWizard</font><font SIZE="3">也有助于覆盖</font><font FACE="Times New Roman" SIZE="3">MFC</font><font SIZE="3">类中的虚函数。先选类,再选择需要覆盖的虚函数。该过程的其余部分与消息处理是类似的。<b></p>
    <p></b></font><font color="#3973DE" SIZE="3">启动</font><font color="#3973DE"><font FACE="Times New Roman" SIZE="3">ClassWizard</font><font SIZE="3">应用程序</font></font><font FACE="Times New Roman" SIZE="3"></p>
    <p ALIGN="JUSTIFY"></font><font SIZE="3">从</font><font FACE="Times New Roman" SIZE="3">View</font><font SIZE="3">菜单或源程序编辑窗口右键菜单中选择</font><font FACE="Times New Roman" SIZE="3">ClassWizard</font><font SIZE="3">(快捷键:</font><font FACE="Times New Roman" SIZE="3">Ctrl +W</font><font SIZE="3">),</font><font FACE="Times New Roman" SIZE="3">Developer Studio</font><font SIZE="3">将弹出</font><font FACE="Times New Roman" SIZE="3">MFC ClassWizard</font><font SIZE="3">对话框。该对话框包含几个标签页,提供以下选项:</font><font FACE="Times New Roman" SIZE="3"></p>
    <blockquote>
      <p>Message Maps</font><font SIZE="3">:它管理消息和成员函数之间的映射关系。</p>
      </font><font FACE="Times New Roman" SIZE="3"><p>Member Variables</font><font SIZE="3">:它可以让用户加进一些数据成员,以便和各种控制进行数据交换。</p>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -