Can not prevent click event on menuItem of WPF App by hook

Answered Can not prevent click event on menuItem of WPF App by hook

  • Friday, August 03, 2012 6:17 PM
     
      Has Code

    Hi All,

    I'm coding for program capture control to get information of that control; 
    My program will will get infomation of control under cursor position when i click on it. So I:

    1. Using hook to prevent 'click event' to windows when user click on any windows or another program.

    2. when press and hold Ctrl key, the hook will allow 'click event' is sent to window.

    My problem:

    1. I press and hold Ctrl key to click on menu of a WPF Application to open list of menuitem.

    2. Release Ctrl key and click on menuitem.

    =>at that time, althought the hook prevented 'click event' so that it is not sent to Window, but 'click event' still affect on menuitem.

    Code of my hook:

    /*
    * hook function for mouse
    */
    static LRESULT CALLBACK InternalMouseHookCallback(int code, WPARAM wparam, LPARAM lparam)
    {
    	if (code < 0)
    	{
    		return CallNextHookEx(hookMouse, code, wparam, lparam);
    	}
    		
    	bool bForwardMessage = true;
    	if(code==HC_ACTION) {
    		
    		MOUSEHOOKSTRUCT* pStruct = (MOUSEHOOKSTRUCT*)lparam;
    		switch(wparam) {
    		case WM_LBUTTONDOWN: 
    		case WM_LBUTTONUP:
    				
    			if (gCtrlPressed == false){
    				bForwardMessage = false;
    			}
    			break;		
    		default:
    			break;
    		}
    	}
    	
    	if (bForwardMessage){		
    		return CallNextHookEx(hookMouse, code, wparam, lparam);
    	}
    
    	return 1;
    }
    
    /*
    * hook function for keyboard
    */
    static LRESULT CALLBACK InternalKeyboardHookCallback(int code, WPARAM wParam, LPARAM lParam)
    {
    	if (code < 0){
    		return CallNextHookEx(hookKeyboard, code, wParam, lParam);
    	}
    
    	bool bForwardMessage = true;
    	
    	KBDLLHOOKSTRUCT* p = (KBDLLHOOKSTRUCT *)lParam;	
    	switch(wParam)  {
    		case WM_KEYDOWN:
    		case WM_SYSKEYDOWN:
    			if(p->vkCode==VK_LCONTROL || p->vkCode==VK_RCONTROL)
    			{					
    				gCtrlPressed = true;				
    			}
    			break;
    		default:
    			break;
    	}
    	if (bForwardMessage){
    		return CallNextHookEx(NULL,code,wParam,lParam);
    	}
    	return -1;
    }

    With this code, It operate good on Win32 App, but WPF is not.

    Why the hook can not prevent click event on menuItem in WPF App? and what solution for this issue?

    Any help in this matter would be greatly appreciated

    Thanks and regards,

    Sugia


    Sugia279 Don't try don't future






    • Edited by sugia279 Friday, August 03, 2012 6:23 PM
    • Edited by sugia279 Friday, August 03, 2012 6:45 PM Update infomation
    • Edited by sugia279 Friday, August 03, 2012 6:50 PM
    • Edited by sugia279 Friday, August 03, 2012 6:53 PM title change
    • Edited by sugia279 Saturday, August 04, 2012 1:24 AM
    • Moved by CoolDadTxMVP Sunday, August 05, 2012 3:19 AM WPF related (From:Visual C# General)
    •  

All Replies

  • Friday, August 03, 2012 6:44 PM
     
     

    The click may be being dispatched in a different window message. I'd suggest logging them all and then going over the codes to find any other mouse events, such as mouse up, mouse down, key, and stuff like that. Do the logging in your 'default' cases.

    http://l.autohotkey.net/docs/misc/SendMessageList.htm

  • Friday, August 03, 2012 6:48 PM
     
     

    Hi Barrakoda,

    Thanks for your reading and your suggest.

    I have just update my question so Please try read it again!.

    Thanks

    sugia.


    Sugia279 Don't try don't future

  • Saturday, August 04, 2012 1:29 AM
     
      Has Code

    Hi All,

    I'm coding for program capture control to get information of that control; 
    My program will will get infomation of control under cursor position when i click on it. So I:

    1. Using hook to prevent 'click event' to windows when user click on any windows or another program.

    2. when press and hold Ctrl key, the hook will allow 'click event' is sent to window.

    My problem:

    1. I press and hold Ctrl key to click on menu of a WPF Application to open list of menuitem.

    2. Release Ctrl key and click on menuitem.

    =>at that time, althought the hook prevented 'click event' so that it is not sent to Window, but 'click event' still affect on menuitem.

    Code of my hook:

    /*
    * hook function for mouse
    */
    static LRESULT CALLBACK InternalMouseHookCallback(int code, WPARAM wparam, LPARAM lparam)
    {
    	if (code < 0)
    	{
    		return CallNextHookEx(hookMouse, code, wparam, lparam);
    	}
    		
    	bool bForwardMessage = true;
    	if(code==HC_ACTION) {
    		
    		MOUSEHOOKSTRUCT* pStruct = (MOUSEHOOKSTRUCT*)lparam;
    		switch(wparam) {
    		case WM_LBUTTONDOWN: 
    		case WM_LBUTTONUP:
    				
    			if (gCtrlPressed == false){
    				bForwardMessage = false;
    			}
    			break;		
    		default:
    			break;
    		}
    	}
    	
    	if (bForwardMessage){		
    		return CallNextHookEx(hookMouse, code, wparam, lparam);
    	}
    
    	return 1;
    }
    
    /*
    * hook function for keyboard
    */
    static LRESULT CALLBACK InternalKeyboardHookCallback(int code, WPARAM wParam, LPARAM lParam)
    {
    	if (code < 0){
    		return CallNextHookEx(hookKeyboard, code, wParam, lParam);
    	}
    
    	bool bForwardMessage = true;
    	
    	KBDLLHOOKSTRUCT* p = (KBDLLHOOKSTRUCT *)lParam;	
    	switch(wParam)  {
    		case WM_KEYDOWN:
    		case WM_SYSKEYDOWN:
    			if(p->vkCode==VK_LCONTROL || p->vkCode==VK_RCONTROL)
    			{					
    				gCtrlPressed = true;				
    			}
    			break;
    		default:
    			break;
    	}
    	if (bForwardMessage){
    		return CallNextHookEx(NULL,code,wParam,lParam);
    	}
    	return -1;
    }

    With this code, It operate good on Win32 App, but WPF is not.

    Why the hook can not prevent click event on menuItem in WPF App? and what solution for this issue?

    Any help in this matter would be greatly appreciated

    Thanks and regards,

    Sugia


    Sugia279 Don't try don't future







    Sugia279 Don't try don't future

  • Saturday, August 04, 2012 1:31 AM
     
      Has Code

    Hi All,

    I'm coding for program capture control to get information of that control; 
    My program will will get infomation of control under cursor position when i click on it. So I:

    1. Using hook to prevent 'click event' to windows when user click on any windows or another program.

    2. when press and hold Ctrl key, the hook will allow 'click event' is sent to window.

    My problem:

    1. I press and hold Ctrl key to click on menu of a WPF Application to open list of menuitem.

    2. Release Ctrl key and click on menuitem.

    =>at that time, althought the hook prevented 'click event' so that it is not sent to Window, but 'click event' still affect on menuitem.

    Code of my hook:

    /*
    * hook function for mouse
    */
    static LRESULT CALLBACK InternalMouseHookCallback(int code, WPARAM wparam, LPARAM lparam)
    {
    	if (code < 0)
    	{
    		return CallNextHookEx(hookMouse, code, wparam, lparam);
    	}
    		
    	bool bForwardMessage = true;
    	if(code==HC_ACTION) {
    		
    		MOUSEHOOKSTRUCT* pStruct = (MOUSEHOOKSTRUCT*)lparam;
    		switch(wparam) {
    		case WM_LBUTTONDOWN: 
    		case WM_LBUTTONUP:
    				
    			if (gCtrlPressed == false){
    				bForwardMessage = false;
    			}
    			break;		
    		default:
    			break;
    		}
    	}
    	
    	if (bForwardMessage){		
    		return CallNextHookEx(hookMouse, code, wparam, lparam);
    	}
    
    	return 1;
    }
    
    /*
    * hook function for keyboard
    */
    static LRESULT CALLBACK InternalKeyboardHookCallback(int code, WPARAM wParam, LPARAM lParam)
    {
    	if (code < 0){
    		return CallNextHookEx(hookKeyboard, code, wParam, lParam);
    	}
    
    	bool bForwardMessage = true;
    	
    	KBDLLHOOKSTRUCT* p = (KBDLLHOOKSTRUCT *)lParam;	
    	switch(wParam)  {
    		case WM_KEYDOWN:
    		case WM_SYSKEYDOWN:
    			if(p->vkCode==VK_LCONTROL || p->vkCode==VK_RCONTROL)
    			{					
    				gCtrlPressed = true;				
    			}
    			break;
    		default:
    			break;
    	}
    	if (bForwardMessage){
    		return CallNextHookEx(NULL,code,wParam,lParam);
    	}
    	return -1;
    }

    With this code, It operate good on Win32 App, but WPF is not.

    Why the hook can not prevent click event on menuItem in WPF App? and what solution for this issue?

    Any help in this matter would be greatly appreciated

    Thanks and regards,

    Sugia


    Sugia279 Don't try don't future








    Sugia279 Don't try don't future

  • Saturday, August 04, 2012 9:10 PM
     
     Answered

    I cannot help so much. But what you have to know is that if WPF windows are graphically identical to Win32 windows, controls inside a window or popup aren't child Windows themselves. The message dispatching in WPF is very different from the one found in Win32.

    Windows have a global system message queue and per interactive thread message queues for Win32. I cannot say if Win32 hook program can hook WPF program and if WPF use the same per interactive thread structure than Win32. The message passing class that presents this mechanism to WPF applications is System.Windows.Threading.Dispatcher.

    You can probably take a look at the excellent Snoop (Spy++ equivalent for WPF programs) source code at http://snoopwpf.codeplex.com/ and you will probably find all what you need to monitor WPF applications.

  • Thursday, August 09, 2012 6:34 AM
    Moderator
     
     Answered

    Hi sugia279,

    What RBancel said is correct. And here is a workaround for disable click event is that to handle mouse event at the top level of WPF.

    Similar issue for your reference:http://www.codeproject.com/Questions/161515/windows-hook-in-WPF-application

    Hope it helps.

    Have a nice day.


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us

  • Thursday, August 09, 2012 9:23 AM
    Moderator
     
     
    Additional, this link may help also:http://stackoverflow.com/questions/5036143/what-does-wpf-use-to-capture-mouse-and-keyboard-input

    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us

  • Monday, November 05, 2012 4:56 PM
     
     

    I have done it. I thick It helps you!

    static bool flagctrl=false;

     myCtrlHookProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
        if (nCode < 0)
            return CallNextHookEx(hHook, nCode, wParam, lParam);

        KBDLLHOOKSTRUCT* p = (KBDLLHOOKSTRUCT *)lParam;    
        switch(wParam)  {
            case WM_KEYDOWN:
            case WM_SYSKEYDOWN:
                if(p->vkCode==VK_LCONTROL || p->vkCode==VK_RCONTROL)
                    {
                        flagctrl=true;
                }
                else
                flagctrl=false;
                break;
            case WM_KEYUP:
            case WM_SYSKEYUP:
                flagctrl=false;
                break;
        }
        return CallNextHookEx(hHook, nCode, wParam, lParam);
    }

    LRESULT CALLBACK myMouseHookProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
        if (nCode < 0)
            return CallNextHookEx(hHook, nCode, wParam, lParam);
        MSLLHOOKSTRUCT *structMouse = (MSLLHOOKSTRUCT*)lParam;

        if (wParam==WM_LBUTTONUP && flagctrl)
        {
            // event Ctrl+ Mouse left click;
        }

        return CallNextHookEx(hHook, nCode, wParam, lParam);

    }