📄 right9-4.htm
字号:
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><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">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"><o:p>
</span></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN"><a href="example9-1.htm" target="_blank">【例</a></font><a href="example9-1.htm" target="_blank">9-1<font FACE="??ì?,SimSun" LANG="ZH-CN">】</font></a><font FACE="??ì?,SimSun" LANG="ZH-CN">简单窗口显示。</font></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">3.
Windows API </font><font FACE="??ì?,SimSun" LANG="ZH-CN"><font size="2">应用程序结构分析</font></p>
</font><font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2">Windows<font FACE="??ì?,SimSun" LANG="ZH-CN">的应用程序具有相对固定的基本结构,其中由入口函数</font>WinMain<font FACE="??ì?,SimSun" LANG="ZH-CN">和窗口函数构成基本框架,并包含各种数据类型、数据结构与函数等。</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font FACE="oúì?,SimHei" LANG="ZH-CN" size="2">
1>
WinMain函数</font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">WinMain<font FACE="??ì?,SimSun" LANG="ZH-CN">函数是所有</font>Windows<font FACE="??ì?,SimSun" LANG="ZH-CN">应用程序的入口,类似于</font>C<font FACE="??ì?,SimSun" LANG="ZH-CN">语言中的</font>main<font FACE="??ì?,SimSun" LANG="ZH-CN">函数,其功能是完成一系列的定义和初始化工作,并产生消息循环。</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">WinMain<font FACE="??ì?,SimSun" LANG="ZH-CN">函数有三个基本的组成部分:函数说明、初始化和消息循环。</font></font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">(1)
WinMain<font FACE="??ì?,SimSun" LANG="ZH-CN"></font><font FACE="??ì?,SimSun" LANG="ZH-CN">函数说明</font></font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">int
WINAPI WinMain (</font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">HINSTANCE hInstance,
//<font FACE="??ì?,SimSun" LANG="ZH-CN">应用程序当前实例句柄</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">HINSTANCE
hPrevInstance, //<font FACE="??ì?,SimSun" LANG="ZH-CN">应用程序其他实例句柄</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">PSTR
szCmdLine, //<font FACE="??ì?,SimSun" LANG="ZH-CN">指向程序命令行参数的指针</font></font></p>
<b><font SIZE="1">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font></b><font size="2" color="#008000">int
iCmdShow ) //<font FACE="??ì?,SimSun" LANG="ZH-CN">应用程序开始执行时窗口显示方式的整数值</font></font></p>
</blockquote>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">(2)
<font FACE="??ì?,SimSun" LANG="ZH-CN">初始化</font></font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">初始化包括窗口类的定义、注册、创建窗口实例和显示四个部分。</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 10; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">
①
窗口类的定义</font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">窗口类定义通过给窗口类数据结构</font>WNDCLASS<font FACE="??ì?,SimSun" LANG="ZH-CN">赋值完成,结构体类型</font>WNDCLASS<font FACE="??ì?,SimSun" LANG="ZH-CN">的定义如下:</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">typedef
struct tagWNDCLASS {</font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">UINT
style;
//<font FACE="??ì?,SimSun" LANG="ZH-CN">窗口类的样式,一般设置为</font>0</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">WNDPROC
lpfnWndProc; //<font FACE="??ì?,SimSun" LANG="ZH-CN">指向窗口函数的指针</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">int
cbClsExtra; //<font FACE="??ì?,SimSun" LANG="ZH-CN">分配在窗口类结构后的字节数</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">int
cbWndExtra; //<font FACE="??ì?,SimSun" LANG="ZH-CN">分配在窗口实例后的字节数</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">HINSTANCE
hInstance; //<font FACE="??ì?,SimSun" LANG="ZH-CN">定义窗口类的应用程序的实例句柄</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">HICON
hIcon; //<font FACE="??ì?,SimSun" LANG="ZH-CN">窗口类的图标</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">HCURSOR
hCursor; //<font FACE="??ì?,SimSun" LANG="ZH-CN">窗口类的光标</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">HBRUSH
hbrBackground; //<font FACE="??ì?,SimSun" LANG="ZH-CN">窗口类的背景刷</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">LPCWSTR
lpszMenuName; //<font FACE="??ì?,SimSun" LANG="ZH-CN">窗口类的菜单名</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">LPCWSTR
lpszClassName; //<font FACE="??ì?,SimSun" LANG="ZH-CN">窗口类名</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">}
WNDCLASS;</font></p>
</blockquote>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">
</font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">②
注册窗口类</font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">RegisterClass(&wndclass);
//wndclass<font FACE="??ì?,SimSun" LANG="ZH-CN">为窗口类结构</font></font></p>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">
③
创建窗口</font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">创建一个窗口类的实例由函数</font>CreatWindow(
)<font FACE="??ì?,SimSun" LANG="ZH-CN">实现,该函数的原形为:</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">HWND
CreateWindow(</font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">LPCTSTR
lpClassName, // <font FACE="??ì?,SimSun" LANG="ZH-CN">窗口类名</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">LPCTSTR
lpWindowName, // <font FACE="??ì?,SimSun" LANG="ZH-CN">窗口标题名称</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">DWORD
dwStyle, // <font FACE="??ì?,SimSun" LANG="ZH-CN">窗口样式,常用窗口样式见表</font>9-5</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">int
x,
// <font FACE="??ì?,SimSun" LANG="ZH-CN">窗口左上角横坐标</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">int
y,
// <font FACE="??ì?,SimSun" LANG="ZH-CN">窗口左上角纵坐标</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">int
nWidth, // <font FACE="??ì?,SimSun" LANG="ZH-CN">窗口宽度</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">int
nHeight, // <font FACE="??ì?,SimSun" LANG="ZH-CN">窗口高度</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">HWND
hWndParent, //<font FACE="??ì?,SimSun" LANG="ZH-CN">该窗口的父窗口句柄</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">HMENU
hMenu, // <font FACE="??ì?,SimSun" LANG="ZH-CN">窗口主菜单句柄</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">HANDLE
hInstance, // <font FACE="??ì?,SimSun" LANG="ZH-CN">创建窗口的应用程序当前句柄</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">LPVOID
lpParam // <font FACE="??ì?,SimSun" LANG="ZH-CN">指向一个传递给窗口的参数值的指针</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">);</font></p>
</blockquote>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">
④
显示窗口</font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">ShowWindow(hwnd,nCmdShow);</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">UpdateWindow(hwnd);</font></p>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">(3)
<font FACE="??ì?,SimSun" LANG="ZH-CN">消息循环</font></font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">MSG
msg //<font FACE="??ì?,SimSun" LANG="ZH-CN">定义一个</font>MSG<font FACE="??ì?,SimSun" LANG="ZH-CN">类型的变量</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">while
(GetMessage (&msg, NULL, 0, 0)) //<font FACE="??ì?,SimSun" LANG="ZH-CN">产生一个消息循环,等待消息的到来</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">{</font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">TranslateMessage
(&msg) ; //<font FACE="??ì?,SimSun" LANG="ZH-CN">翻译消息</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">DispatchMessage
(&msg) ; //<font FACE="??ì?,SimSun" LANG="ZH-CN">将消息传递给窗口函数,以便处理相应的消息</font></font></p>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">}</font></p>
</blockquote>
</blockquote>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font FACE="oúì?,SimHei" LANG="ZH-CN" size="2">
2>
窗口函数</font></p>
<font SIZE="3">
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" 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 FACE="??ì?,SimSun" LANG="ZH-CN" size="2">窗口函数的一般形式如下:</font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">LRESULT
CALLBACK WndProc (</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">HWND hwnd,
//<font FACE="??ì?,SimSun" LANG="ZH-CN">窗口句柄</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">UINT
iMsg,
//<font FACE="??ì?,SimSun" LANG="ZH-CN">代表所处理消息的消息值</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">WPARAM
wParam, //<font FACE="??ì?,SimSun" LANG="ZH-CN">消息的附加参数</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">LPARAM
lParam) //<font FACE="??ì?,SimSun" LANG="ZH-CN">消息的附加参数</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">{</font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">switch
(iMsg)</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">{</font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">case
WM_DESTROY :</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">PostQuitMessage
(0) ; //<font FACE="??ì?,SimSun" LANG="ZH-CN">调用函数</font>PostQuitessage<font FACE="??ì?,SimSun" LANG="ZH-CN">退出程序</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">return
0 ;</font></p>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">}</font></p>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">return
DefWindowProc (hwnd, iMsg, wParam, lParam) ;</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">}</font></p>
</blockquote>
</blockquote>
<hr size="1" color="#008000">
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> <span style="position: absolute; left: 36; top: 2085"><a href="right9-3.htm" target="_self"><img border="0" src="rightd1.gif" width="113" height="70"></a></span>
<span style="position: absolute; left: 482; top: 2085"><a href="right9-5.htm" target="_self"><img border="0" src="rightd2.gif" width="124" height="63"></a></span></p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -