MouseDown and then MouseUp doesn't work

人盡茶涼 提交于 2019-12-12 01:54:44

问题


I am trying to redirect mouse inputs on my Windows 7 application to some other 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 only do mouse releases, I want to be able to capture all mouse stuff, including movements and dragging.

So this is next logical thing to do but it doesn't work:

WM_LBUTTONDOWN:
Do everything like before without MouseUp()

WM_LBUTTONUP:
Do everything like before without MouseDown()

This doesn't even work for regular clicks. I can't figure out why. Can anybody help?


回答1:


Mouse buttons are funky. When it gets an down then up, at some level those are converted to a click (and I think at some point the mouse up gets eaten, but I may not be recalling that correctly).

It could be any number of things, but if the other windows is (or is not) converting the buttondown/up to mouse clicks, it could be confusing your code.

I suggest you print a lot of debugging info and try to figure out exactly what the system is doing.




回答2:


It might be worth looking at the SendMessage/PostMessage P/Invoke calls, and sending the messages directly to the window of the other application. You need to do some translation on the parameters so that the co-ordinates of mouse events tie up with what you want them to in the other application, but it's not a big deal to do that...

Edit -> I dug out some code where I have done this before... This is from a window which appears over the top of a tree view and replaces the default windows tooltip for that tree view.

    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);
    }


来源:https://stackoverflow.com/questions/2181330/mousedown-and-then-mouseup-doesnt-work

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