1

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?

Neil B
  • 269
  • 5
  • 16
  • Update: OnMouseLeftButtonUp fires in WPF, but not OnMouseLeftButtonDown. Lost the plot.com? – Neil B Mar 25 '10 at 16:39

3 Answers3

4

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;
}
Tom P.
  • 56
  • 2
1

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.

Neil B
  • 269
  • 5
  • 16
0

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.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
  • I am hooking into WndProc, I thought that would give me every message sent. Do you know can I register a WndClass with WPF? – Neil B Mar 25 '10 at 16:38
  • I'm not sure what you're doing wrong, but it could be that you're not in the right place in the WndProc chain? I'm not familiar enough with WPF to say how you would do it, but if you can call CreateWindowEx(), I don't see why you couldn't call RegisterClass(). – i_am_jorf Mar 25 '10 at 18:40
  • Ok thanks, I'll have a look into it. I suspect there is something else interfering like possibly the Ogre3D engine that I am passing the Hwnd to so it can render to my window. – Neil B Mar 29 '10 at 23:47
  • You should be able to use Spy++ to see the window chain and where the messages are disappearing. – i_am_jorf Mar 30 '10 at 01:03