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

后端 未结 3 1595
太阳男子
太阳男子 2021-01-01 02:57

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

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

相关标签:
3条回答
  • 2021-01-01 03:07

    This CodeProject article describes 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.

    HTH.

    0 讨论(0)
  • 2021-01-01 03:08

    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.

    [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);
    
    0 讨论(0)
  • 2021-01-01 03:18

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

    foreach(Process proc in Process.GetProcesses())
    {
       if(proc.MainWindowTitle.StartsWith("Some String"))
       {
          IntPtr handle = proc.MainWindowHandle;
          // ...
       }
    }
    
    0 讨论(0)
提交回复
热议问题