2009-07-15 93 views
3

我想傻傻調用是否有像PeekMessage這樣的函數不處理消息?

PeekMessage(&msg, 0, WM_KEYDOWN, WM_KEYUP, PM_NOREMOVE | PM_NOYIELD); 

和Windows Vista 64,在通話的PeekMessage,是處理的消息。結果是,我將重新進入我的繪畫調用和其他各種代碼。

繪畫在我們的應用程序中可能需要幾秒鐘,所以我們添加了PeekMessage調用以查看用戶是否擊中了一個鍵,因此我們可以中斷該繪畫並啓動下一個。我們沒有意識到Windows可以開始處理我們的消息。將繪畫的真實工作放在一個單獨的線程中是一個重大的重構......我們試圖查看是否按下了特定的按鍵,或者是否單擊了鼠標滾輪或鼠標按鈕來中斷呈現。

我試過專門添加代碼以防止重新入侵,然後將隊列中的油漆消息重新注入等。這些都非常糟糕,並且有些情況下它不能正常工作。

是否有一些標誌可以添加到PeekMessage調用中?在MSDN上的文檔中我沒有看到任何新東西。我真的需要一個不處理消息的PeekMessage。幫幫我!

回答

5

也許我缺少明顯的,但spec is pretty verbose會做這樣

的函數的PeekMessage派遣傳入 發送的郵件,檢查了貼 的 線程消息隊列消息,並檢索該消息(如果有 任何存在)。

...

在通話過程中,系統提供 之前,非排隊的消息,也就是說,發送到由使用SendMessage, SendMessageCallback的 調用線程擁有的窗口 消息, SendMessageTimeout或 SendNotifyMessage函數。然後檢索與指定的過濾器匹配的第一個排隊消息 。 系統也可以處理內部 事件。如果沒有指定過濾器, 消息在 處理順序如下:

  • 已發送郵件
  • 發佈消息
  • 輸入(硬件)消息和系統內部事件
  • 已發送郵件(再次)
  • WM_PAINT消息
  • WM_TIMER消息

要在 發佈消息之前檢索輸入消息,請使用wMsgFilterMin 和wMsgFilterMax參數。

+0

同意。我沒有寫這個代碼。不幸的是,我不相信有任何函數調用會做我所需要的 - 確定如果按鍵或鼠標事件在隊列中,而不處理任何消息。 – 2009-07-15 20:00:45

2

我認爲這是PeekMessage應該做的。它和GetMessage唯一的區別在於,GetMessage會阻塞,直到消息到達,PeekMessage將根據是否找到匹配過濾器的消息返回TRUE或FALSE。如果它們被發現,它仍然會處理這些消息。

0

PeekMessage處理消息,因爲這是PeekMessage的功能。

也許它的命名不正確,但如果有任何可用的消息,PeekMessage會從隊列中刪除消息。

1

GetQueueStatus是檢查是否有可用消息的最快方法。與peekmessage的5個參數相比,它只會檢查幾個標誌並僅佔用1個參數。如果有可用消息,它將提供一個快速提示,它不會以任何方式處理消息。

GetQueueStatus和GetInputStatus是相關的函數。

-1
Just modified the PM_REMOVE flag for the PM_NOREMOVE 





using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace PROJECT_NAME 
{ 
    class cUtil 
    { 
     //=============================== 
     cUtil() 
     { 
     } 
     //================================ 
     ~cUtil() 
     { 
     } 
     //================================= 
     public struct Message 
     { 
      public IntPtr handle; 
      public uint msg; 
      public IntPtr wParam; 
      public IntPtr lParam; 
      public uint time; 
      public System.Drawing.Point p; 
     } 

     [System.Runtime.InteropServices.DllImport("user32.dll")] 
     private static extern bool PeekMessage(out Message lpMsg, Int32 hwnd, Int32 wMsgFilterMin, Int32 wMsgFilterMax, uint wRemoveMsg); 
     [System.Runtime.InteropServices.DllImport("user32.dll")] 
     private static extern bool TranslateMessage(out Message lpMsg); //(ref Message lpMsg); 
     [System.Runtime.InteropServices.DllImport("user32.dll")] 
     private static extern Int32 DispatchMessage(out Message lpMsg); //(ref Message lpMsg); 

     //private static uint PM_NOREMOVE = 0x0000; 
     private static uint PM_REMOVE = 0x0001; 
     //private static uint PM_NOYIELD = 0x0002; 
     public static void Peek() 
     { 
      Message winMsg; 
      while (PeekMessage(out winMsg, (Int32)0, (Int32)0, (Int32)0, PM_REMOVE)) 
      { 
       TranslateMessage(out winMsg); 
       DispatchMessage(out winMsg); 
      } 

     } 
    } 
} 


//================================ 
//================================ 
//=============================== 

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace PROJECT_NAME 
{ 
    public partial class foNAME : Form 
    { 
     //=================================== 
     public foRAMQ() 
     { 
      InitializeComponent(); 
     } 
     //=================================== 
     private void Job() 
     { 
      int cnt = 0; 

      while(reading_DBMS()) 
      { 
       cUtil.Peek(); 

       . 
       . 
       . 
       . 
       . 
       cnt++; 
       lable_count.Text = string.Format("Count: {0}", cnt)    
      } 
    } 


    } 
} 
相關問題