最新消息:

VC++复习笔记1-Windows程序的运行机制

C/C++ admin 2936浏览 0评论

没用VC++很久了,趁这个暑假有时间复习一下吧.

看了孙鑫老师的视频,感觉很多地方都生疏了.今天就复习第一章,Windows程序的运行机制.

先说一下大致的结构:

首先是入口函数WinMain,然后定义一个窗口类,接着注册窗口类,再创建窗口,显示窗口,编写消息循环机制,编写回调函数.

下面是一个简单的Windows程序的C代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<span style="color: #339933;">#include &lt;windows.h&gt;</span>
<span style="color: #339933;">#include &lt;stdio.h&gt;</span>

LRESULT CALLBACK WinProc<span style="color: #009900;">(</span>
  HWND hwnd<span style="color: #339933;">,</span>      <span style="color: #666666; font-style: italic;">// handle to window</span>
  UINT uMsg<span style="color: #339933;">,</span>      <span style="color: #666666; font-style: italic;">// message identifier</span>
  WPARAM wParam<span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">// first message parameter</span>
  LPARAM lParam   <span style="color: #666666; font-style: italic;">// second message parameter</span>
<span style="color: #009900;">)</span><span style="color: #339933;">;</span>

<span style="color: #993333;">int</span> WINAPI WinMain<span style="color: #009900;">(</span>
  HINSTANCE hInstance<span style="color: #339933;">,</span>      <span style="color: #666666; font-style: italic;">// handle to current instance</span>
  HINSTANCE hPrevInstance<span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">// handle to previous instance</span>
  LPSTR lpCmdLine<span style="color: #339933;">,</span>          <span style="color: #666666; font-style: italic;">// command line</span>
  <span style="color: #993333;">int</span> nCmdShow              <span style="color: #666666; font-style: italic;">// show state</span>
<span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
 WNDCLASS wndcls<span style="color: #339933;">;</span>

 wndcls.<span style="color: #202020;">lpfnWndProc</span><span style="color: #339933;">=</span>WinProc<span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;">//注意这:回调函数名</span>
 wndcls.<span style="color: #202020;">lpszClassName</span><span style="color: #339933;">=</span><span style="color: #ff0000;">"WinClass"</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//注意:类名</span>
 ...

 RegisterClass<span style="color: #009900;">(</span><span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span>wndcls<span style="color: #009900;">)</span><span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;">//注册窗口类</span>

 HWND hwnd<span style="color: #339933;">;</span>
 hwnd<span style="color: #339933;">=</span>CreateWindow<span style="color: #009900;">(</span><span style="color: #ff0000;">"WinClass"</span><span style="color: #339933;">,</span><span style="color: #ff0000;">"这是标题"</span><span style="color: #339933;">,</span>WS_OVERLAPPEDWINDOW<span style="color: #339933;">,</span>
  <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span><span style="color: #0000dd;">600</span><span style="color: #339933;">,</span><span style="color: #0000dd;">400</span><span style="color: #339933;">,</span>NULL<span style="color: #339933;">,</span>NULL<span style="color: #339933;">,</span>hInstance<span style="color: #339933;">,</span>NULL<span style="color: #009900;">)</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//具体参数参阅MSDN</span>

 ShowWindow<span style="color: #009900;">(</span>hwnd<span style="color: #339933;">,</span>SW_SHOWNORMAL<span style="color: #009900;">)</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//显示窗口</span>
 UpdateWindow<span style="color: #009900;">(</span>hwnd<span style="color: #009900;">)</span><span style="color: #339933;">;</span>     <span style="color: #666666; font-style: italic;">//刷新窗口</span>

 MSG msg<span style="color: #339933;">;</span>
 <span style="color: #b1b100;">while</span><span style="color: #009900;">(</span>GetMessage<span style="color: #009900;">(</span><span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span>msg<span style="color: #339933;">,</span>NULL<span style="color: #339933;">,</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span><span style="color: #0000dd;">0</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
 <span style="color: #009900;">{</span>
  TranslateMessage<span style="color: #009900;">(</span><span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span>msg<span style="color: #009900;">)</span><span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;">//翻译消息</span>
  DispatchMessage<span style="color: #009900;">(</span><span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span>msg<span style="color: #009900;">)</span><span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;">//将消息交给回调函数处理</span>
 <span style="color: #009900;">}</span>
 <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>

LRESULT CALLBACK WinSunProc<span style="color: #009900;">(</span>  <span style="color: #666666; font-style: italic;">//回调函数</span>
  HWND hwnd<span style="color: #339933;">,</span>      <span style="color: #666666; font-style: italic;">// handle to window</span>
  UINT uMsg<span style="color: #339933;">,</span>      <span style="color: #666666; font-style: italic;">// message identifier</span>
  WPARAM wParam<span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">// first message parameter</span>
  LPARAM lParam   <span style="color: #666666; font-style: italic;">// second message parameter</span>
<span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
 <span style="color: #b1b100;">switch</span><span style="color: #009900;">(</span>uMsg<span style="color: #009900;">)</span>   <span style="color: #666666; font-style: italic;">//消息处理</span>
 <span style="color: #009900;">{</span>
 <span style="color: #b1b100;">case</span> WM_CLOSE<span style="color: #339933;">:</span>
  DestroyWindow<span style="color: #009900;">(</span>hwnd<span style="color: #009900;">)</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">case</span> WM_DESTROY<span style="color: #339933;">:</span>
  PostQuitMessage<span style="color: #009900;">(</span><span style="color: #0000dd;">0</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>
  <span style="color: #b1b100;">return</span> DefWindowProc<span style="color: #009900;">(</span>hwnd<span style="color: #339933;">,</span>uMsg<span style="color: #339933;">,</span>wParam<span style="color: #339933;">,</span>lParam<span style="color: #009900;">)</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">}</span>
 <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>

这里要注意窗口类里的lpfnWndProc和lpszClassName两个参数是与回调函数和CreateWindow里的参数是对应的.

转载请注明:爱开源 » VC++复习笔记1-Windows程序的运行机制

您必须 登录 才能发表评论!