当前位置: 移动技术网 > IT编程>开发语言>c# > C#实现获取鼠标句柄的方法

C#实现获取鼠标句柄的方法

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例讲述了c#实现获取鼠标句柄的方法,分享给大家供大家参考。具体实现方法如下:

一、调用user32.dll

(1)引用

using system.runtime.interopservices;

(2)调用方法

1、获取窗口标题

[dllimport( "user32.dll" )]
public static extern int getwindowtext( intptr hwnd, stringbuilder lpstring,int nmaxcount );

注:hwnd 窗口句柄  lpstring 窗口标题   nmaxcount 最大值

2、获取类名

[dllimport( "user32.dll" )]  
public static extern int getclassname( intptr hwnd, stringbuilder lpstring,int nmaxcount );

注:hwnd 句柄 lpstring 类名 nmaxcount 最大值

3、根据坐标获取窗口句柄

[dllimport( "user32.dll" )]  
public static extern intptr windowfrompoint(point point);

注:point 坐标

二、显示数据

(1) 获取鼠标坐标

int x = cursor.position.x;
int y = cursor.position.y;
this.textbox4.text = string.format( "({0},{1})" , x, y);

(2) 获取句柄

point p = new point(x,y);
intptr formhandle = windowfrompoint(p);
this.textbox1.text = formhandle.tostring();

(3) 得到窗口标题

getwindowtext(formhandle,title,title.capacity);
this.textbox2.text = title.tostring();

(4)得到窗体的类名

stringbuilder cllassname = new stringbuilder();
getclassname(formhandle,cllassname,cllassname.capacity);
this.textbox3.text = cllassname.tostring();

(5)load事件

this.timer1.enabled = !this.timer1.enabled;

注:动态显示信息

三、运行结果如下图所示:

 

四、完整实例代码点击此处。

希望本文所述对大家的c#程序设计有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网