我使用SetWindowsHookEx來監視另一個應用程序。當用戶在程序中單擊鼠標時,我使用PostMessage向程序發送消息。發佈消息在用戶單擊鼠標按鈕之後
我的問題是,有時我的郵件到達之前 WM_LBUTTONUP。如何確保我的郵件將在 WM_LBUTTONUP之後到達?
我用Gma.UserActivityMonitor監測和下面的代碼來發送消息:
HookManager.MouseUp += HookManagerMouseUp;
void HookManagerMouseUp(object sender, MouseEventArgs e)
{
uint ret;
uint attachedThredId = GetCurrentThreadId();
uint attachedProcessId = GetWindowThreadProcessId(ActiveWindow, out ret);
AttachThreadInputSafe(attachedThredId, attachedProcessId, true);
PostMessage(ActiveControlInWindow, Message.KeyDown, 'A', 0x1);
PostMessage(ActiveControlInWindow, Message.KeyUp, 'A', 0xC0010001);
AttachThreadInputSafe(attachedThredId, attachedProcessId, false);
}
我使用PostMessage的發送鍵擊,是因爲我想這樣做,即使主機應用程序是不活躍。
在Gma.UserActivityMonitor鉤如下:
s_MouseDelegate = MouseHookProc;
s_MouseHookHandle = SetWindowsHookEx(
WH_MOUSE_LL,
s_MouseDelegate,
Marshal.GetHINSTANCE(
Assembly.GetExecutingAssembly().GetModules()[0]),
0);
我使用間諜++監視到達給應用程序的消息。當信息以正確的順序到達我得到以下結果:
S WM_MOUSEACTIVATE hwndTopLevel:00030A46 nHittest:HTCLIENT uMsg:WM_LBUTTONDOWN
R WM_MOUSEACTIVATE fuActivate:MA_ACTIVATE
S WM_KILLFOCUS hwndGetFocus:00020A32
R WM_KILLFOCUS
S WM_IME_SETCONTEXT fSet:1 iShow:C000000F
R WM_IME_SETCONTEXT
S WM_SETFOCUS hwndLoseFocus:00030A46
R WM_SETFOCUS
P WM_LBUTTONDOWN fwKeys:MK_LBUTTON xPos:278 yPos:73
S WM_ERASEBKGND hdc:0E010DEE
R WM_ERASEBKGND fErased:True
P WM_LBUTTONUP fwKeys:0000 xPos:278 yPos:73
S WM_CAPTURECHANGED hwndNewCapture:00000000
R WM_CAPTURECHANGED
P WM_KEYDOWN nVirtKey:VK_OEM_5 cRepeat:1 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
P WM_KEYUP nVirtKey:VK_OEM_5 cRepeat:1 ScanCode:01 fExtended:0 fAltDown:0 fRepeat:1 fUp:1
P WM_CHAR chCharCode:'167' (167) cRepeat:1 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
P WM_KEYDOWN nVirtKey:'2' cRepeat:1 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
P WM_KEYUP nVirtKey:'2' cRepeat:1 ScanCode:01 fExtended:0 fAltDown:0 fRepeat:1 fUp:1
P WM_KEYDOWN nVirtKey:'2' cRepeat:1 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
P WM_KEYUP nVirtKey:'2' cRepeat:1 ScanCode:01 fExtended:0 fAltDown:0 fRepeat:1 fUp:1
P WM_CHAR chCharCode:'50' (50) cRepeat:1 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
P WM_CHAR chCharCode:'50' (50) cRepeat:1 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
S WM_KILLFOCUS hwndGetFocus:(null)
R WM_KILLFOCUS
S WM_IME_SETCONTEXT fSet:0 iShow:C000000F
R WM_IME_SETCONTEXT
當消息順序錯誤我得到下面的結果到達(請注意,WM_LBUTTONUP到達WM_CHAR已處理後):
S WM_MOUSEACTIVATE hwndTopLevel:00030A46 nHittest:HTCLIENT uMsg:WM_LBUTTONDOWN
R WM_MOUSEACTIVATE fuActivate:MA_ACTIVATE
S WM_KILLFOCUS hwndGetFocus:00020A32
R WM_KILLFOCUS
S WM_IME_SETCONTEXT fSet:1 iShow:C000000F
R WM_IME_SETCONTEXT
S WM_SETFOCUS hwndLoseFocus:00030A46
R WM_SETFOCUS
P WM_LBUTTONDOWN fwKeys:MK_LBUTTON xPos:329 yPos:166
S WM_ERASEBKGND hdc:1E0117FD
R WM_ERASEBKGND fErased:True
P WM_KEYDOWN nVirtKey:VK_OEM_5 cRepeat:1 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
P WM_KEYUP nVirtKey:VK_OEM_5 cRepeat:1 ScanCode:01 fExtended:0 fAltDown:0 fRepeat:1 fUp:1
P WM_KEYDOWN nVirtKey:'2' cRepeat:1 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
P WM_KEYUP nVirtKey:'2' cRepeat:1 ScanCode:01 fExtended:0 fAltDown:0 fRepeat:1 fUp:1
P WM_KEYDOWN nVirtKey:'2' cRepeat:1 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
P WM_KEYUP nVirtKey:'2' cRepeat:1 ScanCode:01 fExtended:0 fAltDown:0 fRepeat:1 fUp:1
P WM_CHAR chCharCode:'167' (167) cRepeat:1 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
P WM_CHAR chCharCode:'50' (50) cRepeat:1 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
P WM_CHAR chCharCode:'50' (50) cRepeat:1 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
P WM_LBUTTONUP fwKeys:0000 xPos:329 yPos:166
S WM_CAPTURECHANGED hwndNewCapture:00000000
R WM_CAPTURECHANGED
S WM_KILLFOCUS hwndGetFocus:(null)
R WM_KILLFOCUS
S WM_IME_SETCONTEXT fSet:0 iShow:C000000F
R WM_IME_SETCONTEXT
我可以看到它是隨機的,當一個或另一個發生。如果我在發佈消息之前添加延遲,它會起作用。但我不想使用延遲。
你爲什麼不在這裏發佈相關代碼,以便我們看到你是如何做到的? – Nawaz 2010-12-14 08:52:54
@Nawaz我已經添加了我想要做的簡化代碼 – magol 2010-12-14 09:17:03