【问题标题】:Visual Studio Extension Mouse Button events not workingVisual Studio 扩展鼠标按钮事件不起作用
【发布时间】:2020-06-11 23:35:24
【问题描述】:

我有一个带有(异步)工具窗口的 Visual Studio 扩展。工具窗口有一个UserControl,控件有一个Image,我使用WriteableBitmap 作为源。基本上,我正在尝试在工具窗口上使用图形 API (Vulkan)。

基本上,我的扩展看起来类似于 Visual Studio 的 3d 模型编辑器。

我的问题是,当我单击工具窗口时,不会调用像 MouseLeftButtonDownPreprocessMouseLeftButtonDown 这样的鼠标事件处理程序。我曾尝试向UserControlImage 甚至HwndHost 添加鼠标事件处理程序,但没有成功。

我知道IMouseProcessor 或其他变体,但在我看来,它们用于文本编辑器,而不仅仅是工具窗口窗格或用户控件。

HwndHostWndProc 方法似乎也没有收到任何鼠标事件。简单地通过替换父 Hwnd 的 WndProc 来“取悦”它似乎可行,但我不想创建一个控制整个 Visual Studio 的扩展。

== 编辑 ==

public partial class MyControl : UserControl {
    public MyControl () {
        InitializeComponents ();
        MouseLeftButtonDown += MyLeftButtonDown;
    }
    ...
    private void MyLeftButtonDown (object sender, MouseButtonEventArgs e) {
        // This code never gets executed.
    }
}

这些也不是。

<UserControl ...
             IsHitTestVisible="True"
             MouseLeftButtonDown="MyLeftButtonDown ">
    <Image x:Name="View" ... />
    <!--- nor this --->
    <!--- <Image x:Name="View" MouseLeftButtonDown="MyLeftButtonDown " /> --->
</UserControl>

这也行不通。

public partial class MyControl : UserControl {
    public MyControl () {
        InitializeComponents ();
        // View is an object of Image class.
        View.MouseLeftButtonDown += MyLeftButtonDown;
    }
    ...
    private void MyLeftButtonDown (object sender, MouseButtonEventArgs e) {
        // This code never gets executed.
    }
}
// C++/CLI
IntPtr MyHost::WndProc (IntPtr hWndPtr, int uMsg, IntPtr wParamPtr, IntPtr lParamPtr, bool % handled)
{
    auto hWnd = (HWND) hWndPtr.ToPointer ();
    auto wParam = (WPARAM) wParamPtr.ToPointer ();
    auto lParam = (LPARAM) lParamPtr.ToPointer ();

    switch (uMsg)
    {
        case WM_LBUTTONDOWN: // This doesn't get called.
            break;
        ...
        default:
            handled = false;
            return IntPtr (DefWindowProc (hWnd, uMsg, wParam, lParam));
    }
    ...
    handled = true;
    return IntPtr (0);
}

【问题讨论】:

标签: c# wpf visual-studio-2019 visual-studio-extensions vsix


【解决方案1】:

我还没有弄清楚如何使用ImageUserControl 处理鼠标输入,但我已经为HwndHost 弄清楚了。

显然,如果您的HwndHost 没有回复WM_NCHITTEST,那么您的HwndHost 将永远不会收到像WM_LBUTTONDOWN 这样的鼠标事件。

// C++/CLI
IntPtr MyHost::WndProc (IntPtr hWndPtr, int uMsg, IntPtr wParamPtr, IntPtr lParamPtr, bool % handled)
{
    auto hWnd = (HWND) hWndPtr.ToPointer ();
    auto wParam = (WPARAM) wParamPtr.ToPointer ();
    auto lParam = (LPARAM) lParamPtr.ToPointer ();

    switch (uMsg)
    {
        case WM_NCHITTEST: // This message.
            SetFocus (hWnd);
            handled = true;
            return IntPtr (HTCLIENT);
        case WM_LBUTTONDOWN: // This works now.
            break;
        ...
        default:
            handled = false;
            return IntPtr (DefWindowProc (hWnd, uMsg, wParam, lParam));
    }
    ...
    handled = true;
    return IntPtr (0);
}

然后,您可以放置​​一些委托或事件并调用它们以获取WndProc 消息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多