C#操作窗口类(句柄操作)
更新时间:2023-11-10 17:42:01 阅读量: 教育文库 文档下载
C#写个类操作窗口(句柄操作)
实现过程:
过程一:找到当前鼠标位置的句柄
您的使用2个WinAPI(俺喜欢自己封装下来用):
View Code
[DllImport(\, EntryPoint = \)] public static extern bool GetCursorPos(out Point pt);
[DllImport(\, EntryPoint = \)] public static extern IntPtr WindowFromPoint(Point pt);
//鼠标位置的坐标
public static Point GetCursorPosPoint() {
Point p = new Point(); if (GetCursorPos(out p)) {
return p; }
return default(Point); }
///
/// 找到句柄 ///
///
///
public static IntPtr GetHandle(Point p) {
return WindowFromPoint(p); }
过程二:改变窗口的Text
您的使用1个WinAPI:
View Code
[DllImport(\, EntryPoint = \)]
private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);
///
///
///
public static void SetText(IntPtr hWnd, string lParam) {
SendMessage(hWnd, WM_SETTEXT, IntPtr.Zero, lParam); }
private const int WM_SETTEXT = 0x000C;
通过这个方法就能改变Text的值了。
思考:如果俺把这个窗口的句柄记录下来,下次不用鼠标获取,直接就能改变值不蛮好的嘛。
例如:我有个桌面系统老叫我输入用户名,密码。我记录用户名和密码的窗口句柄,然后改变他们的输入值,这样多省事。(只是举例,不考虑安全性) 问题:你会告诉我,窗口句柄的每次重建会变的呀,咋办。 回答:类名不变呀。
过程三:您的准备一些工具吧,例如:句柄找类名呀,类名找句柄什么的等等,下面会用到一些WinAPI
View Code
[DllImport(\, EntryPoint = \)]
private static extern IntPtr FindWindow(string IpClassName, string IpWindowName);
[DllImport(\, EntryPoint = \)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string
lpszClass, string lpszWindow);
[DllImport(\, EntryPoint = \)] public static extern IntPtr GetParent(IntPtr hWnd);
[DllImport(\, CharSet = CharSet.Auto)]
public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
///
///
///
public static IntPtr GetHandle(string IpClassName) {
return FindWindow(IpClassName, null); }
///
///
/// ///
///
public static IntPtr GetChildHandle(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass)
{
return FindWindowEx(hwndParent, hwndChildAfter, lpszClass, null); }
///
/// ///
///
public static List
List
for (IntPtr hwndClient = GetChildHandle(hwndParent, IntPtr.Zero, className); hwndClient != IntPtr.Zero; hwndClient = GetChildHandle(hwndParent, hwndClient, className)) {
return null; }
#region 转义
private static string Escape(string arg) {
return arg.Replace(\, \).Replace(\,\); }
private static string UnEscape(string arg) {
return arg.Replace(\, \).Replace(\, \); }
#endregion
public static WinHWND GetWinHWND() {
return new WinHWND(WinAPI.GetHandle(WinAPI.GetCursorPosPoint())); } }
上全部代码,里面加了窗口的部分属性,扩展其他的属性,自己发挥吧,就是搞WinAPI
View Code
using System;
using System.Collections.Generic; using System.Linq; using System.Text;
using System.Runtime.InteropServices; using System.Drawing;
using System.Collections;
namespace InformationCollectionDataFill {
public class WinAPI {
#region WinodwsAPI
[DllImport(\, EntryPoint = \)]
private static extern IntPtr FindWindow(string IpClassName, string IpWindowName);
[DllImport(\, EntryPoint = \)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport(\, EntryPoint = \)]
private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);
[DllImport(\, EntryPoint = \)] public static extern IntPtr GetParent(IntPtr hWnd);
[DllImport(\, EntryPoint = \)] public static extern bool GetCursorPos(out Point pt);
[DllImport(\, EntryPoint = \, CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr WindowFromPoint(Point pt);
[DllImport(\, CharSet = CharSet.Auto)]
public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport(\, CharSet = CharSet.Auto)]
public static extern int GetWindowText(IntPtr hWnd, [Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpString, int nMaxCount);
[DllImport(\, CharSet = CharSet.Auto)]
public static extern int GetWindowRect(IntPtr hwnd, ref Rectangle rc);
[DllImport(\, CharSet = CharSet.Auto)]
public static extern int GetClientRect(IntPtr hwnd, ref Rectangle rc);
[DllImport(\, CharSet = CharSet.Auto)]
public static extern int MoveWindow(IntPtr hwnd, int x, int y, int nWidth, int nHeight, bool bRepaint);
[DllImport(\, CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] public static extern int ScreenToClient(IntPtr hWnd, ref Rectangle rect); #endregion
#region 封装API方法 ///
///
///
public static IntPtr GetHandle(string IpClassName) {
return FindWindow(IpClassName, null); }
///
///
///
public static IntPtr GetHandle(Point p)
{
return WindowFromPoint(p); }
//鼠标位置的坐标
public static Point GetCursorPosPoint() {
Point p = new Point(); if (GetCursorPos(out p)) {
return p; }
return default(Point); }
///
///
/// ///
///
public static IntPtr GetChildHandle(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass)
{
return FindWindowEx(hwndParent, hwndChildAfter, lpszClass, null); }
///
/// ///
///
public static List
List
for (IntPtr hwndClient = GetChildHandle(hwndParent, IntPtr.Zero, className); hwndClient != IntPtr.Zero; hwndClient = GetChildHandle(hwndParent, hwndClient, className)) {
resultList.Add(hwndClient); }
return resultList;
}
///
///
///
public static void SetText(IntPtr hWnd, string lParam) {
SendMessage(hWnd, WM_SETTEXT, IntPtr.Zero, lParam); }
private const int WM_SETTEXT = 0x000C;
///
///
///
public static string GetText(IntPtr hWnd) {
StringBuilder result = new StringBuilder(128);
GetWindowText(hWnd, result, result.Capacity); return result.ToString(); }
///
///
///
public static string GetClassName(IntPtr hWnd) {
StringBuilder lpClassName = new StringBuilder(128);
if (GetClassName(hWnd, lpClassName, lpClassName.Capacity) == 0) {
throw new Exception(\); }
return lpClassName.ToString(); }
///
///
///
public static Rectangle GetWindowRect(IntPtr hWnd) {
Rectangle result = default(Rectangle); GetWindowRect(hWnd, ref result); return result; }
///
/// 窗口相对屏幕位置转换成父窗口位置
///
///
public static Rectangle ScreenToClient(IntPtr hWnd, Rectangle rect) {
Rectangle result = rect;
ScreenToClient(hWnd, ref result); return result; }
///
///
public static Rectangle GetClientRect(IntPtr hWnd) {
Rectangle result = default(Rectangle); GetClientRect(hWnd, ref result); return result; }
#endregion }
public class WinHWND {
public IntPtr HWND { get; set; }
public string ClassName { get; set; } public WinHWND Parent { get; set; }
public int InParentSequence { get; set; }
private Rectangle _currentRect; private string _Text; private int _Left; private int _Top; private int _Width; private int _Height;
public string Text
{
sChildMarks[i] = sMarks[i]; }
return sChildMarks; }
//我一直觉得这段写很丑陋,谁能帮帮我改改
public static WinHWND GetWinHWND(string sMark) {
List
//由于主窗口在桌面出现所以很可能同名,所以要看看他的儿子和孙子...是不是都匹配
foreach (IntPtr baseHwnd in baseHwnds) {
IntPtr handle = baseHwnd;
for (int i = sChildMarks.Length - 1; i >= 0; i--) {
string[] sChildMark = sChildMarks[i].Split(':'); try {
handle = WinAPI.GetChildHandles(handle, UnEscape(sChildMark[0]))[int.Parse(sChildMark[1])];
}
catch
{
break; }
if (i == 0) return new WinHWND(handle); }
continue; }
return null; }
#region 转义
private static string Escape(string arg) {
return arg.Replace(\, \).Replace(\,\); }
private static string UnEscape(string arg) {
return arg.Replace(\, \).Replace(\, \); }
#endregion } }
Demo.rar效果:
正在阅读:
C#操作窗口类(句柄操作)11-10
睡觉前的微信说说12-18
三年级数学上册天天练8110-08
计网 - 第四章作业11-04
芜湖市规划管理技术规定06-16
被高估的越南09-27
新课标PEP小学英语五年级下册《Unit 4 What are you doing》精品03-08
预备党员个人思想学习工作总结(含5篇)08-23
公众承诺书模板(共8篇)06-25
- exercise2
- 铅锌矿详查地质设计 - 图文
- 厨余垃圾、餐厨垃圾堆肥系统设计方案
- 陈明珠开题报告
- 化工原理精选例题
- 政府形象宣传册营销案例
- 小学一至三年级语文阅读专项练习题
- 2014.民诉 期末考试 复习题
- 巅峰智业 - 做好顶层设计对建设城市的重要意义
- (三起)冀教版三年级英语上册Unit4 Lesson24练习题及答案
- 2017年实心轮胎现状及发展趋势分析(目录)
- 基于GIS的农用地定级技术研究定稿
- 2017-2022年中国医疗保健市场调查与市场前景预测报告(目录) - 图文
- 作业
- OFDM技术仿真(MATLAB代码) - 图文
- Android工程师笔试题及答案
- 生命密码联合密码
- 空间地上权若干法律问题探究
- 江苏学业水平测试《机械基础》模拟试题
- 选课走班实施方案
- 操作
- C#
- 句柄
- 窗口
- 临沂市五金零售行业企业名录2018版1044家 - 图文
- SDH光传输技术复习题库 3
- 人教版小学数学四年级上册单元测试卷附答案-全册(1)
- 电力系统继电保护原理复习题(ZH电0791班)
- 调频收音机原理框图
- 2016年高考地理二轮复习 专题18 环境保护(讲)(含解析)
- 2016植物生理学复习题(问答)
- 第二课 青春的心弦 男生女生
- 暗标施工组织设计
- 0308东方今报三八特刊策划
- 《公共管理学》 - 笔记
- 2011年江苏省公务员考试申论真题及解析
- 土木工程材料B(1)
- 土方开挖施工方案 Microsoft Word 文档
- 组织胚胎复习资料
- 2015-2016学年上三校生高考英语试题三
- 前期物业管理介入工作重点探讨
- 关于公司员工启用“钉钉”软件的管理规定
- 2016届中考英语 完形填空与阅读理解复习练习 Unit 5
- 三年级上册劳动与技术教案