PeekMessage与GetMessage区别

原文地址:http://www.cnblogs.com/faceang/archive/2010/05/25/1743757.html

 

PeekMessage与GetMessage的对比
相同点:
           PeekMessage函数与GetMessage函数都用于查看应用程序消息队列,有消息时将队列中 
的消息派发出去。

不同点:
          无论应用程序消息队列是否有消息,PeekMessage函数都立即返回,程序得以继续执行
后面的语句(无消息则执行其它指令,有消息时一般要将消息派发出去,再执行其它指令)。GetMessage函数只有在消息对立中有消息时返回,队列中无消息就会一直等,直至下一个消息出现时才返回。在等的这段时间,应用程序不能执行任何指令。

(从他们的不同点上来看,PeekMessage函数有点像“乞丐行乞”,有你就施舍点,没有也不强。GetMessage函数有点像“强盗打劫”,有你得给,没有我就等你什么时候有了再给,这段时间我什么都不干,我就等你。)

下面的程序用来在窗体内画随机生成的矩形,分别使用PeekMessage函数和GetMessage函数实现,读者可以种实际效果看出他们两者的区别。

  1. <span style="font-size:18px;">#include <windows.h> 
  2. #include <stdlib.h>           // for the rand function 
  3.  
  4. LRESULT CALLBACK WndProc (HWNDUINTWPARAMLPARAM) ; 
  5. void DrawRectangle (HWND) ; 
  6.  
  7. int cxClient, cyClient ; 
  8.  
  9. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, 
  10.                     PSTR szCmdLine, int iCmdShow) 
  11.      static TCHAR szAppName[] = TEXT ("RandRect") ; 
  12.      HWND         hwnd ; 
  13.      MSG          msg ; 
  14.      WNDCLASS     wndclass ; 
  15.       
  16.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ; 
  17.      wndclass.lpfnWndProc   = WndProc ; 
  18.      wndclass.cbClsExtra    = 0 ; 
  19.      wndclass.cbWndExtra    = 0 ; 
  20.      wndclass.hInstance     = hInstance ; 
  21.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ; 
  22.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ; 
  23.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; 
  24.      wndclass.lpszMenuName  = NULL ; 
  25.      wndclass.lpszClassName = szAppName ; 
  26.  
  27.      if (!RegisterClass (&wndclass)) 
  28.      { 
  29.           MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
  30.                       szAppName, MB_ICONERROR) ; 
  31.           return 0 ; 
  32.      } 
  33.       
  34.      hwnd = CreateWindow (szAppName, TEXT ("Random Rectangles"), 
  35.                           WS_OVERLAPPEDWINDOW, 
  36.                           CW_USEDEFAULT, CW_USEDEFAULT, 
  37.                           CW_USEDEFAULT, CW_USEDEFAULT, 
  38.                           NULL, NULL, hInstance, NULL) ; 
  39.       
  40.      ShowWindow (hwnd, iCmdShow) ; 
  41.      UpdateWindow (hwnd) ; 
  42.       
  43.      //用于替换的部分  
  44.     while (TRUE) 
  45.      { 
  46.           if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) 
  47.           { 
  48.                if (msg.message == WM_QUIT) 
  49.                     break ; 
  50.                TranslateMessage (&msg) ; 
  51.                DispatchMessage (&msg) ; 
  52.           } 
  53.           else 
  54.                DrawRectangle (hwnd) ; 
  55.      } 
  56.     //用于替换的部分  
  57.      return msg.wParam ; 
  58.  
  59. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM  
  60.  
  61. lParam) 
  62.      switch (iMsg) 
  63.      { 
  64.      case WM_SIZE: 
  65.           cxClient = LOWORD (lParam) ; 
  66.           cyClient = HIWORD (lParam) ; 
  67.           return 0 ; 
  68.            
  69.      case WM_DESTROY: 
  70.           PostQuitMessage (0) ; 
  71.           return 0 ; 
  72.      } 
  73.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ; 
  74.  
  75. void DrawRectangle (HWND hwnd) 
  76.      HBRUSH hBrush ; 
  77.      HDC    hdc ; 
  78.      RECT   rect ; 
  79.       
  80.      if (cxClient == 0 || cyClient == 0) 
  81.           return ; 
  82.       
  83.      SetRect (&rect, rand () % cxClient, rand () % cyClient, 
  84.                      rand () % cxClient, rand () % cyClient) ; 
  85.       
  86.      hBrush = CreateSolidBrush ( 
  87.                     RGB (rand () % 256, rand () % 256, rand () % 256)) ; 
  88.      hdc = GetDC (hwnd) ; 
  89.       
  90.      FillRect (hdc, &rect, hBrush) ; 
  91.      ReleaseDC (hwnd, hdc) ; 
  92.      DeleteObject (hBrush) ; 
  93. }</span> 

  1. <span style="font-size:18px;">#include <windows.h>  
  2. #include <stdlib.h>           // for the rand function  
  3.   
  4. LRESULT CALLBACK WndProc (HWNDUINTWPARAMLPARAM) ;  
  5. void DrawRectangle (HWND) ;  
  6.   
  7. int cxClient, cyClient ;  
  8.   
  9. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,  
  10.                     PSTR szCmdLine, int iCmdShow)  
  11. {  
  12.      static TCHAR szAppName[] = TEXT ("RandRect") ;  
  13.      HWND         hwnd ;  
  14.      MSG          msg ;  
  15.      WNDCLASS     wndclass ;  
  16.        
  17.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;  
  18.      wndclass.lpfnWndProc   = WndProc ;  
  19.      wndclass.cbClsExtra    = 0 ;  
  20.      wndclass.cbWndExtra    = 0 ;  
  21.      wndclass.hInstance     = hInstance ;  
  22.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;  
  23.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;  
  24.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;  
  25.      wndclass.lpszMenuName  = NULL ;  
  26.      wndclass.lpszClassName = szAppName ;  
  27.   
  28.      if (!RegisterClass (&wndclass))  
  29.      {  
  30.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),  
  31.                       szAppName, MB_ICONERROR) ;  
  32.           return 0 ;  
  33.      }  
  34.        
  35.      hwnd = CreateWindow (szAppName, TEXT ("Random Rectangles"),  
  36.                           WS_OVERLAPPEDWINDOW,  
  37.                           CW_USEDEFAULT, CW_USEDEFAULT,  
  38.                           CW_USEDEFAULT, CW_USEDEFAULT,  
  39.                           NULL, NULL, hInstance, NULL) ;  
  40.        
  41.      ShowWindow (hwnd, iCmdShow) ;  
  42.      UpdateWindow (hwnd) ;  
  43.        
  44.      //用于替换的部分   
  45.     while (TRUE)  
  46.      {  
  47.           if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))  
  48.           {  
  49.                if (msg.message == WM_QUIT)  
  50.                     break ;  
  51.                TranslateMessage (&msg) ;  
  52.                DispatchMessage (&msg) ;  
  53.           }  
  54.           else  
  55.                DrawRectangle (hwnd) ;  
  56.      }  
  57.     //用于替换的部分   
  58.      return msg.wParam ;  
  59. }  
  60.   
  61. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM   
  62.   
  63. lParam)  
  64. {  
  65.      switch (iMsg)  
  66.      {  
  67.      case WM_SIZE:  
  68.           cxClient = LOWORD (lParam) ;  
  69.           cyClient = HIWORD (lParam) ;  
  70.           return 0 ;  
  71.             
  72.      case WM_DESTROY:  
  73.           PostQuitMessage (0) ;  
  74.           return 0 ;  
  75.      }  
  76.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;  
  77. }  
  78.   
  79. void DrawRectangle (HWND hwnd)  
  80. {  
  81.      HBRUSH hBrush ;  
  82.      HDC    hdc ;  
  83.      RECT   rect ;  
  84.        
  85.      if (cxClient == 0 || cyClient == 0)  
  86.           return ;  
  87.        
  88.      SetRect (&rect, rand () % cxClient, rand () % cyClient,  
  89.                      rand () % cxClient, rand () % cyClient) ;  
  90.        
  91.      hBrush = CreateSolidBrush (  
  92.                     RGB (rand () % 256, rand () % 256, rand () % 256)) ;  
  93.      hdc = GetDC (hwnd) ;  
  94.        
  95.      FillRect (hdc, &rect, hBrush) ;  
  96.      ReleaseDC (hwnd, hdc) ;  
  97.      DeleteObject (hBrush) ;  
  98. }</span>  

