首先,调用API,编写获取坐标函数,不要忘了导入命名空间:
using System.Runtime.InteropServices;
具体代码:
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
public POINT(int x, int y)
{
this.X = x;
this.Y = y;
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool GetCursorPos(out POINT pt);
在需要获取屏幕坐标的地方使用,如,打开某个窗口,设置窗口的打开位置为当前鼠标相对屏幕的坐标:
Window detail = new win_Detail(displayText);
POINT pt = new POINT();
GetCursorPos(out pt);
detail.Left = pt.X;
detail.Top = pt.Y;
detail.ShowDialog();
OK.
本文为参考网络资源。