C# 获取标题不完全已知的窗口句柄。(C#)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/637652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 11:16:06  来源:igfitidea点击:

Get the handle of a window with not fully known title. (C#)

c#api

提问by Sven

The title is partially static with an variable suffix. For example "Window Title {- user_id}".

标题是部分静态的,带有可变后缀。例如“窗口标题{- user_id}”

How can I get the handle without knowing the exact title?

在不知道确切标题的情况下如何获得句柄?

采纳答案by Daniel LeCheminant

Look through all the Processes and check the MainWindowTitle. (You can use regexps, or StartsWith, etc)

查看所有进程并检查MainWindowTitle。(您可以使用正则表达式,或StartsWith等)

foreach(Process proc in Process.GetProcesses())
{
   if(proc.MainWindowTitle.StartsWith("Some String"))
   {
      IntPtr handle = proc.MainWindowHandle;
      // ...
   }
}

回答by Serge Wautier

This CodeProject articledescribes how to enumerate Top level windows (Based on Win32 API EnumWindows). You can easily modify it to filter on a partial window title: Modify EnumWindowsCallBack.

这篇 CodeProject 文章描述了如何枚举顶级窗口(基于 Win32 API EnumWindows)。您可以轻松修改它以过滤部分窗口标题:修改 EnumWindowsCallBack。

HTH.

哈。

回答by ebattulga

Get by class name and parent window handle. For example: get start button handle using win32api. First you know parent window class name using spyxx tool.

通过类名和父窗口句柄获取。例如:使用 win32api 获取开始按钮句柄。首先,您使用 spyxx 工具知道父窗口类名称。

[DllImport("user32.dll")]
public static extern IntPtr FindWindowEx(IntPtr handleParent, IntPtr handleChild, string className, string WindowName);
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string className, string windowTitle);

Usage:

用法:

IntPtr handle = FindWindowEx(FindWindow("Shell_TrayWnd",null), new IntPtr(0), "Button", null);