在 C# WPF 中实现 HwndHost 以处理 MouseMove 和 MouseWheel

Implement HwndHost to handle MouseMove and MouseWheel in C# WPF

我正在尝试在 WPF 应用程序中托管 DirectX 内容。我从这个 link 中学到的是子类化 HwndHost 并创建您自己的 WndProc 实现以及 BuildWindowCore 和 DestroyWindowCore。 https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/walkthrough-hosting-a-win32-control-in-wpf#implement-a-class-to-host-the-microsoft-win32-control

然后我在这里发现了非常有用的实现: https://github.com/Smartrak/WpfSharpDxControl/blob/master/WpfSharpDxControl/Win32HwndControl.cs

现在,我需要再实现两个方法,例如WM_MOUSEMOVE (0x0200) 和WM_MOUSEWHEEL (0x020A)。 我不确定如何正确地传递鼠标滚轮增量和时间戳等信息。 任何指向这个方向的指示都会很有帮助。

protected override IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        switch (msg)
        {
            case NativeMethods.WM_MOUSEMOVE:
                //What should go here
                break;
            case NativeMethods.WM_MOUSEWHEEL:
                //What should go here
                break;
            case NativeMethods.WM_LBUTTONDOWN:
                RaiseMouseEvent(MouseButton.Left, Mouse.MouseDownEvent);
                break;

            case NativeMethods.WM_LBUTTONUP:
                RaiseMouseEvent(MouseButton.Left, Mouse.MouseUpEvent);
                break;

            case NativeMethods.WM_RBUTTONDOWN:
                RaiseMouseEvent(MouseButton.Right, Mouse.MouseDownEvent);
                break;

            case NativeMethods.WM_RBUTTONUP:
                RaiseMouseEvent(MouseButton.Right, Mouse.MouseUpEvent);
                break;
        }

        return base.WndProc(hwnd, msg, wParam, lParam, ref handled);
    }

谢谢, 周杰伦

我终于在这里找到了我想要的答案和确切的实现:

https://github.com/tgjones/gemini/blob/master/src/Gemini/Framework/Controls/HwndWrapper.cs

它非常完美。如果 NativeMethods.GetWheelDeltaWParam(Convert.ToInt32(value));

出现 OverFlow 异常

使用 NativeMethods.GetWheelDeltaWParam(Convert.ToInt64(值));

感谢 Tim Jones 的出色 github 回购和工作! https://github.com/tgjones