私の.NETアプリケーションキャプチャは、Webカメラから特定のタイプのオブジェクトの動きを検出します。オブジェクトの動きを変換することで、フォーム内のマウスの動きを制御できます。ただし、ある種の仮想マウスのように、フォームの外側でマウスの動きを制御したいと思います。
これを達成するための最良のテクニックは何でしょうか?
私の.NETアプリケーションキャプチャは、Webカメラから特定のタイプのオブジェクトの動きを検出します。オブジェクトの動きを変換することで、フォーム内のマウスの動きを制御できます。ただし、ある種の仮想マウスのように、フォームの外側でマウスの動きを制御したいと思います。
これを達成するための最良のテクニックは何でしょうか?
WinAPI呼び出しを介してこれを達成してみることができます。
[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);
これは、アプリケーションの外部で機能します。
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);
クリックしてカーソルをフォームの外に移動するには、次のコードを使用します。
[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
}