Click here to Skip to main content
14,739,403 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi All,

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

1. Using hook to prevent 'click event' to windows when user click on 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 window or 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 appreciated

Thanks and regards,

Sugia
Posted
Updated 3-Aug-12 9:52am
v4
Comments
   
Would you tell use why doing so?
--SA

It sounds like you're trying to emulate the behaviour of Spy++ or it's WPF equivalent, Snoop.

Snoop is opensource and available on CodePlex.
Why don't you download the source and have a peek for some ideas?

Snoop: http://snoopwpf.codeplex.com/[^]
   
Comments
   
Even though we still don't know OP's goals, it looks like this product is just good to know.
Thank you for sharing, my 5.
--SA
xenotron 3-Aug-12 15:55pm
   
+5 useful info!
LOL, the logo of snoop is like a horse smelling a big bunch of excrement! :-D
sugia 3-Aug-12 21:41pm
   
Yes, you're wright.
I'm trying to emulate the behaviour of Spy++ on WPF.
I downloaded Snoop but I'm still not find solution for my problem.
Please help me to research other solution for my problem
Thanks and regard,
There is nothing which could prevent a click, except removing the mouse, because this is a hardware event. :-)
And, from what I know, you cannot prevent an event using a Windows Hook (perhaps because it would be very unsafe). There are certain events which you cannot even detect, because they are handled on the OS level in a special way. In particular, to best of my knowledge, you cannot detect Ctrl+Alt+Del by any code in a user mode ring.

Now, too bad you did not explain your ultimate goal. You should always explain in when asking questions. So, I cannot be quite sure, but something tells me that you might need a "kiosk mode". Please see:
Running a Web Site in Kiosk Mode with C#[^],
c# program ctrl-alt-del screen windows 7[^].

—SA
   
v2
Comments
Kenneth Haugland 3-Aug-12 14:51pm
   
From OP:
Hi Sergey,
Thanks for your advice,
I have just update my question.
Please try to read again.
Thanks
sugia

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900