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!

4

1 に答える 1

1

作成者のヴィアスフォラが使用した方法を使用して成功を収めました。こちらの GitHub リポジトリでコードを確認できます。特に、TextObfuscationDialog とそのホスト方法に注目してください。あなたと同じ問題に遭遇したので、VS 拡張機能のドキュメントに何か問題があると思います。

編集:

このメソッドが機能することを示すサンプル プロジェクトを作成しました (現在、2 つの独自の拡張機能で使用しています)。うまくいけば、この作業コードにより、独自のプロジェクトで簡単に実装できるようになります。

ソース コードは、こちらの OneDriveからダウンロードできます。

WPF オプション ページ

UIElementDialogPage MSDN には次のように書かれています。

IsDialogMessage スタイルのメッセージ ループを実行するネイティブ ダイアログ内で、Windows Presentation Foundation (WPF) コンテンツのシームレスなホスティングを提供します。このクラスは、WPF 子ウィンドウ ハンドル (HWND) へのタブ移動と、WPF 子ウィンドウ ハンドル (HWND) からのタブ移動を有効にし、WPF 子 HWND 内のキーボード ナビゲーションを有効にします。

したがって、ElementHostwill は normal/WinForms のメッセージ ループ内では正しく機能しませんがDialogPageUIElementDialogPagewill. または同様のプレフィックスを持つクラスがいくつかありUIElement*ます。これらは、VS のレガシ コードを Windows フォームから WPF に移行するのに役立ちます。

于 2015-10-09T05:09:14.133 に答える