ElementHost blocks mouse events

非 Y 不嫁゛ 提交于 2019-12-07 22:46:29

问题


NOTE: I AM TRYING TO SOLVE THE MOUSE ISSUE, NOT THE KEYBOARD PROBLEM, WHICH IS ALREADY SOLVED

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

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 TextBox not accepting Input when in ElementHost in Window Forms

A quick run down of the solution:

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

var s = HwndSource.FromVisual(this) as HwndSource;
s?.AddHook(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;
}

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.

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!


回答1:


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.

EDIT:

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.

You can download the source code from my OneDrive here.

The UIElementDialogPage on MSDN says:

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.

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.



来源:https://stackoverflow.com/questions/33026380/elementhost-blocks-mouse-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!