2017-12-14 36 views
1

私はコードの下でマウスクリックイベントにマウスフックを作成しています、特定のアプリケーションをクリック:

mousehook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProc, NULL, 0); 

それは、マウスクリックでMouseHookProc関数を呼び出すことができます。

しかし、他のアプリケーションをクリックしても、普通のデスクトップ画面でもこのMouseHookProc関数が呼び出されます。 このフックイベントを現在のアプリケーションのみに制限するにはどうすればよいですか?

答えて

0

まず、アプリケーション内でマウスイベントだけを探しているのであれば、フックの代わりにメインメッセージポンプを使用するといいでしょう。

しかし、低レベルのマウスフックを使用して、これは他のアプリでたときに邪魔にアプリで働いて処理していないでしょう

MSLLHOOKSTRUCT *hookStruct = (MSLLHOOKSTRUCT*)lParam; 
// hMyMainAppHWND in the line below would already be defined 
// and set when your program starts and gets its handle 
if(GetAncestor(WindowFromPoint(hookStruct->pt),GA_ROOTOWNER) != hMyMainAppHWND){ 
    // if the owner of the window where the mouse event occurred 
    // isn't your application's owning window, pass the event on 
    return CallNextHookEx(mousehook, nCode, wParam, lParam); 
} else { 
    // The event occurred on your application 
    // Do your stuff here 
    // Don't forget to do one of the following: 
    // if you want to consume the event, making it as though it never happened: 
    // return TRUE; 
    // if you want the event to be processed as normal: 
    // return CallNextHookEx(mousehook, nCode, wParam, lParam); 
}