i'm trying create a class for getting the child controls from it's parent, but without sucess :(

class ChildControls
{
private:
    vector<HWND> childcontrols;
    UINT childcontrolsindex;
    HWND windowparent;

public:
    ChildControls(const HWND parent)
    {
        windowparent=parent;
        //EnumChildWindows(parent, EnumChildProc, 0);
    }

    UINT childcontrolscount()
    {
        return childcontrolsindex;
    }

    HWND GetHWND(UINT index)
    {
        EnumChildWindows( windowparent,
                              [this]( HWND hwnd, LPARAM lParam )
                              {
                                    this->childcontrols.push_back(hwnd);
                                    this->childcontrolsindex = this->childcontrols.size(); // TODO:
                                    return TRUE;
                              },
                              0);


        if(index>=childcontrolsindex)
            index=childcontrolsindex-1;
        return childcontrols[index];
    }
}ChildControls;

EnumChildWindows() isn't correct with 3rd and 4th arguments :(
please can anyone advice me?

Recommended Answers

All 6 Replies

EnumChildWindows() isn't correct with 3rd and 4th arguments :(
I find this comment a little confusing because EnumChildWindows takes only 3 parameters, not 4.

The first parameter in your call to EnumChildWindows is the handle of the parent window.

The second parameter in your call to EnumChildWindows is a pointer to a callback function that will be called once for every child window (unless you bail out early, see below). I think that callback function should be a simple function, not a class method. So you should write your code in a function that is not part of your class. This function will receive (must accept) 2 parameters: the handle for the child window and a value that you specify when you call EnumChildWindows.
If your callback function returns TRUE then it will continue to be called for more child windows. If it returns FALSE then enumeration will stop and the callback function will not be called for any remaining child windows.

The third parameter in your call to EnumChildWindows is a value that will be passed as the 2nd parameter in each call to the callback function. Typically this parameter will hold a pointer to some data to be used by the callback function, so you can effectively pass anything you like really. In your case you might pass a pointer to the ChildControls instance calling EnumChildWindows, and simply have the callback function add its 1st parameter to a list of child window handles held by that instance. Your GetHWND method could then simply return the appropriate entry in that list.

commented: thanks for all +2

i have that function ouside of class, but i don't get correct results :(

BOOL CALLBACK EnumChildProc( HWND hwnd, LPARAM lParam );
class ChildControls
{
private:
    vector<HWND> childcontrols;
    UINT childcontrolsindex;
    UINT controlscount=0;
    HWND windowparent;
    friend BOOL EnumChildProc( HWND hwnd, LPARAM lParam );
public:

    ChildControls()
    {
        //nothing
    }

    ChildControls(const HWND parent)
    {
        windowparent=parent;
        //EnumChildWindows(parent, EnumChildProc, 0);
    }

    UINT childcontrolscount()
    {
        return controlscount;
    }

    HWND GetHWND(UINT index)
    {
        EnumChildWindows( windowparent,EnumChildProc,(LPARAM)this);


        if(index>=childcontrolsindex)
            index=childcontrolsindex-1;
        return childcontrols[index];
    }
};

BOOL EnumChildProc( HWND hwnd, LPARAM lParam )
{
    static UINT i=0;
    ChildControls *cc=(ChildControls *)lParam;

    cc->childcontrols.push_back(hwnd);
    cc->childcontrolsindex = cc->childcontrols.size(); // TODO:
    cc->controlscount=i;
    i=i+1;
    return TRUE;
}

Try making if(index>=childcontrolsindex) if(index>childcontrolsindex)?

my problem was with HWND parent ;)
let me ask more:
1 - why i can't do these with class(when i close it)?

}ChildControls;

i need avoid instances of that class. how can i do that?

Sorry, I don't have a C++ development environment installed to check this, but shouldn't you clear the childcontrols vector before you call EnumChildWindows? Also, do you need to maintain your own count in controlscount? Can't you use childcontrols.size directly?

Should the order of these 2 lines be reversed?

    cc->childcontrolsindex = cc->childcontrols.size(); // TODO:
    cc->controlscount=i;

With these lines in the current order, after you add the first child window handle controlscount is 0 rather than 1.

commented: thanks for that tip +0

SalmiSoft: you have right... thanks
heres the class updated:

BOOL CALLBACK EnumChildProc( HWND hwnd, LPARAM lParam );
class ChildControls
{
private:
    vector<HWND> childcontrols;
    HWND windowparent;
    friend BOOL EnumChildProc( HWND hwnd, LPARAM lParam );
public:

    ChildControls()
    {
        //nothing
    }

    ChildControls(const HWND parent)
    {
        windowparent=parent;
        //EnumChildWindows(parent, EnumChildProc, 0);
    }

    UINT childcontrolscount()
    {
        return childcontrols.size();
    }

    HWND GetHWND(UINT index,HWND parent)
    {
        windowparent=parent;
        EnumChildWindows( windowparent,EnumChildProc,(LPARAM)this);


        if(index>=childcontrols.size())
            index=childcontrols.size()-1;
        if(index<0)
            index=0;
        return childcontrols[index];
    }
};

BOOL EnumChildProc( HWND hwnd, LPARAM lParam )
{
    ChildControls *cc=(ChildControls *)lParam;
    cc->childcontrols.push_back(hwnd);
    return TRUE;
}

but how can i avoid instances from these class? or continue use the class name without the instance?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.