日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

[原创]C#应用WindowsApi实现查找枚举(FindWindow、EnumChildWindows)窗体控件,并发送消息。

發布時間:2023/12/13 综合教程 20 生活家
生活随笔 收集整理的這篇文章主要介紹了 [原创]C#应用WindowsApi实现查找枚举(FindWindow、EnumChildWindows)窗体控件,并发送消息。 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

首先介紹基本WindowsApi:

public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

函數說明:在窗口列表中尋找與指定條件相符的第一個窗口

導入庫:user32.lib
頭文件:winuser.h
命名空間 using System.Runtime.InteropServices;
參數說明 
lpClassName String,窗口類名
lpWindowName String,窗口標題
返回值:窗口句柄

public static extern IntPtr FindWindowEx(IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow);

函數說明:在窗口列表中尋找與指定條件相符的第一個子窗口

導入庫:user32.lib
頭文件:winuser.h
命名空間 using System.Runtime.InteropServices;
參數說明 
hwndParentIntPtr,父窗口句柄,如果hwndParent為 0 ,則函數以桌面窗口為父窗口,查找桌面窗口的所有子窗口。
hwndChildAfterIntPtr,子窗口句柄,查找從在Z序中的下一個子窗口開始。子窗口必須為hwndParent窗口的直接子窗口而非后代窗口。如果HwndChildAfter為NULL,查找從hwndParent的第一個子窗口開始。如果hwndParent 和 hwndChildAfter同時為NULL,則函數查找所有的頂層窗口及消息窗口。
lpszClassstring ,控件類名
lpszWindowstring ,控件標題,如果該參數為 NULL,則為所有窗口全匹配。
返回值:控件句柄。

public static extern int EnumChildWindows(IntPtrhWndParent, CallBack lpfn, int lParam);

函數功能:枚舉一個父窗口的所有子窗口。

導入庫:user32.lib
頭文件:winuser.h
命名空間 using System.Runtime.InteropServices;
參數說明

hWndParent IntPtr父窗口句柄
lpfnCallBack回調函數的地址
lParam int自定義的參數
注意:回調函數的返回值將會影響到這個API函數的行為。如果回調函數返回true,則枚舉繼續直到枚舉完成;如果返回false,則將會中止枚舉。

其中CallBack是這樣的一個委托:public delegate bool CallBack(IntPtr hwnd, int lParam);如果 CallBack 返回的是true,則會繼續枚舉,否則就會終止枚舉。

應用實例:

用到的WindowApi類

static class WindowsApi
{
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow);

[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam);

[DllImport("user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true)]
public static extern void SetForegroundWindow(IntPtr hwnd);

[DllImport("user32.dll")]
public static extern int EnumChildWindows(IntPtr hWndParent, CallBack lpfn, int lParam);

public delegate bool CallBack(IntPtr hwnd, int lParam);
}

class Program

{

/// <summary>
/// 查找窗體上控件句柄
/// </summary>
/// <param name="hwnd">父窗體句柄</param>
/// <param name="lpszWindow">控件標題(Text)</param>
/// <param name="bChild">設定是否在子窗體中查找</param>
/// <returns>控件句柄,沒找到返回IntPtr.Zero</returns>
private IntPtr FindWindowEx(IntPtr hwnd, string lpszWindow, bool bChild)
{
IntPtr iResult = IntPtr.Zero;
// 首先在父窗體上查找控件
iResult = WindowsApi.FindWindowEx(hwnd, 0, null, lpszWindow);
// 如果找到直接返回控件句柄
if (iResult != IntPtr.Zero) return iResult;

// 如果設定了不在子窗體中查找
if (!bChild) return iResult;

// 枚舉子窗體,查找控件句柄
int i = WindowsApi.EnumChildWindows(
hwnd,
(h, l) =>
{
IntPtr f1 = WindowsApi.FindWindowEx(h, 0, null, lpszWindow);
if (f1 == IntPtr.Zero)
return true;
else
{
iResult = f1;
return false;
}
},
0);
// 返回查找結果
return iResult;
}

private void OnRunClick(object sender, EventArgs e)
{
// 查找主界面句柄
IntPtr mainHandle = WindowsApi.FindWindow(null, "主界面(Ver:3.1.3.47)");
if (mainHandle != IntPtr.Zero)
{
// 查找按鈕句柄
IntPtr iBt = FindWindowEx(mainHandle, "現(F1)", true);
if (iBt != IntPtr.Zero)
// 發送單擊消息
WindowsApi.SendMessage(iBt, 0xF5, 0, 0);
}
}

}

應用工具:

這里可以應用spy工具來查看窗口的句柄、標題、類型等信息,非常方便。

總結

以上是生活随笔為你收集整理的[原创]C#应用WindowsApi实现查找枚举(FindWindow、EnumChildWindows)窗体控件,并发送消息。的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。