Thread: Finding correct window handle

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    43

    Finding correct window handle

    Hello,

    I am trying to work out the correct way to print out a list of child windows (and child of child windows etc). So far I have done a few tests on the Windows Notepad program by using:

    Code:
    #include <windows.h>
    .....
    hwnd1 = FindWindow(NULL, "Untitled - Notepad");
    hwnd2 = GetWindow(hwnd1, GW_CHILD);
    which gets the main text box window.

    Could someone help me work out how to do a similar thing for more complex programs which have multiple child windows or sub-child windows.

    Thanks,
    Chris

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Yes, the EnumWindows function is tailor made for that task.

    You pass a callback function to it, in which you can simply call EnumChildWindows on all the window handles you get.


    Here's something I was playing with a long time ago, I'll just post it as is, so it might have a few errors, but it still demonstrates its use. The purpose of it was to find a notepad window and type the message in it using SendMessage.

    Code:
    #include <windows.h>
    #include <stdio.h>
    
    char message[] = "\n\nI can see you.\n\nI herd u like mudkipz.\n\n .. Is dat right?\n";
    
    BOOL CALLBACK EnumChildProc(HWND hWnd, LPARAM lParam)
    {
         char buffer[100] = {0};
         unsigned int i = 0;
         
         if(!hWnd)  return TRUE; 
         
         if(GetClassName(hWnd, buffer, 100) == 0) return TRUE;
         
         printf(" - %s\n", buffer);
         
         /*for(i = 0; i < strlen(message); i++)
         {
               SendMessage(hWnd, WM_CHAR, message[i], 0);
               
               sleep(200);
         }*/
         
    
         
         memset(buffer, 0, 100);
    }
    
    
    BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
    {
         char buffer[100] = {0};
         int i = 0;
         if(!hWnd) return TRUE;
         
         
         if(GetWindowText(hWnd, buffer, 100) == 0) return TRUE;
         
         
         
         printf("Child windows for '%s':\n", buffer);
         //SetForegroundWindow(hWnd);               
         EnumChildWindows(hWnd, EnumChildProc, 0);
         
         
                   
         
         
    
         memset(buffer, 0, 100);
    }
    
    
    int main(void)
    {
        
         //ShellExecute(NULL, "open", "notepad.exe", NULL, "C:\\Windows", SW_SHOW);
         
         sleep(500);
         EnumWindows(EnumWindowsProc, 0);
        
        
        
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  2. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  3. Adding colour & bmps to a Win 32 Window??
    By carey_sizer in forum Windows Programming
    Replies: 4
    Last Post: 09-04-2004, 05:55 PM
  4. my wndProc is out of scope
    By Raison in forum Windows Programming
    Replies: 35
    Last Post: 06-25-2004, 07:23 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM