根据窗口名,获得窗口句柄

根据窗口名,获得窗口句柄

1.引入User32函数 FindWindow

通过窗口名获取句柄的函数

/// <summary>
/// 获取窗体句柄
/// </summary>
/// <param name="lpClassName">传null</param>
/// <param name="lpWindowName">窗口名</param>
/// <returns></returns>
[DllImport("User32.dll", EntryPoint = "FindWindow")]
public extern static IntPtr FindWindow(string lpClassName, string lpWindowName);

 

2.通过关键字获取完整的窗口名称

Tip:筛选出来的process中的Handle不是真正的Handle

1  var Processes = Process.GetProcesses();
2  var result = Processes.Where(x => x.MainWindowTitle.Contains("关键字"));
3  var process = result.First();
4  var WindowsHandle = FindWindow(null, p.MainWindowTitle);        

 

FindWindow
posted @ 2023-05-29 10:31  牛牛油果  阅读(228)  评论(0编辑  收藏  举报