随笔 - 47, 文章 - 10, 评论 - 8, 引用 - 0
数据加载中……

孙鑫VC++讲座笔记-(1)Windows程序内部运行机制

1,windows程序设计是种事件驱动方式的程序设计,主要基于消息的。当用户需要完成某种功能时,需要调用OS某种支持,然后OS将用户的需要包装成消息,并投入到消息队列中,最后应用程序从消息队列中取走消息并进行响应。
2,消息结构:
1typedef struct tagMSG {     // msg 
2    HWND   hwnd;     //接收消息的窗口句柄。和哪个窗口相关联。
3    UINT   message;  //消息标识。消息本身是什么。
4    WPARAM wParam;   //消息的附加信息。具体取决于消息本身。  
5    LPARAM lParam; 
6    DWORD  time;     //消息投递时间。 
7    POINT  pt;       //消息投递时,光标在屏幕上的位置。 
8}
 MSG; 

3,消息队列:
每个应用程序OS都为它建立一个消息队列,消息队列是个先进先出的缓冲区,其中每个元素都是一个消息,OS将生成的每个消息按先后顺序放进消息队列中,应用程序总是取走当前消息队列中的第一条消息,应用程序取走消息后便知道用户的操作和程序的状态,然后对其处理即消息响应,消息响应通过编码实现。

4,使用VC编程除了良好的C基础外还需要掌握两方面:
一,消息本身。不同消息所代表的用户操作和应用程序的状态。
二,对于某个特定的消息来说,要让OS执行某个特定的功能去响应消息。

5,Window程序入口:

1int WINAPI WinMain(
2  HINSTANCE hInstance,  // 当前事例句柄。
3  HINSTANCE hPrevInstance,  // 先前事例句柄。
4  LPSTR lpCmdLine,      // 命令行指针
5  int nCmdShow          // (窗口)显示的状态
6);
7
说明:WinMain函数是Windows程序入口点函数,由OS调用,当OS启动应用程序的时候,winmain函数的参数由OS传递的。

6,创建一个完整的窗口需要经过下面四个操作步骤:
一,设计一个窗口类;如:WNDCLASS wndcls;
二,注册窗口类;    如:RegisterClass(&wndcls);
三,创建窗口;      如:CreateWindow(),CreateWindowEX();
四,显示及更新窗口。如:ShowWindow(),UpdateWindow();

说明:创建窗口的时候一定要基于已经注册的窗口类.
7,Windows提供的窗口类:

 1typedef struct _WNDCLASS 
 2    UINT    style;        //窗口的类型
 3    WNDPROC lpfnWndProc;  //窗口过程函数指针(回调函数)
 4    int     cbClsExtra; //窗口类附加字节,为该类窗口所共享。通常0。
 5    int     cbWndExtra; //窗口附加字节。通常设为0。
 6    HANDLE  hInstance;  //当前应用程序事例句柄。
 7    HICON   hIcon;      //图标句柄 LoadIcon();
 8    HCURSOR hCursor;    //光标句柄 LoadCursor();
 9    HBRUSH  hbrBackground; //画刷句柄 (HBRUSH)GetStockObject();
10    LPCTSTR lpszMenuName;  //菜单名字
11    LPCTSTR lpszClassName; //类的名字 
12}
 WNDCLASS; 
13

8,窗口类注册:
1ATOM RegisterClass(
2  CONST WNDCLASS *lpWndClass   // address of structure with class 
3                               // data
4);

9,创建窗口:
 1HWND CreateWindow(
 2  LPCTSTR lpClassName,  // pointer to registered class name
 3  LPCTSTR lpWindowName, // pointer to window name
 4  DWORD dwStyle,        // window style
 5  int x,                // horizontal position of window
 6  int y,                // vertical position of window
 7  int nWidth,           // window width
 8  int nHeight,          // window height
 9  HWND hWndParent,      // handle to parent or owner window
10  HMENU hMenu,          // handle to menu or child-window identifier
11  HANDLE hInstance,     // handle to application instance
12  LPVOID lpParam        // pointer to window-creation data
13);

10,显示和更新窗口窗口:
1BOOL ShowWindow(
2  HWND hWnd,     // handle to window
3  int nCmdShow   // show state of window
4);
5BOOL UpdateWindow(
6  HWND hWnd   // handle of window
7);

11,消息循环:
1MSG msg;
2while(GetMessage(&msg,))    //从消息队列中取出一条消息
3{
4 TranslateMessage(&msg); //进行消息(如键盘消息)转换
5 DispatchMessage(&msg); //分派消息到窗口的回调函数处理,(OS调用窗口回调函数进行处理)。
6}
其中:
 1//**The GetMessage function retrieves a message from the calling thread's message queue and places it in the specified structure. 
 2//**If the function retrieves a message other than WM_QUIT, the return value is nonzero.If the function retrieves the WM_QUIT message, the return value is zero. If there is an error, the return value is -1. 
 3
 4BOOL GetMessage(
 5  LPMSG lpMsg,         // address of structure with message
 6  HWND hWnd,           // handle of window
 7  UINT wMsgFilterMin,  // first message
 8  UINT wMsgFilterMax   // last message
 9);
10
11
12//The TranslateMessage function translates virtual-key messages into character messages. The character messages are posted to the calling thread's message queue, to be read the next time the thread calls the GetMessage or PeekMessage function. 
13BOOL TranslateMessage(
14  CONST MSG *lpMsg   // address of structure with message
15);
16
17//The DispatchMessage function dispatches a message to a window procedure. 
18LONG DispatchMessage(
19  CONST MSG *lpmsg   // pointer to structure with message
20);
21
22

12,窗口过程函数(回调函数)原型:
1The WindowProc function is an application-defined function that processes messages sent to a window. The WNDPROC type defines a pointer to this callback function. WindowProc is a placeholder(占位符) for the application-defined function name. 
2
3LRESULT CALLBACK WindowProc(  //这里WindowProc是个代号名字。
4  HWND hwnd,      // handle to window
5  UINT uMsg,      // message identifier
6  WPARAM wParam,  // first message parameter
7  LPARAM lParam   // second message parameter
8);
9

说明:两种函数调用约定(__stdcall 和 __cdecl):

1#define CALLBACK    __stdcall 
2//__stdcall 标准调用预定,是PASCAL 调用约定,象DELPHI使用的就是标准调用约定
3#define WINAPIV     __cdecl  
4// __cdecl 是C 语言形式的调用约定。
主要区别:函数参数传递顺序 和 对堆栈的清除上。
问题:除了那些可变参数的函数调用外,其余的一般都是__stdcall约定。但 C/C++编译默然的是__cdecl约定。所以如果在VC等环境中调用__stdcall约定的函数,必须要在函数声明的时加上 __stdcall 修饰符,以便对这个函数的调用是使用__stdcall约定(如使用DELPHI编写的DLL时候)。
(VC中可通过这途径修改:project|settings..|c/c++|...)
在窗口过程函数中通过一组switch语句来对消息进行处理:
如:
 1LRESULT CALLBACK WindowProc(  
 2  HWND hwnd,
 3  UINT uMsg,
 4  WPARAM wParam,
 5  LPARAM lParam   
 6)
 7{
 8    switch(uMsg)
 9    {
10 case WM_PAINT:
11  
12  break;
13 case 
14  break;
15 case WM_CLOSE:
16  //DestroyWindow(hwnd);
17   //销毁窗口,并发送WM_DESTROY消息。
18  break;
19 case WM_DESTROY:
20  //PostQuitMessage(0);
21  //发送WM_QUIT消息到消息队列中,请求终止。
22         //GetMessage()取到WM_QUIT消息后,返回0,退出消息循                //   环,从而终止应用程序。
23  break;
24 default:
25  return DefWindowProc(hwnd,uMsg,wParam,lParam);
26 //用缺省的窗口过程处理我们不感兴趣的消息(其它消息)。
27 //这是必须的。
28    }
//switch
29 return 0;
30}
//WindowProc
31

13,DestroyWindow()函数和PostQuitMessage()函数原型:
 1//**The DestroyWindow function destroys the specified window. The function sends WM_DESTROY and WM_NCDESTROY messages。
 2
 3BOOL DestroyWindow(
 4  HWND hWnd   // handle to window to destroy
 5);
 6
 7//**The PostQuitMessage function indicates to the system that a thread has made a request to terminate (quit). It is typically used in response to a WM_DESTROY message. 
 8//**The PostQuitMessage function posts a WM_QUIT message to the thread's message queue and returns immediately; the function simply indicates(预示,通知) to the system that the thread is requesting to quit at some time in the future. 
 9
10When the thread retrieves the WM_QUIT message from its message queue, it should exit its message loop and return control to the system.
11
12VOID PostQuitMessage(
13  int nExitCode   // exit code
14);
15

14,关于DC句柄获取:
a)使用BeginPaint(),EndPaint()对。注意只能在响应WM_PAINT消息时使用。
b)使用GetDc(),ReleaseDC()对。注意他们不能在响应WM_PAINT中使用。

posted on 2006-04-04 11:07 编程之道 阅读(171) 评论(0)  编辑 收藏 引用 所属分类: C/C++


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理