

The parentHandle parameter is optional, taking in a zero if omitted. The main search function of the WindowFinder class. Members that'll hold the search criterias while searching. Public delegate bool FoundWindowCallback( int hWnd) private event FoundWindowCallback foundWindow The boolean // return value of the delegate matches the functionality of the PChildCallBack delegate function. This is an event that is run each time a window was found that matches the search criterias. private delegate bool PChildCallBack( int hWnd, int lParam) The PChildCallBack delegate that we used with EnumWindows.

Private static extern Boolean EnumChildWindows( int hWndParent, PChildCallBack lpEnumFunc, int lParam) Otherwise it'll only return windows // whose parent window handle matches the hWndParent parameter.


If this is NULL or zero, it works just like EnumWindows. EnumChildWindows works just like EnumWindows, except we can provide a parameter that specifies the parent // window handle. Private static extern int GetWindowThreadProcessId( int hWnd, out int lpdwProcessId) / /// A class used for finding windows based upon their class, title, process and parent window handle. Now we can write out the information we have on the window.Ĭonsole.WriteLine( "Class : " + sbClass) SendMessage(handle, WM_GETTEXT, sbText.Capacity, sbText) StringBuilder sbText = new StringBuilder(txtLength + 1) int txtLength = SendMessage(handle, WM_GETTEXTLENGTH, 0, 0) GetClassName(handle, sbClass, sbClass.Capacity) StringBuilder sbClass = new StringBuilder( 256) private static void printWindowInfo( int handle) Prints basic properties of a window, uses function already used in previous blogs. private static bool foundWindow( int handle) Gets called each time a window is found by the WindowFinder class. Wf.FindWindows( 0, null, new Regex( " - Microsoft Visual Studio"), new Regex( "devenv"), new WindowFinder.FoundWindowCallback(foundWindow)) Wf.FindWindows( 0, null, null, new Regex( "iexplore"), new WindowFinder.FoundWindowCallback(foundWindow)) Main entrypoint function static void Main( string args) Private static extern Int32 SendMessage( int hWnd, int Msg, int wParam, int lParam) Private static extern Int32 SendMessage( int hWnd, int Msg, int wParam, StringBuilder lParam) Private static extern int GetWindowText( int hWnd, StringBuilder text, int count) Private static extern void GetClassName( int hWnd, StringBuilder s, int nMaxCount) Win32 functions that have all been used in previous blogs. You can search for any number of these parameters at the same time, using regular expressions for all string matches to provide optimal flexibility. This time I present to you a class that greatly simplifies the process of searching for specific windows, types of windows, windows belonging to a specific process, having a specific text. Last time I made an example of how to enumerate windows.
