[英]How to get cursor position within another app
我正在使用.Net c#winforms。 我想将鼠标移到另一个应用程序上,并在将鼠标移到其界面上时查看光标X,Y的位置。 在表单标题栏上显示X,Y是可以的。 我想在此应用程序的表单上查看特定位置的X,Y位置。
我要执行此操作的原因是,此应用程序界面上有一些控件,我可以通过鼠标单击来旋转一个旋钮,每旋转一个旋钮单击一次。 我想编写一个应用程序,可以将鼠标光标定位到此应用程序表单上的特定X,Y位置,然后单击软件鼠标以将同一旋钮旋转一圈。 但是我想通过我的应用程序执行此操作,我想您可以说这有点像遥控器。 当您位于正确的X,Y位置上方时,其他应用程序旋钮会响应鼠标单击。
感谢您提供正确方向的指导。
将标签添加到您的窗体,并连接其MouseMove()和QueryContinueDrag()事件。 使用WindowFromPoint()和GetAncestor()API获取包含光标位置的主窗口的句柄,然后使用ScreenToClient()API将屏幕坐标转换为该Form的客户端坐标。 运行该应用程序,然后将“窗体”中的“标签”拖动到目标应用程序中的旋钮上。 标题栏应使用相对于当前应用程序的当前鼠标位置的客户端坐标进行更新:
private const uint GA_ROOT = 2;
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr WindowFromPoint(int xPoint, int yPoint);
[System.Runtime.InteropServices.DllImport("user32.dll", ExactSpelling = true)]
private static extern IntPtr GetAncestor(IntPtr hwnd, uint gaFlags);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool ScreenToClient(IntPtr hWnd, ref POINT lpPoint);
private void label1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
label1.DoDragDrop(label1, DragDropEffects.Copy);
}
}
private void label1_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
{
Point pt = Cursor.Position;
IntPtr wnd = WindowFromPoint(pt.X, pt.Y);
IntPtr mainWnd = GetAncestor(wnd, GA_ROOT);
POINT PT;
PT.X = pt.X;
PT.Y = pt.Y;
ScreenToClient(mainWnd, ref PT);
this.Text = String.Format("({0}, {1})", PT.X.ToString(), PT.Y.ToString());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.