无法通过钩子阻止menuItem上的单击事件 [英] Can not prevent click event on menuItem by hook

查看:80
本文介绍了无法通过钩子阻止menuItem上的单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在为程序捕获控件编码,以获取该控件的信息;
我的程序将在光标位置下绘制突出显示控件,我将获得该控件的信息
当我点击它.所以:

1.当用户单击另一个程序时,使用钩子防止Windows的单击事件".

2.按住Ctrl键时,该挂钩将允许单击事件"发送到窗口.

我的问题:

1.我按住Ctrl键单击 Wpf应用程序的菜单,以打开菜单项的列表.

2.释放Ctrl键,然后单击menuitem.

=>当时,尽管该钩子阻止了单击事件",因此它不会发送到Window,但是单击事件"仍然会影响菜单项.

我的钩子代码:

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;
}


使用此代码,它在window或Win32 App上运行良好,但WPF却不行.
为什么该钩子无法阻止WPF App中menuItem上的click事件?以及该问题的解决方案是什么?
在这方面的任何帮助将不胜感激

谢谢和问候,

Sugia


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

推荐答案

听起来您正在尝试模仿Spy ++或WPF等效项Snoop的行为.

Snoop是开源的,可在CodePlex上使用.
为什么不下载源代码并偷看一些想法?

探听: http://snoopwpf.codeplex.com/ [ ^ ]
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/[^]


没有什么可以阻止单击的操作,除了将鼠标移开是因为这是硬件事件. :-)
而且,据我所知,您无法使用Windows Hook阻止事件(可能是因为这很不安全).有些事件甚至无法检测到,因为它们是在操作系统级别以特殊方式处理的.特别是,据我所知,您无法通过用户模式环中的任何代码检测到Ctrl + Alt + Del.

现在,太糟糕了,您没有解释您的最终目标.问问题时,您应该总是在解释.因此,我不确定,但是有件事告诉我可能需要信息亭模式".请参阅:
使用C#以Kiosk模式运行网站 [ c#程序ctrl-alt-del屏幕窗口7 [ ^ ].

—SA
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


这篇关于无法通过钩子阻止menuItem上的单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