C#当前窗口句柄,只需要使用下列的方法即可:
public static IntPtr WinGetHandle(string wName) { IntPtr hWnd = IntPtr.Zero; foreach (Process pList in Process.GetProcesses()) { if (pList.MainWindowTitle.Contains(wName)) { hWnd = pList.MainWindowHandle; } } return hWnd; }
传入的“wName”值就是当前窗口的text标题名称,而不是name,如图所示:
在使用中要想获取当前窗口句柄,则这样调用即可:
int handle = WinGetHandle("Form1").ToInt32();