前面已经介绍从系统队列里获取一条消息,然后经过快捷键的函数检查,又通过字符消息函数的转换,最后要做的事情就是调用DispatchMessage函数,它的意思就是说要把这条消息发送到窗口里的消息处理函数WindowProc。
函数DispatchMessage声明如下:
WINUSERAPI
LRESULT
WINAPI
DispatchMessageA(
     __in CONST MSG *lpMsg);
WINUSERAPI
LRESULT
WINAPI
DispatchMessageW(
     __in CONST MSG *lpMsg);
#ifdef UNICODE
#define DispatchMessage DispatchMessageW
#else
#define DispatchMessage DispatchMessageA
#endif // !UNICODE
lpMsg是指向想向消息处理函数WindowProc发送的消息。
调用这个函数的例子如下:
#001 //主程序入口
#002 //
#003 // 蔡军生 2007/07/19
#004 // QQ: 9073204
#005 //
#006 int APIENTRY _tWinMain(HINSTANCE hInstance,
#007                        HINSTANCE hPrevInstance,
#008                        LPTSTR     lpCmdLine,
#009                        int        nCmdShow)
#010 {
#011  UNREFERENCED_PARAMETER(hPrevInstance);
#012  UNREFERENCED_PARAMETER(lpCmdLine);
#013 
#014    //
#015  MSG msg;
#016  HACCEL hAccelTable;
#017 
#018  // 加载全局字符串。
#019  LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
#020  LoadString(hInstance, IDC_TESTWIN, szWindowClass, MAX_LOADSTRING);
#021  MyRegisterClass(hInstance);
#022 
#023  // 应用程序初始化:
#024  if (!InitInstance (hInstance, nCmdShow))
#025  {
#026          return FALSE;
#027  }
#028 
#029  hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TESTWIN));
#030 
#031  // 消息循环:
#032  BOOL bRet;
#033  while ( (bRet = GetMessage(&msg, NULL, 0, 0)) != 0)
#034  {
#035          if (bRet == -1)
#036          {
#037                //处理出错。
#038 
#039          }
#040          else if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
#041          {
#042                TranslateMessage(&msg);
#043                DispatchMessage(&msg);
#044          }
#045  }
#046 
#047  return (int) msg.wParam;
#048 }
#049 
第43行就是调用函数DispatchMessage发送消息。
码农场