以上程序用PeekMessage函数实现,在应用程序消息队列没有消息时,随机生成的矩形将坚持不懈地画下去。当有消息产生时,PeekMessage函数获取并派发消息出去,然后继续画随机生成的矩形。

下面部分代码用于替换WinMain函数中“用于替换的部分”的代码

  1. <span style="font-size:18px;">while (TRUE) 
  2.      { 
  3.           if (GetMessage (&msg, NULL, 0, 0)) 
  4.           { 
  5.                if (msg.message == WM_QUIT) 
  6.                     break ; 
  7.                TranslateMessage (&msg) ; 
  8.                DispatchMessage (&msg) ; 
  9.            DrawRectangle (hwnd) ; 
  10.           }  
  11.           else 
  12.                 break
  13.      }</span> 

  1. <span style="font-size:18px;">while (TRUE)  
  2.      {  
  3.           if (GetMessage (&msg, NULL, 0, 0))  
  4.           {  
  5.                if (msg.message == WM_QUIT)  
  6.                     break ;  
  7.                TranslateMessage (&msg) ;  
  8.                DispatchMessage (&msg) ;  
  9.            DrawRectangle (hwnd) ;  
  10.           }   
  11.           else  
  12.                 break;  
  13.      }</span>  

替换后,应用程序产生一个消息(鼠标移动、改变窗体大小等),在窗体内画一个随机生成的矩形,无消息产生时,窗体无变化。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值