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

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

BOOL GetMessage(
  LPMSG lpMsg,         // address of structure with message
  HWND hWnd,           // handle of window
  UINT wMsgFilterMin,  // first message
  UINT wMsgFilterMax   // last message
);


//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.
BOOL TranslateMessage(
  CONST MSG *lpMsg   // address of structure with message
);

//The DispatchMessage function dispatches a message to a window procedure.
LONG DispatchMessage(
  CONST MSG *lpmsg   // pointer to structure with message
);


12,窗口过程函数(回调函数)原型:
The 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.

LRESULT CALLBACK WindowProc(  //这里WindowProc是个代号名字。
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
);

说明:两种函数调用约定(__stdcall 和 __cdecl):
#define CALLBACK    __stdcall
//__stdcall 标准调用预定,是PASCAL 调用约定,象DELPHI使用的就是标准调用约定
#define WINAPIV     __cdecl 
// __cdecl 是C 语言形式的调用约定。


主要区别:函数参数传递顺序 和 对堆栈的清除上。
问题:除了那些可变参数的函数调用外,其余的一般都是__stdcall约定。但 C/C++编译默然的是__cdecl约定。所以如果在VC等环境中调用__stdcall约定的函数,必须要在函数声明的时加上 __stdcall 修饰符,以便对这个函数的调用是使用__stdcall约定(如使用DELPHI编写的DLL时候)。
(VC中可通过这途径修改:project|settings..|c/c++|...)


在窗口过程函数中通过一组switch语句来对消息进行处理:
如:
LRESULT CALLBACK WindowProc( 
  HWND hwnd,
  UINT uMsg,
  WPARAM wParam,
  LPARAM lParam  
)
{
    switch(uMsg)
    {
 case WM_PAINT:
  ...
  break;
 case ...
  break;
 case WM_CLOSE:
  //DestroyWindow(hwnd);
   //销毁窗口,并发送WM_DESTROY消息。
  break;
 case WM_DESTROY:
  //PostQuitMessage(0);
  //发送WM_QUIT消息到消息队列中,请求终止。
         //GetMessage()取到WM_QUIT消息后,返回0,退出消息循                //   环,从而终止应用程序。
  break;
 default:
  return DefWindowProc(hwnd,uMsg,wParam,lParam);
 //用缺省的窗口过程处理我们不感兴趣的消息(其它消息)。
 //这是必须的。
    }//switch
 return 0;
}//WindowProc

13,DestroyWindow()函数和PostQuitMessage()函数原型:
//**The DestroyWindow function destroys the specified window. The function sends WM_DESTROY and WM_NCDESTROY messages。

BOOL DestroyWindow(
  HWND hWnd   // handle to window to destroy
);

//**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.
//**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.

When the thread retrieves the WM_QUIT message from its message queue, it should exit its message loop and return control to the system.

VOID PostQuitMessage(
  int nExitCode   // exit code
);

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

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

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top