📄 c++ builder 程序设计实用教程 - 3_2_1 菜单及其概述 - 张晋 - 17xie.htm
字号:
<P class=MsoCaption><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">图</SPAN><SPAN
lang=EN-US>3-22<SPAN style="mso-spacerun: yes"> </SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">设置了加速键和热键的菜单项</SPAN></P>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">其中各个菜单项的属性设置如下所示:</SPAN></P>
<P class=a0><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">object
File1: TMenuItem<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">Caption =
'File'<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">object New1:
TMenuItem<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">Caption =
'&New'<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">end<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">object
Open1: TMenuItem<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">Caption =
'&Open'<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">end<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">object
Close1: TMenuItem<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">Caption =
'&Close'<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">end<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">object N1:
TMenuItem<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">Caption =
'-'<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">end<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">object
Save1: TMenuItem<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">Caption =
'&Save'<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">end<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">object
Save2: TMenuItem<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">Caption =
'save &As'<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">end<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">object
About1: TMenuItem<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">Caption =
'&About'<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">end<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">object N2:
TMenuItem<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">Caption =
'-'<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">end<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">object
Exit1: TMenuItem<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">Caption =
'&Exit'<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">end<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">end<o:p></o:p></SPAN></P>
<H4><SPAN lang=EN-US>7</SPAN><SPAN
style="FONT-FAMILY: 楷体_GB2312; mso-ascii-font-family: 'Times New Roman'">.设置复选标志</SPAN></H4>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">所谓的复选标志,指的是当用户选择菜单项中的某一个子菜单项时,在相应的菜单项的前面就会显示一个选中的标记符号“√”,而当用户再次单击这个菜单项时,复选标志又会消失。</SPAN></P>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">复选标志可以在程序的设计阶段通过子菜单项的属性来加以设置,方法是在程序的设计阶段首先用鼠标选中子菜单项。</SPAN></P>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">然后把鼠标移动到菜单项对应的属性列表框中,把</SPAN><SPAN
lang=EN-US>Checked</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">属性设置为</SPAN><SPAN
lang=EN-US>True</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。</SPAN></P>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">如图</SPAN><SPAN
lang=EN-US>3-23</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">所示。那么在程序的运行过程中,该菜单项的前面就会显示一个复选标志。</SPAN></P>
<P class=a><SPAN lang=EN-US><IMG height=201
src="C++ Builder 程序设计实用教程 - 3_2_1 菜单及其概述 - 张晋 - 17xie.files/d0c75209cb.jpg"
width=99 v:shapes="_x0000_i1035"></SPAN></P>
<P class=MsoCaption><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">图</SPAN><SPAN
lang=EN-US>3-23 <SPAN style="mso-spacerun: yes"> </SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">设置菜单的复选标志</SPAN></P>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">当然,我们也可以在程序的运行过程中通过代码来动态的设置菜单项的复选标志,首先在窗体的</SPAN><SPAN
lang=EN-US>FormCreate()</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">事件中加入对菜单项的初始化代码如下所示:</SPAN></P>
<P class=a0><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">void
__fastcall TForm1::FormCreate(TObject *Sender)<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">{<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">Form1->Open1->Checked=true;<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">//</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">初始化菜单项的状态</SPAN><SPAN
lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">}<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">以上的代码在程序运行的初期就把子菜单项</SPAN><SPAN
lang=EN-US>New</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">设置为复选状态,然后在程序的设计阶段用鼠标的左键单击子菜单项</SPAN><SPAN
lang=EN-US>New</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">,在弹出的代码窗口中添加如下所示的相应菜单事件的代码:</SPAN></P>
<P class=a0><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">void
__fastcall TForm1::New1Click(TObject *Sender)<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">{<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">Form1->Open1->Checked=false;<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">Form1->New1->Checked=true;<o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">//</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">鼠标单击后改变菜单项的状态</SPAN><SPAN
lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><o:p></o:p></SPAN></P>
<P class=a0><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">}<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">这段代码能够实现子菜单项复选状态之间的互换,即用鼠标单击子菜单项会改变菜单项的选中状态。以上代码的运行结果如图</SPAN><SPAN
lang=EN-US>3-24</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">所示。</SPAN></P>
<P class=a><SPAN lang=EN-US><IMG height=164
src="C++ Builder 程序设计实用教程 - 3_2_1 菜单及其概述 - 张晋 - 17xie.files/0e43ef8f9c.jpg"
width=261 v:shapes="_x0000_i1036"></SPAN></P>
<P class=MsoCaption><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">图</SPAN><SPAN
lang=EN-US>3-24<SPAN style="mso-spacerun: yes"> </SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">动态改变菜单的复选状态</SPAN></P>
<H4><SPAN lang=EN-US>8</SPAN><SPAN
style="FONT-FAMILY: 楷体_GB2312; mso-ascii-font-family: 'Times New Roman'">.菜单项有效状态设置</SPAN></H4>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">在其它的应用程序中,经常的看到在一些菜单中,程序可以动态的设置菜单的有效状态,即在一定的条件下,某个菜单处于无效的状态,在另外的条件下,菜单项又会变为有效的状态,一个十分明显的例子就是编辑菜单中</SPAN><SPAN
lang=EN-US>Paste</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">子菜单项,当剪贴板中没有数据时,</SPAN><SPAN
lang=EN-US>Paste</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">就会处于无效的状态,一旦剪贴板上有数据,那么</SPAN><SPAN
lang=EN-US>Paste</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">就会立刻的变为有效的状态,在</SPAN><SPAN
lang=EN-US>CBuilder</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中也可以实现这种效果。</SPAN></P>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">下面就讲述一下如何通过代码来在程序的运行过程中动态的改变菜单项的有效状态。</SPAN></P>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">首先按照如图</SPAN><SPAN
lang=EN-US>3-25</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">,按照前面所讲的方法设计一个比较完整的系统下拉式菜单,包括另外两个主菜单</SPAN><SPAN
lang=EN-US>Edit</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN
lang=EN-US>Help</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。</SPAN></P>
<P class=a><SPAN lang=EN-US><IMG height=190
src="C++ Builder 程序设计实用教程 - 3_2_1 菜单及其概述 - 张晋 - 17xie.files/b8d111d99c.jpg"
width=298 v:shapes="_x0000_i1037"></SPAN></P>
<P class=MsoCaption><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">图</SPAN><SPAN
lang=EN-US>3-25<SPAN style="mso-spacerun: yes"> </SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">设计完成的下拉式菜单</SPAN></P>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">然后,在程序的设计过程中,用鼠标的左键双击窗体的空白处,在屏幕上就会弹出一个代码窗口,把光标移动到代码窗口中的</SPAN><SPAN
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -