c# - 尝试模拟鼠标点击/拖动

标签 c# winforms mouseevent kinect

所以我试图模拟鼠标左键单击和鼠标左键释放来执行一些自动拖放操作。

它目前在 C# Winforms(是的,winforms :|)中并且有点笨拙。

基本上,一旦发送了点击,我希望它根据 Kinect 输入更新光标位置。 Kinect 方面的东西很好,但我不确定如何确定按钮是否仍被按下。

这是我目前正在使用的代码 + 一些伪代码来帮助更好地解释我自己(做的时候)。

class MouseImpersonator
{
    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    private static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);

    private const int leftDown = 0x02;
    private const int leftUp = 0x04;

    public static void Grab(int xPos, int yPos)
    {
        Cursor.Position = new Point(xPos + 25, yPos + 25);
        mouse_event(leftDown, (uint) xPos, (uint) yPos, 0, 0);

        //do
        //{
        //Cursor.Position = new Point(KinectSettings.movement.LeftHandX, KinectSettings.movement.LeftHandY);
        //} while (the left mouse button is still clicked);
    }

    public static void Release(int xPos, int yPos)
    {
        Cursor.Position = new Point(xPos + 25, yPos + 25);
        mouse_event(leftUp, (uint) xPos, (uint) yPos, 0, 0);
    }
}

我搜索了谷歌,除了 WPF 等价物外找不到任何我需要的东西:http://msdn.microsoft.com/en-us/library/system.windows.input.mouse.aspx

我有点力不从心,但非常感谢任何帮助。

卢卡斯。

    -

最佳答案

事实上,最简单的答案是使用 bool 并检查发生了什么。

我在一个新线程上启动它,所以它不会破坏其他一切。

理想情况下,你会稍微整理一下。

    public static void Grab(int xPos, int yPos)
    {
        _dragging = true;

        Cursor.Position = new Point(xPos, yPos + offSet);
        mouse_event(leftDown, (uint) xPos, (uint) yPos, 0, 0);

        var t = new Thread(CheckMouseStatus);
        t.Start();
    }
    public static void Release(int xPos, int yPos)
    {
        _dragging = false;
        Cursor.Position = new Point(xPos, yPos + offSet);
        mouse_event(leftUp, (uint) xPos, (uint) yPos, 0, 0);
    }

    private static void CheckMouseStatus()
    {
        do
        {
            Cursor.Position = new Point(KinectSettings.movement.HandX, KinectSettings.movement.HandY + offSet);
        } 
        while (_dragging);
    }

关于c# - 尝试模拟鼠标点击/拖动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8561755/

相关文章:

c# - 如何通过索引访问对象 C#

c# - 捕获访问路径被拒绝的异常

c# - Linq 到文本文件

c# - 单击和双击事件在 Canvas 中无法按预期工作

java - JTable 上的复选框列编辑

c# - Process.Close() 不关闭程序

c# - 我应该处理/捕获这些异常吗?

python - 如何通过在opencv python中单击鼠标按钮在播放视频的顶部绘制形状

c# - 澄清多线程/异步/并行编程

c# - Winform : Placing a panel over multiple panels