C#实现获取鼠标句柄的方法
作者:shichen2014 时间:2022-05-06 21:13:41
本文实例讲述了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#程序设计有所帮助
标签:C#,获取
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解Flutter桌面应用如何进行多分辨率适配
2023-06-17 07:14:59
c#基础学习之封装
2023-12-06 16:45:21
C#异步下载文件
2021-10-05 03:23:40
![](https://img.aspxhome.com/file/2023/8/94598_0s.jpg)
SpringBoot 2.x整合Log4j2日志的详细步骤
2022-08-05 09:50:19
![](https://img.aspxhome.com/file/2023/0/101760_0s.jpg)
Flink开发IDEA环境搭建与测试的方法
2023-02-21 21:20:46
![](https://img.aspxhome.com/file/2023/0/84270_0s.jpg)
Java 关于时间复杂度和空间复杂度的深度刨析
2023-11-10 16:07:39
![](https://img.aspxhome.com/file/2023/8/59358_0s.jpg)
jmeter+ant+jenkins自动化测试环境配置搭建过程
2023-11-17 23:38:06
![](https://img.aspxhome.com/file/2023/4/81184_0s.png)
Java 二分查找算法的实现
2022-07-23 11:10:13
C#实现微信分账功能的完整步骤
2022-10-24 17:43:46
![](https://img.aspxhome.com/file/2023/2/81132_0s.png)
深入探究Java线程与进程有哪些区别
2023-05-06 13:26:03
![](https://img.aspxhome.com/file/2023/3/92543_0s.png)
C#飞行棋小程序设计代码
2021-10-06 23:45:25
mybatis 传入null值的解决方案
2023-11-23 06:54:44
![](https://img.aspxhome.com/file/2023/5/59775_0s.png)
spring+hibernate 两种整合方式配置文件的方法
2022-02-27 21:36:47
![](https://img.aspxhome.com/file/2023/7/94227_0s.png)
java实例方法被覆盖,静态方法被隐藏Explain(详解)
2022-07-20 08:05:03
Java线程池用法实战案例分析
2022-01-22 07:57:16
Java判断两个日期相差天数的方法
2021-11-29 05:55:07
Java多线程之读写锁分离设计模式
2021-06-08 07:20:48
详解springmvc常用5种注解
2023-03-27 15:15:43
![](https://img.aspxhome.com/file/2023/4/68514_0s.png)
springmvc实现json交互-requestBody和responseBody
2023-02-26 17:10:45
![](https://img.aspxhome.com/file/2023/0/83440_0s.png)
Java陷阱之assert关键字详解
2023-05-24 20:20:23