首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用.NET从网络摄像头进行运动检测以控制鼠标指针

使用.NET从网络摄像头进行运动检测以控制鼠标指针
EN

Stack Overflow用户
提问于 2012-03-24 21:27:11
回答 3查看 2K关注 0票数 0

我的.NET应用程序捕获并检测网络摄像头中特定类型对象的运动。我能够通过翻译物体的运动来控制鼠标在我的形状中的移动。然而,我想控制鼠标在窗体之外的移动,就像某种虚拟鼠标一样。

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-03-25 14:08:30

要在代码中执行单击并将光标移动到窗体外部的操作:

代码语言:javascript
复制
[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
}
票数 0
EN

Stack Overflow用户

发布于 2012-03-24 21:39:27

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

代码语言:javascript
复制
 [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);

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

票数 2
EN

Stack Overflow用户

发布于 2012-03-24 21:40:54

在C#中:

代码语言:javascript
复制
//using System.Windows.Forms;
//using System.Drawing;
Cursor.Position = new Point(x, y);

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

代码语言:javascript
复制
//using System.Windows.Forms;
//using System.Drawing;
Cursor.Position = Cursor.Position + new Size(deltaX, deltaY);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9852124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档