Using EnumChildWindows
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Using EnumChildWindows

  1. #1
    Join Date
    Sep 2003
    Posts
    72

    Using EnumChildWindows

    I want to be able to access all my windows that are currently visible in my application. I tried using
    EnumChildWindows((HWND)::AfxGetMainWnd(WNDENUMPROC)EnumChildProc, 0))
    but it fails, so my first question is what's wrong with my call to
    EnumChildWindows & secondly can you please provide sample code on how to call it correctly & get the windows from it.

    Thanks in advance

  2. #2
    Join Date
    May 2002
    Posts
    159
    There are some good code examples in MSDN but here is a summary of what you can do to enumerate child windows.

    Hope this is helpful for you.

    [ccode]
    void CMyClass::MyFunction ( void )
    {
    EnumChildWindows ( AfxGetMainWnd() -> m_hWnd,
    MyEnumProc,
    NULL ); // 3rd parameters
    // non-NULL only if you want to
    //pass something to the callback
    // function
    }

    BOOL CALLBACK CMyClass::MyEnumProc(HWND hWnd, LPARAM lParam)
    {
    // Do your work here.
    TRACE ("Enumerated Child Window : %x\n", hWnd);
    }

    [/ccode]

  3. #3
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940
    I tend to put it into a class e.g.

    Code:
    class CEnumWindows
    {
    public:
        CEnumWindows(HWND hWndParent);
        virtual ~CEnumWindows();
    
        HWND Window(int nIndex) const { return (HWND)m_apWindowHandles.GetAt(nIndex); }
        int Count() const { return m_apWindowHandles.GetCount(); }
    
    private:
        BOOL CALLBACK TheEnumProc(HWND hWnd, LPARAM lParam);
    
        CPtrArray m_apWindowHandles;
    } ;
    
    // in.cpp
    CEnumWindows::CEnumWindows(HWND hWndParent)
    {
        EnumChildWindows ( hWndParent, TheEnumProc, (LPARAM) &this );
    }
    
    CEnumWindows::~CEnumWindows()
    {
    }
    
    BOOL CALLBACK CEnumWindows::TheEnumProc(HWND hWnd, LPARAM lParam)
    {
        CEnumWindows *pEnumWindows = (CEnumWindows *)lParam;
        pEnumWindows->m_apWindowHandles.Add((void *)hWnd);
        return TRUE;
    }
    Get the idea ? All nice and neat. Then all you have to do is :

    Code:
    CEnumWindows ahwndChildWindows(NULL); // for desktop
    
    for (int nWindow = 0 ; nWindow < ahwndChildWindows.Count(); nWindow++)
    {
        HWND hWndWindow = ahwndChildWindows.Window(nWindow);
    
        // do whatever
    }
    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  4. #4
    Join Date
    Sep 2003
    Posts
    72
    Thanks alot I now get into the call back function
    BOOL CALLBACK CMyClass::MyEnumProc(HWND hWnd, LPARAM lParam)
    {
    // Do your work here.
    TRACE ("Enumerated Child Window : %x\n", hWnd);
    }
    but I want to be able to access the windows ID to map it to the resource id it has when it gets created or something that will let me know which window it is. what function do I call to get specific information on this child window.

    Thanks

  5. #5
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940
    Use GetDlgCtrlID(hwnd) to get the control ID. For items on a dialog or form this control ID is the same as the resource ID.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  6. #6
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940
    And also I far prefer to use a seperate class to encapsulate all of this. Why ? Reuse, reuse !

    What happens if you want to do all of this again somewhere else ? Duplication of code and methods that's what.

    Do youself a favour, leave the Ctrl+C, Ctrl+V keys alone and use a class.

    If you have a class you can use it in both locations (and if you're very sensible you'll put it in a shared library). And you won't have to duplicate any code to do it.

    There are so many advantages to doing it this way (never mind it's the object oriented design way of doing things) that I'm not even going to bother to list them.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  7. #7
    Join Date
    May 2002
    Posts
    159
    Hi, see the post by Darwen. Class encapsulation is always a nice design.

    To get the resourceID, you can try using GetDlgCtrlID (). Of course this could be a problem if you are trying to enum a window that you did not create. You will have to use other methods to indentify the controls because you will not know the resource id.

    Hope this is what you are looking for.

    Hujan

  8. #8
    Join Date
    Sep 2003
    Posts
    72
    yes I think to use it as a separate class is a very smart idea, which I think I will do, but let me first get it working when I tried calling on my hwnd CWnd::GetDlgCtrlID it fails I am not interested on the ID's of the controls within this window all I have to know is the actual window name or window id not it's control for what I am doing I don't need it's specific controls.

  9. #9
    Join Date
    May 2002
    Posts
    159
    I do not know of a good way to uniquely indentify controls in another program.

    I think the only information you can get is the class type, window name ( if any ) and not much more. I have had this trouble before. Use the Windows Spy tool to see what you can get from the control. I think what you see if all you can get from your program. This may be enough depending on what you want to do with the controls.

    Good luck.

    Hujan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center