Get a window handle with a not fully known name. (FROM#)

The name is partially static with a variable suffix. For example, "Window Name {- user_id}".

How can I get a descriptor without knowing the exact name?

+3
source share
3 answers

View all Processes and check MainWindowTitle . (You can use regular expressions, or StartsWithetc.)

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

This CodeProject article describes how to list top-level windows (based on the Win32 EnumWindows API ). You can easily change it to filter on a partial window title: Change EnumWindowsCallBack.

NTN.

+5

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

[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);

Using:

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

Source: https://habr.com/ru/post/1704635/


All Articles