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 GetChildHandles(IntPtr hwndParent, string className) {

List resultList = new 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 GetChildHandles(IntPtr hwndParent, string className) {

List resultList = new 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 baseHwnds = WinAPI.GetChildHandles(IntPtr.Zero, GetBaseMark(sMark)); string[] sChildMarks = GetChildMarks(sMark);

//由于主窗口在桌面出现所以很可能同名,所以要看看他的儿子和孙子...是不是都匹配

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效果:

本文来源:https://www.bwwdw.com/article/ds0v.html

Top