-
?获取VS2010 C#代码】用于获取鼠标当前位置关联的窗口句柄名称、文本内容及其位置
资源介绍
Microsoft Visual Studio 2010做的C#获取鼠标所在位置的窗口句柄名称和文字及位置实例,主要代码:
[StructLayout(LayoutKind.Sequential)]//定义与API相兼容结构体,实际上是一种内存转换
public struct POINTAPI
{
public int X;
public int Y;
}
[DllImport("user32.dll", EntryPoint = "GetCursorPos")]//获取鼠标坐标
public static extern int GetCursorPos(
ref POINTAPI lpPoint
);
[DllImport("user32.dll", EntryPoint = "WindowFromPoint")]//指定坐标处窗体句柄
public static extern int WindowFromPoint(
int xPoint,
int yPoint
);
[DllImport("user32.dll", EntryPoint = "GetWindowText")]
public static extern int GetWindowText(
int hWnd,
StringBuilder lpString,
int nMaxCount
);
[DllImport("user32.dll", EntryPoint = "GetClassName")]
public static extern int GetClassName(
int hWnd,
StringBuilder lpString,
int nMaxCont
);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left; //最左坐标
public int Top; //最上坐标
public int Right; //最右坐标
public int Bottom;//最下坐标
}