tags:

views:

2375

answers:

4

Hi,

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?

+4  A: 

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.

Serge - appTranslator
+3  A: 

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;
      // ...
   }
}
Daniel LeCheminant
Interesting. Should work in most cases. But what happens if a process has more than one top lavel window?
Serge - appTranslator
@Serge: Not exactly sure...
Daniel LeCheminant
Thanks! Wow, I never knew you could get around this and avoid Win32 API kludges.
Jonas
A: 

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);
ebattulga
A: 

Hi,

how to get the handle in case there is no title for the window? is there a way to use other attributes, like window contains a specific button/Text Box etc.. Also this is not the mainwindow for a process..

Any help would greatly help..

satya