實例說明 PeekMessage與GetMessage的區別


PeekMessage與GetMessage的對比
相同點:
PeekMessage函數與GetMessage函數都用於查看應用程序消息隊列,有消息時將隊列中

的消息派發出去。

不同點:
無論應用程序消息隊列是否有消息,PeekMessage函數都立即返回,程序得以繼續執行

后面的語句(無消息則執行其它指令,有消息時一般要將消息派發出去,再執行其它

指令)。
GetMessage函數只有在消息對立中有消息時返回,隊列中無消息就會一直等,直至下

一個消息出現時才返回。在等的這段時間,應用程序不能執行任何指令。

 

(從他們的不同點上來看,PeekMessage函數有點像“乞丐行乞”,有你就施舍點,沒

有也不強求。GetMessage函數有點像“強盜打劫”,有你得給,沒有我就等你什么時

候有了再給,這段時間我什么都不干,我就等你。)

 

下面的程序用來在窗體內畫隨機生成的矩形,分別使用PeekMessage函數和GetMessage

函數實現,讀者可以種實際效果看出他們兩者的區別。

#include <windows.h>
#include <stdlib.h>           // for the rand function

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
void DrawRectangle (HWND) ;

int cxClient, cyClient ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     static TCHAR szAppName[] = TEXT ("RandRect") ;
     HWND         hwnd ;
     MSG          msg ;
     WNDCLASS     wndclass ;
     
     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;

     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, TEXT ("This program requires Windows NT!"),
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }
     
     hwnd = CreateWindow (szAppName, TEXT ("Random Rectangles"),
                          WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          NULL, NULL, hInstance, NULL) ;
     
     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;
     
     //用於替換的部分 
    while (TRUE)
     {
          if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
          {
               if (msg.message == WM_QUIT)
                    break ;
               TranslateMessage (&msg) ;
               DispatchMessage (&msg) ;
          }
          else
               DrawRectangle (hwnd) ;
     }
    //用於替換的部分 
     return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM

lParam)
{
     switch (iMsg)
     {
     case WM_SIZE:
          cxClient = LOWORD (lParam) ;
          cyClient = HIWORD (lParam) ;
          return 0 ;
          
     case WM_DESTROY:
          PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
}

void DrawRectangle (HWND hwnd)
{
     HBRUSH hBrush ;
     HDC    hdc ;
     RECT   rect ;
     
     if (cxClient == 0 || cyClient == 0)
          return ;
     
     SetRect (&rect, rand () % cxClient, rand () % cyClient,
                     rand () % cxClient, rand () % cyClient) ;
     
     hBrush = CreateSolidBrush (
                    RGB (rand () % 256, rand () % 256, rand () % 256)) ;
     hdc = GetDC (hwnd) ;
     
     FillRect (hdc, &rect, hBrush) ;
     ReleaseDC (hwnd, hdc) ;
     DeleteObject (hBrush) ;

以上程序用PeekMessage函數實現,在應用程序消息隊列沒有消息時,隨機生成的矩形

將堅持不懈地畫下去。當有消息產生時,PeekMessage函數獲取並派發消息出去,然后

繼續畫隨機生成的矩形。

下面部分代碼用於替換WinMain函數中“用於替換的部分”的代碼。
 while (TRUE)
     {
          if (GetMessage (&msg, NULL, 0, 0))
          {
               if (msg.message == WM_QUIT)
                    break ;
               TranslateMessage (&msg) ;
               DispatchMessage (&msg) ;
           DrawRectangle (hwnd) ;
          } 
          else
                break;
     }
替換后,應用程序產生一個消息(鼠標移動、改變窗體大小等),在窗體內畫一個隨

機生成的矩形,無消息產生時,窗體無變化。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM