MouseDown 然后 MouseUp 不起作用

编程入门 行业动态 更新时间:2024-10-26 14:29:15
本文介绍了MouseDown 然后 MouseUp 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试将 Windows 7 应用程序上的鼠标输入重定向到其他窗口.如果我在获得 WM_LBUTTONUP 时执行此操作,则它可以工作(其中 MouseDown 和 MouseUp 是 Win api 中的 SendInput 函数):

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?

推荐答案

可能值得查看 SendMessage/PostMessage P/Invoke 调用,并将消息直接发送到其他应用程序的窗口.您需要对参数进行一些转换,以便鼠标事件的坐标与您希望它们在其他应用程序中的位置保持一致,但这样做没什么大不了的...

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

这篇关于MouseDown 然后 MouseUp 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 00:08:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1126815.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   MouseDown   MouseUp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!