📄 right11-3.htm
字号:
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>第 11 章</title>
<base target="rbottom">
<style>
<!--
.右标题 { font-size: 10pt; color: #000080; text-indent: 0; margin: 0 }
.右内容 { font-size: 10pt; text-align: left; text-indent: 0; line-height: 100%; margin:
0 }
-->
</style>
</head>
<body>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"><span style="position: absolute; left: 4; top: -8"><img border="0" src="1.gif" width="63" height="70"></span></p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p style="line-height: 100%; text-indent: 0; margin: 0" class="右标题" align="left">
<span style="background-color: #CCFF99">第 11 章</span><span style="background-color: #CCFF99">
</span>><span style="background-color: #CCFF99"> 第 3 节 </span>><span style="background-color: #CCFF99">
11.3 对话框</span></p>
<hr color="#008000" size="1">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0" class="右标题"><font LANG="ZH-CN" size="2">对话框是一种弹出式的特殊窗口,它主要用来实现应用程序和用户之间的信息交互。</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">1.
</font><font LANG="ZH-CN" size="2">对话框资源通常有如下的功能:</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font LANG="ZH-CN" size="2">①
发送消息,如提示消息、警告消息;</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">②
接收输入,如用户输入的数据或消息;</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font LANG="ZH-CN">对话框分为两类:模态的和非模态的。当显示一个模态对话框时,使用者不能在对话框与同一个程序中的另一个视窗之间进行切换;</font></font><font LANG="ZH-CN" size="2">与模态对话框不同,非模态对话框允许用户在该对话框与应用程序之间切换窗口。</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 10; margin-bottom: 0"><font size="2">2.
<font LANG="ZH-CN">模态对话框的编程方法</font></font><font LANG="ZH-CN" size="2"> </font></p>
<blockquote>
<p ALIGN="justify" style="line-height: 100%; margin-top: 10; margin-bottom: 0"><font LANG="ZH-CN" size="2">(1)
创建模态对话框资源</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2"> 对话框资源的创建和编辑与菜单相似。创建对话框资源以后在资源描述文件中会出现类似如下所示的代码:</font></p>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
ABOUTBOX <font color="#008000">DIALOG</font> DISCARDABLE 20, 20, 160, 80</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
STYLE DS_MODALFRAME | WS_POPUP</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
FONT 10, "System"</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
BEGIN</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0">
... </p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0">
<font size="2">END</font></p>
<blockquote>
<p ALIGN="justify" style="line-height: 100%; margin-top: 5; margin-bottom: 0"><font LANG="ZH-CN" size="2">(2)
调用函数DialogBox显示模态对话框</font></p>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">
int DialogBox(HINSTANCE hlnstance, LPCTSTR lpTemplate, HWND hWndParent, DLGPROC</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">
lpDialogFunc);</font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">(3)
编写对话框消息处理函数</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font color="#008000" size="2">int
DialogBox(HINSTANCE hlnstance, LPCTSTR lpTemplate, HWND hWndParent, DLGPROC
lpDialogFunc);</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2"> </font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">
在对话框消息处理函数中经常处理以下两类消息:</font></p>
<font SIZE="3">
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font LANG="ZH-CN">①
</font>WM_INITDIALOG<font LANG="ZH-CN">消息</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font LANG="ZH-CN">②
</font>WM_COMMAND<font LANG="ZH-CN">消息</font></font></p>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">(4)
关闭模态对话框</font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">BOOL
EndDialog(HWND hDlg, int nResult);</font></p>
</blockquote>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">3.
<font LANG="ZH-CN">非模态对话框的编程方法</font></font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">(1)
创建非模态对话框资源资源</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font LANG="ZH-CN"><font size="2">创建非模态对话框资源和创建模态对话框资源十分相似。<font color="#FF9933">值得注意的是</font></font></font><font lang="ZH-CN" color="#FF9933" size="2">标识对话框内容的标题一般不可省略,并且</font><font size="2"><font LANG="ZH-CN">非模态对话框样式中应包含</font><font color="#FF9933">WS_VISIABLE<font LANG="ZH-CN">,否则非模态对话框将无法在屏幕上显示。</font></font></font></p>
</blockquote>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">(2)
调用函数CreateDialog显示非模态对话框</font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">HWND
CreateDialog(HINSTANCE hlnstance<font LANG="ZH-CN">,</font>LPCTSTR
lpTemplate<font LANG="ZH-CN">,</font>HWND hWndParent<font LANG="ZH-CN">,</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">DLGPROC
lpDialogFunc);</font></p>
</blockquote>
</blockquote>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">(3)
编写对话框消息处理函数</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font LANG="ZH-CN">由于非模态对话框并不禁止应用程序向其他窗口发送消息,因此,在</font>WinMain<font LANG="ZH-CN">函数的消息循环中必须包含截获发往非模态对话框的消息,并将其发往相应的对话框处理函数进行处理的语句。其消息循环过程的一般形式为:</font></font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">while
(GetMessage (&msg, NULL, 0, 0))</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">if(!IsDialogMessage(hWndModeless,&msg))
//<font LANG="ZH-CN">截获非模态对话框消息并发往处理函数</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">TranslateMessage
(&msg) ;</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">DispatchMessage
(&msg) ;</font></p>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">}</font></p>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">}</font></p>
</blockquote>
</blockquote>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">(4)
关闭非模态对话框</font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">BOOL
DestroyWindow(HWND hDlg);</font></p>
</blockquote>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">4.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -