Mousedown and then mouseup not working

I am trying to redirect mouse inputs in my windows 7 application to another window. If I do this when I get WM_LBUTTONUP, it works (where MouseDown and MouseUp are SendInput functions in Win api):

SetForegroundWindow( other window );
SetCursorPos( somewhere on the window );
MouseDown();
MouseUp();
SetCursorPos( back );
SetForegroundWindow( main window );

But I donโ€™t want to do only mouse releases, I want to be able to capture all mouse elements, including movements and drag and drop.

So, this is the next logical task, but it does not work:

WM_LBUTTONDOWN:
Do everything like before without MouseUp()

WM_LBUTTONUP:
Do everything like before without MouseDown()

This does not work even with regular clicks. I canโ€™t understand why. Can anyone help?

0
source share
2 answers

, SendMessage/PostMessage P/Invoke . , , , , ...

โ†’ - , ... , .

    private IntPtr _translate(IntPtr LParam)
    {
        // lparam is currently in client co-ordinates, and we need to translate those into client co-ordinates of
        // the tree view we're attached to
        int x = (int)LParam & 0xffff;
        int y = (int)LParam >> 16;

        Point screenPoint = this.PointToScreen(new Point(x, y));
        Point treeViewClientPoint = _tv.PointToClient(screenPoint);

        return (IntPtr)((treeViewClientPoint.Y << 16) | (treeViewClientPoint.X & 0xffff));
    }

    const int MA_NOACTIVATE = 3;

    protected override void WndProc(ref Message m)
    {
        switch ((WM)m.Msg)
        {
            case WM.LBUTTONDBLCLK:
            case WM.RBUTTONDBLCLK:
            case WM.MBUTTONDBLCLK:
            case WM.XBUTTONDBLCLK:
            {
                IntPtr i = _translate(m.LParam);
                _hide();
                InteropHelper.PostMessage(_tv.Handle, m.Msg, m.WParam, i);
                return;
            }
            case WM.MOUSEACTIVATE:
            {
                m.Result = new IntPtr(MA_NOACTIVATE);
                return;
            }
            case WM.MOUSEMOVE:
            case WM.MOUSEHWHEEL:
            case WM.LBUTTONUP:
            case WM.RBUTTONUP:
            case WM.MBUTTONUP:
            case WM.XBUTTONUP:
            case WM.LBUTTONDOWN:
            case WM.RBUTTONDOWN:
            case WM.MBUTTONDOWN:
            case WM.XBUTTONDOWN:
            {
                IntPtr i = _translate(m.LParam);
                InteropHelper.PostMessage(_tv.Handle, m.Msg, m.WParam, i);
                return;
            }         
        }
        base.WndProc(ref m);
    }
0

. , - ( , - , ).

, ( ) buttondown/up to mouse, .

, .

0

All Articles