您的位置:首页 > 其它

获取鼠标坐标,鼠标所在窗体句柄

2008-12-26 15:36 471 查看
1.获取鼠标坐标

小持续没用hook

加了个timer

private void Form1_Load(object sender, EventArgs e)

{

this.timer1.Enabled = true;

this.timer1.Interval = 10;//timer控件的执行频率

}

为timer控件加代码

private void timer1_Tick(object sender, EventArgs e)

{

//显示鼠标坐标

//方法1:

textBox1.Text = Cursor.Position.ToString();

//textBox2.Text = Cursor.Position.Y.ToString();

//方法2:

//textBox1.Text = Control.MousePosition.X.ToString();

//textBox2.Text = Control.MousePosition.Y.ToString();

}

2.获取句柄与句柄标题

在获取坐标之后添加

// 显示鼠标所在句柄

IntPtr ptrWnd =(IntPtr)WindowFromPoint(Cursor.Position.X,Cursor.Position.Y);

textBox2.Text = "0x"+ptrWnd.ToString("x");

//显示所选句柄标题

int len = GetWindowTextLength(ptrWnd) + 1;

StringBuilder text = new StringBuilder(len);

GetWindowText(ptrWnd, text, len);

textBox3.Text = text.ToString();

api

[DllImport("user32.dll")]

public static extern int WindowFromPoint(int xPoint, int yPoint);

[DllImport("User32.dll")]

static extern int GetWindowText(IntPtr handle, StringBuilder text, int MaxLen);

[DllImport("User32.dll")]

static extern int GetWindowTextLength(IntPtr handle);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: