做了个简单的指向窗口显示句柄,只能显示一级窗口。
自己拖个 Button 控件和 Timer 控件。把下面的控件名称改一下。
把 Button 拖大点可以显示多行文字内容。
API声明放到最前面。
[DllImport("User32.dll", CharSet = CharSet.Ansi, EntryPoint = "WindowFromPoint")]
public static extern IntPtr WindowFromPoint(int xPoint, int yPoint);
[DllImport("user32.dll", EntryPoint = "GetCursorPos")]
public static extern int GetCursorPos(ref MOUSEPOINT lpPoint);
[DllImport("user32.dll", EntryPoint = "GetWindowText")]
public static extern int GetWindowText(IntPtr hwnd, StringBuilder lpString, int cch);
bool 窗体捕获模式 == false;
private void 按钮_窗口句柄测试_Click(object sender, EventArgs e) {
if (窗体捕获模式 == false) {
窗体捕获模式 = true;
按钮_窗口句柄测试.BackColor = Color.LightBlue;
timer1.Enabled = true;
} else {
窗体捕获模式 = false;
按钮_窗口句柄测试.BackColor = Color.WhiteSmoke;
timer1.Enabled = false;
}
}
private void timer1_Tick(object sender, EventArgs e) {
if (窗体捕获模式) {
MOUSEPOINT p = new MOUSEPOINT();
var k = 0;
k = 窗体API.GetCursorPos(ref p);
var 窗体句柄 = 窗体API.WindowFromPoint(p.X, p.Y);
var 标题 = 获取窗体标题(窗体句柄);
按钮_窗口句柄测试.Text = string.Format("[ {0}, {1} ]",p.X,p.Y) +"\r\n"+ 标题 + "\r\n" + 窗体句柄.ToString();
}
}
public static string 获取窗体标题(IntPtr 句柄, int 文本长度 = 200) {
StringBuilder sb = new StringBuilder(文本长度);
int i = 窗体API.GetWindowText(句柄, sb, sb.Capacity);
return sb.ToString();
}
要想实现 SPY++ 的功能还要参见 ChildWindowFromPoint, ChildWindowFromPointEx, RealChildWindowFromPoint 等
这不就是spy++这种软件的功能吗
网上搜下这种工具的源码就有了