2

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!

Community
  • 1
  • 1
Chris Bordeman
  • 255
  • 7
  • 19

1 Answers1

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.

WPF Options Page

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.

Dax Pandhi
  • 843
  • 4
  • 13
  • Sorry, your answer actually doesn't work at all. I looked closer at this code and you are doing little more than wrap the original answer I had implemented into a derived ElementHost class. It doesn't help with the mouse not changing issue. :( – Chris Bordeman Oct 11 '15 at 00:59
  • I'll see if I can make a new project with working code that you can use. – Dax Pandhi Oct 11 '15 at 07:37
  • @ChrisBordeman I've edited my answer with a working project. You should be able to take the files directly to your project and refactor them to fit your namespace. I've also added a screenshot to show the proper mouse behavior resulting from this wrapper. – Dax Pandhi Oct 11 '15 at 07:55
  • Ok, your sample works, so you get credit, thanks. However, all you're doing is deriving TextObfuscationDialog from `UIElementDialogPage` instead of the usual class. That's it. I really wish you hadn't wasted hours of my time pointing me to Github, where you did something completely different, reimplementing `UIElementDialogPage` entirely. That said, I tried deriving from the Shell UIElementDialogPage myself and didn't get it right because I think the Winforms generated code must have not used the new derivation. I used a plain source file, will let you know if it still doesn't work. Thx! – Chris Bordeman Oct 13 '15 at 00:10
  • I'm flabbergasted. It still doesn't work. I derive from UserControl, and on the Options page I derive from `UIElementDialogPage`. It's very simple, yet the cursor doesn't change when I mouse over a TextBox. ;( – Chris Bordeman Oct 13 '15 at 01:55
  • Chris, my code is the exact code from the Github link. Sorry you had to spend a lot of time there. I just stripped out all the unnecessary bits. If you post your code, I'd be happy to take a look at it. Btw, I'm assuming the WinForms usercontrol is no longer in the picture, correct? UIElementDialogPage won't host a WinForms control properly, if I read the docs right. – Dax Pandhi Oct 13 '15 at 05:59
  • The Github link had used a reimplementation of `UIElementElementDialogPage`. Anyway, I figured out my own custom textbox style was causing part of the issue. Simply using `UIElementElementDialogPage` as my base class solves all issues. Thanks again. – Chris Bordeman Oct 16 '15 at 02:45