[原創]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;
參數說明 
hwndParent IntPtr ,父窗口句柄,如果hwndParent為 0 ,則函數以桌面窗口為父窗口,查找桌面窗口的所有子窗口。
hwndChildAfter IntPtr ,子窗口句柄,查找從在Z序中的下一個子窗口開始。子窗口必須為hwndParent窗口的直接子窗口而非后代窗口。如果HwndChildAfter為NULL,查找從hwndParent的第一個子窗口開始。如果hwndParent 和 hwndChildAfter同時為NULL,則函數查找所有的頂層窗口及消息窗口。
lpszClass string ,控件類名
lpszWindow string ,控件標題,如果該參數為 NULL,則為所有窗口全匹配。
返回值:控件句柄
 
public static extern int EnumChildWindows(IntPtr hWndParent, CallBack lpfn, int lParam);
函數功能:枚舉一個父窗口的所有子窗口。
導入庫:user32.lib
頭文件:winuser.h
命名空間 using System.Runtime.InteropServices;
參數說明
hWndParent IntPtr 父窗口句柄
lpfn CallBack 回調函數的地址
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工具來查看窗口的句柄、標題、類型等信息,非常方便。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM