ElementHost阻止鼠标事件 [英] ElementHost blocks mouse events

查看:193
本文介绍了ElementHost阻止鼠标事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:我正在尝试解决鼠标问题,而不是已经解决的键盘问题

所以我要创建一个Visual Studio 2015扩展,在选项"页面上工作.

So I am creating a Visual Studio 2015 extension, working on the Options pages.

我正在使用WPF,因此我使用ElementHost托管UserControl.起初它没有收到键盘事件,所以我在以下位置实现了解决方案:

I am using WPF, so I use ElementHost to host a UserControl. At first it wasn't receiving keyboard events, so I implemented the solution at:

WPF文本框在输入时不接受输入窗口形式的ElementHost

快速查看解决方案:

A)在UserControl的Loaded事件上,我这样做:

A) on the UserControl's Loaded event, I do:

var s = HwndSource.FromVisual(this) as HwndSource;
s?.AddHook(ChildHwndSourceHook);

B)在ChildHwndSourceHook()中,我做类似的事情:

B) In ChildHwndSourceHook(), I do something like:

static IntPtr ChildHwndSourceHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
    if (msg == WM_GETDLGCODE)
    {
        handled = true;
        return new IntPtr(DLGC_WANTCHARS | DLGC_WANTARROWS | DLGC_HASSETSEL | DLGC_WANTTAB);
    }
    return IntPtr.Zero;
}

但是,现在鼠标悬停事件似乎被忽略了,因为在文本框或网格拆分器上移动光标时,光标不会发生变化,即使在我创建的新Windows上也是如此.但是,偶尔会,鼠标事件确实起作用,并且一直起作用,直到我移至另一页或关闭对话框为止.那是最奇怪的部分.

HOWEVER, now mouse over events seem to be being ignored, as the cursor doesn't change when moving it over textboxes or grid splitters, not even on new Windows I create. Very occasionally, though, the mouse events do work, though, and continue to work until I move to another page or close the dialog. That's the weirdest part.

我已经尝试了一切,并整日搜寻了Google,但是我还无法理解为什么点击效果很好,但是鼠标悬停事件似乎没有被记录.

I've tried everything and have scoured Google all day, but I am no closer to understanding why clicks work fine, but mouse over events don't seem to be registered.

我确实尝试过删除消息处理程序,然后打开一个窗口,但是似乎一旦添加了处理程序,将其删除将无法解决任何问题.

I did try REMOVING the message handler, then opening a Window, but it seems once the handler is added, removing it won't fix anything.

有人知道我如何将鼠标悬停在事件上以在控件上工作吗?非常感谢!

Does anyone know how I can get mouse over events to work on my controls? Thanks so much!

推荐答案

我已经使用创建者Viasfora使用的方法找到了成功.您可以在 GitHub存储库此处上查看代码.特别是,请查看TextObfuscationDialog及其托管方式.我认为VS扩展文档可能有问题,因为我遇到了与您相同的问题.

I've found success using the method the creator Viasfora used. You can see the code on the GitHub repo here. In particular, look at the TextObfuscationDialog and how it is hosted. I believe there might be something wrong with the VS extension documentation, because I ran into the same problem you did.

我已经创建了一个示例项目来演示此方法是否有效(我现在在自己的2个扩展中使用它).希望此工作代码可以使您更轻松地在自己的项目中实现它.

I've created a sample project to show this method does work (I'm using it in 2 of my own extensions right now). Hopefully this working code might make it easier for you to implement it in your own project.

您可以从我的

UIElementDialogPage

提供Windows Presentation Foundation(WPF)的无缝托管 运行IsDialogMessage样式的本机对话框中的内容 消息循环.此类允许切换到WPF子项和从中跳出 窗口句柄(HWND),并在WPF中启用键盘导航 子级HWND.

Provides seamless hosting of Windows Presentation Foundation (WPF) content inside a native dialog running an IsDialogMessage-style message loop. This class enables tabbing into and out of the WPF child window handle (HWND), and enables keyboard navigation within the WPF child HWND.

因此,尽管ElementHost在正常/WinForms DialogPage的消息循环内无法正常运行,但UIElementDialogPage可以.有许多具有UIElement*或类似前缀的类-它们是用来帮助将VS的旧代码从Windows窗体迁移到WPF.

So while an ElementHost will not function correctly within the message loop of a normal/WinForms DialogPage, the UIElementDialogPage will. There are a number of classes that have UIElement* or similar prefix - they are to help migrate the legacy code of VS from Windows Forms to WPF.

这篇关于ElementHost阻止鼠标事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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