1

我正在尝试在 WPF 应用程序中托管 DirectX 内容。我从这个链接中学到的是继承 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控制

然后我在这里找到了非常有用的实现: 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);
    }

谢谢,杰

4

1 回答 1

2

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

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

它只是完美的工作。如果您得到 NativeMethods.GetWheelDeltaWParam(Convert.ToInt32(value)) 的溢出异常;

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

完全归功于 Tim Jones 这个很棒的 github 存储库和工作! https://github.com/tgjones

于 2017-11-09T05:43:16.660 回答