试图模拟鼠标单击/拖动
本文关键字:拖动 单击 鼠标 模拟 | 更新日期: 2023-09-27 18:18:21
所以我试图模拟鼠标左键点击和鼠标左键释放来做一些自动拖放。
它目前在c# Winforms中(是的,Winforms:|),并且有点像鹅。
基本上,一旦发送点击,我希望它根据Kinect输入更新光标位置。Kinect方面的东西很好,但我不确定如何发现按钮是否仍然按下。
这是我目前使用的代码+一些伪代码来帮助更好地解释我自己(do while)。
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);
}
如果鼠标左键按下则返回true,如果按上则返回false, Control为System.Windows.Forms.Control:
Control.MouseButtons.HasFlag(MouseButtons.Left)
注。
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern void mouse_event(uint dwFlags, int dx, int dy, uint cButtons, uint dwExtraInfo);
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
const uint MOUSEEVENTF_LEFTUP = 0x0004;
const uint MOUSEEVENTF_MOVE = 0x0001;
static void Drag(int startX,int startY,int endX,int endY)
{
endX = endX - startX;
endY = endY - startY;
SetCursorPos(startX, startY);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_MOVE, endX, endY, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}