Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

api

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?

like image 669
Sven Avatar asked Mar 12 '09 07:03

Sven


2 Answers

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;
      // ...
   }
}
like image 161
Daniel LeCheminant Avatar answered Oct 06 '22 19:10

Daniel LeCheminant


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.

like image 21
Serge Wautier Avatar answered Oct 06 '22 20:10

Serge Wautier