📄 csdn_文档中心_mfc编程中“占位符和动态创建”技巧的应用.htm
字号:
</SCRIPT>
</B> </TD></TR>
<TR bgColor=#999999>
<TD colSpan=3 height=1></TD></TR></TBODY></TABLE>
<TABLE border=0 width=770>
<TBODY>
<TR>
<TD align=middle bgColor=#fafafa class=td1 vAlign=top width=150><BR>
<SCRIPT src="CSDN_文档中心_MFC编程中“占位符和动态创建”技巧的应用.files/microsoft.js"></SCRIPT>
</TD>
<TD align=middle width=620>
<TABLE bgColor=#eeeeee border=0 cellPadding=0 cellSpacing=0 width=600>
<TBODY>
<TR bgColor=#ffffff>
<TD align=middle height=10 width=50></TD>
<TD align=right><A href="http://www.csdn.net/">CSDN</A> - <A
href="http://www.csdn.net/develop/">文档中心</A> - <FONT
color=#003399>Visual C++</FONT> </TD></TR>
<TR>
<TD align=middle height=5></TD>
<TD align=middle width=500></TD></TR>
<TR>
<TD align=middle bgColor=#003399 height=10><FONT
color=#ffffff>标题</FONT></TD>
<TD><B> MFC编程中“占位符和动态创建”技巧的应用</B> think77(原作)
</TD></TR>
<TR>
<TD align=middle height=5></TD>
<TD align=middle width=500></TD></TR>
<TR>
<TD align=middle bgColor=#003399><FONT color=#ffffff>关键字</FONT></TD>
<TD width=500> MFC编程中“占位符和动态创建”技巧的应用</TD></TR>
<TR>
<TD align=middle height=5></TD>
<TD align=middle width=500></TD></TR></TBODY></TABLE><!--文章说明信息结束//-->
<TABLE border=0 width=600>
<TBODY>
<TR>
<TD align=left><BR>
<P align=right> <B><SPAN
style="FONT-FAMILY: 宋体; FONT-SIZE: 12pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">刘勇</SPAN></B><B><SPAN
lang=EN-US style="FONT-SIZE: 12pt"><SPAN
style="mso-spacerun: yes"> </SPAN></SPAN></B><B><SPAN
style="FONT-FAMILY: 宋体; FONT-SIZE: 12pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">夏安邦</SPAN></B><B><SPAN
lang=EN-US style="FONT-SIZE: 12pt"><?xml:namespace prefix = o ns =
"urn:schemas-microsoft-com:office:office"
/><o:p></o:p></SPAN></B></P>
<P class=MsoNormal style="LINE-HEIGHT: 150%"><SPAN lang=EN-US><SPAN
style="mso-tab-count: 1">
</SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">在</SPAN><SPAN
lang=EN-US>MFC</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">编程中,<A
name=OLE_LINK1>“占位符和动态创建”</A>技巧的应用是十分广泛的,虽然在有些编程书籍和文章中有所涉及,但缺乏系统的介绍和必要的总结,给应用带来不便。本文将对这方面的编程技巧进行介绍,并对该技巧的应用范围和注意事项都进行了详细的总结。</SPAN></P>
<P class=MsoNormal style="LINE-HEIGHT: 200%"><B><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">一、引言</SPAN><SPAN
lang=EN-US><o:p></o:p></SPAN></B></P>
<P class=MsoNormal style="LINE-HEIGHT: 150%"><SPAN lang=EN-US><SPAN
style="mso-tab-count: 1">
</SPAN>MFC</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">提供了</SPAN><SPAN
lang=EN-US>Windows</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">环境下应用开发的一套标准类,多数情况下能满足应用开发的需要,但在某些具体情况下为了开发具有个性的应用,开发者需在</SPAN><SPAN
lang=EN-US>MFC</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的基础上使用一些技巧加以变通,“占位符和动态创建”就是其中一种。</SPAN></P>
<P class=MsoNormal
style="LINE-HEIGHT: 150%; TEXT-INDENT: 21pt"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">动态创建</SPAN><SPAN
lang=EN-US>MFC</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">编程中是一项较常用的技巧,即根据需要在某一位置动态创建有关控件、窗口,但该方法有不便之处,即控件位置的确定是非常繁琐的。“占位符和动态创建”技巧在此基础上,合理利用</SPAN><SPAN
lang=EN-US>Windows</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的显示是在平面上的特点以及</SPAN><SPAN
lang=EN-US>Microsoft Visual C++</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的编辑环境,解决了这一问题。该方法首先在需要显示的位置预先放置一个有特定标示符的“占位符”,“占位符”随创建位置不同可以是一个按钮(在工具栏中),窗格(在状态栏中)或是</SPAN><SPAN
lang=EN-US>Pictrue</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">控件(在对话框中),其大小可根据需要通过编辑环境或有关代码调整;然后在放置“占位符”的地方“动态创建”所需的界面元素。</SPAN></P>
<P class=MsoNormal
style="LINE-HEIGHT: 150%; TEXT-INDENT: 21pt"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">利用这项技巧可以实现</SPAN><SPAN
lang=EN-US>Windows</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">应用中一些常见但</SPAN><SPAN
lang=EN-US>MFC</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">不支持的界面,例如在工具栏、状态栏上创建一些非按钮控件;在对话框中创建工具栏、状态栏或一个控件;在对话框中嵌入一个属性表或另一个对话框,等等。下面通过实例详细介绍该技巧的使用。</SPAN></P>
<P class=MsoNormal style="LINE-HEIGHT: 200%"><B><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">二、实例说明</SPAN><SPAN
lang=EN-US><o:p></o:p></SPAN></B></P>
<P class=MsoNormal style="LINE-HEIGHT: 150%"><SPAN lang=EN-US><SPAN
style="mso-tab-count: 1">
</SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">属性表是一种很有用的界面元素,为我们采集大量数据带来了便利,但如遇到“查找”等需要在对话框中嵌入属性表的情况,则只有借助“占位符和动态创建”技巧才能满足我们的要求。下面通过构造一个类似</SPAN><SPAN
lang=EN-US>Windows</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">“查找”应用的例程来说明该技巧的使用,该例程是</SPAN><SPAN
lang=EN-US>VC++6.0</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">环境下的对话框应用,生成有关代码后可按以下步骤进行。</SPAN></P>
<P class=MsoNormal
style="LINE-HEIGHT: 150%; MARGIN-LEFT: 18pt; TEXT-INDENT: -18pt; mso-list: l1 level1 lfo1; tab-stops: list 18.0pt"><SPAN
lang=EN-US>1.<SPAN style="FONT: 7pt 'Times New Roman'">
</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
style="LINE-HEIGHT: 150%; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">首先在对话框中需要嵌入属性表的位置放置一个</SPAN><SPAN
lang=EN-US>Picture</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">控件,除需设置一个特定的标示符</SPAN><SPAN
lang=EN-US>ID</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">外控件的属性可采用缺省设置。找到控件的方法一般有两种:或是利用</SPAN><SPAN
lang=EN-US>ClassWizard</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">将该控件映射为对话框类的数据成员(是一个</SPAN><SPAN
lang=EN-US>CStatic</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">类的实例),本例采用这种方法;或是在程序中根据“占位符”的特定</SPAN><SPAN
lang=EN-US>ID</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">,利用</SPAN><I><SPAN
lang=EN-US>GetDlgItem </SPAN></I><SPAN lang=EN-US>() </SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数获得对控件的控制权。</SPAN></P>
<P class=MsoNormal
style="LINE-HEIGHT: 150%; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">然后在对话框编辑器中将</SPAN><SPAN
lang=EN-US>Picture</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">控件的大小调整为对应属性页对话框的大小。</SPAN></P>
<P class=MsoNormal
style="LINE-HEIGHT: 150%; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">最后在对话框的</SPAN><SPAN
lang=EN-US>OnInitDialog ( ) </SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数中加入如下代码,以获得“占位符”位置并作相应的坐标转换,其中</SPAN><I><SPAN
lang=EN-US>m_ctrlHolder</SPAN></I><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是“占位符”经</SPAN><SPAN
lang=EN-US>ClassWizard</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">映射所得的</SPAN><SPAN
lang=EN-US>Cstatic</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">类的实例。</SPAN></P>
<P class=MsoNormal
style="LINE-HEIGHT: 150%; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><I><SPAN
lang=EN-US>CRect rect;<o:p></o:p></SPAN></I></P>
<P class=MsoNormal
style="LINE-HEIGHT: 150%; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><I><SPAN
lang=EN-US>m_ctrlHolder.GetWindowRect
(&rect);<o:p></o:p></SPAN></I></P>
<P class=MsoNormal
style="LINE-HEIGHT: 150%; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><I><SPAN
lang=EN-US>ScreenToClient (&rect);<o:p></o:p></SPAN></I></P>
<P class=MsoNormal
style="LINE-HEIGHT: 150%; MARGIN-LEFT: 18pt; TEXT-INDENT: -18pt; mso-list: l1 level1 lfo1; tab-stops: list 18.0pt"><SPAN
lang=EN-US>2.<SPAN style="FONT: 7pt 'Times New Roman'">
</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
style="LINE-HEIGHT: 150%; MARGIN-LEFT: 18pt"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">根据需要生成要创建的属性表和属性页的代码,这一步可照常进行。</SPAN></P>
<P class=MsoNormal style="LINE-HEIGHT: 150%"><SPAN
lang=EN-US>3</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">.属性表的“动态创建”及数据交换</SPAN></P>
<P class=MsoNormal style="LINE-HEIGHT: 150%"><SPAN lang=EN-US><SPAN
style="mso-tab-count: 1">
</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
style="LINE-HEIGHT: 150%; TEXT-INDENT: 21pt"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">然后,在</SPAN><SPAN
lang=EN-US>OnInitDialog</SPAN><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 + -