【问题标题】:motion detection from webcam to control mouse pointer using .NET从网络摄像头进行运动检测以使用 .NET 控制鼠标指针
【发布时间】:2012-03-24 13:27:10
【问题描述】:

我的 .NET 应用程序捕获检测到来自网络摄像头的特定类型对象的运动。我可以通过平移对象的运动来控制鼠标在我的表单中的移动。但是我想控制表单外的鼠标移动,就像某种虚拟鼠标一样。

实现这一目标的最佳技术是什么?

【问题讨论】:

    标签: .net mouse driver emgucv motion-detection


    【解决方案1】:

    您可以尝试通过 Win API 调用实现此目的:

     [DllImport("user32.dll")]
     static extern bool SetCursorPos(int X, int Y);
    
     [DllImport("user32.dll")]
     public static extern bool GetCursorPos(out Point pt);
    
     Point current;
     GetCursorPos(out current);
     SetCursorPos(current.X + 10, current.Y + 10);
    

    这将在应用程序之外工作。

    【讨论】:

    • 谢谢.. 我刚刚发现也可以使用 user32.dll 控制鼠标事件
    【解决方案2】:

    在 C# 中:

    //using System.Windows.Forms;
    //using System.Drawing;
    Cursor.Position = new Point(x, y);
    

    或者如果你想移动鼠标,而不是定位它:

    //using System.Windows.Forms;
    //using System.Drawing;
    Cursor.Position = Cursor.Position + new Size(deltaX, deltaY);
    

    【讨论】:

    • 谢谢.... 是否也可以通过代码在表单外进行点击或双击???
    • @Naveeen:是的,但要复杂得多。这是一个很好的例子:codeproject.com/Articles/28064/…
    【解决方案3】:

    通过代码执行单击并将光标移到表单外:

    [DllImport("user32.dll")]
    static extern void mouse_event(int flags, int dX, int dY, int buttons, int extraInfo);
    #region mouseConstants
    const int MOUSE_MOVE = 0x00000001;
    const int MOUSE_LEFTDOWN = 0x00000002;
    const int MOUSE_LEFTUP = 0x00000004;
    const int MOUSE_RIGHTDOWN = 0x00000008;
    const int MOUSE_RIGHTUP = 0x00000010;
    const int MOUSE_MIDDLEDOWN = 0x00000020;
    const int MOUSE_MIDDLEUP = 0x00000040;
    const int MOUSE_WHEEL = 0x00000800;
    const int MOUSE_ABSOLUTE = 0x00008000; 
    #endregion
    
    private void performClick(int posX, int posY)
    {
        Cursor.Position = new Point(posX, posY);  // to move the cursor at desired position
        mouse_event(MOUSE_LEFTDOWN, 0, 0, 0, 0);  // to perform left mouse down
        mouse_event(MOUSE_LEFTUP, 0, 0, 0, 0);    // to perform left mouse up
    }
    

    【讨论】:

      猜你喜欢
      • 2016-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-01
      相关资源
      最近更新 更多