WPF WIN32 hwndhost WM_MOUSEMOVE WM_MOUSEHOVER

匿名 (未验证) 提交于 2019-12-03 10:10:24

问题:

I have a WPF app with a usercontrol that contains a HwndHost. The HwndHost is created as follows:

  hwndHost = CreateWindowEx(0, "static", "",                             WS_CHILD | WS_VISIBLE,                             0, 0,                             hostHeight, hostWidth,                             hwndParent.Handle,                             (IntPtr)HOST_ID,                             IntPtr.Zero,                             0);    hwndControl = CreateWindowEx(0, "Static", "",                                 WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN                                   ,                                 0, 0,                                 hostHeight, hostWidth,                                 hwndHost,                                 (IntPtr)PICTUREBOX_ID,                                 IntPtr.Zero,                                 0); 

I then hook into the message pump using HwndSourceHook and loads of messages come through.

Except the ones I want i.e. WM_MOUSEMOVE, WM_MOUSEHOVER, WM_LBUTTONDOWN and WM_LBUTTONUP

Also the OnMouseLeftButtonDown event is not fired in the WPF code on the main window or the control, I assume because windows is trapping it and throwing it away.

Anybody know how I can get these to come through, either with or without using the WIN32 window messages?

回答1:

You need to handle WM_NCHITTEST. Until you do, it won't send you mouse messages.

// C++/CLI implementation of HwndHost.WndProc(). virtual IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, bool% handled) override {     switch (msg)     {         case WM_NCHITTEST:         {             handled = true;         return IntPtr(HTCLIENT);         }     }      return IntPtr::Zero; } 


回答2:

It would appear that the Border element in WPF prevents the OnMouseLeftButtonDown being thrown. My temporary solution until I find a better one is to change the WPF border to a WPF button then all mouse events are triggered.

Not ideal for most people but as I am using it to render 3D onto it doesn't matter what is underneath it.



回答3:

Why don't you register a WndClass and get messages delivered to your own WndProc instead of hooking the message pump? I suspect you'd get much better results.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!