当前位置: 首页 > 新闻动态 > 软件编程

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

作者:用户投稿 浏览: 发布日期:2026-01-16
[导读]:这篇文章主要介绍了C#实现获取鼠标句柄的方法,详细的讲述了实现获取鼠标句柄的具体步骤及实现方法,并附有完整的实例源码供大家参考,需要的朋友可以参考下

本文实例讲述了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#程序设计有所帮助

免责声明:转载请注明出处:http://shjed.com/news/378438.html

扫一扫高效沟通

多一份参考总有益处

免费领取网站策划SEO优化策划方案

请填写下方表单,我们会尽快与您联系
感谢您的咨询,我们会尽快给您回复!