win32 - how can i set the button parent?

heres my SetParent():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
void setParent(HWND parent=WindowMain)
    {
        static WNDCLASS ButtonClass;
        if (hwnd==NULL)
        {

            HINSTANCE mod = (HINSTANCE)GetModuleHandle(NULL);

            ZeroMemory(&ButtonClass, sizeof(WNDCLASS));
            GetClassInfo(mod, TEXT("BUTTON"), &ButtonClass);

            ButtonClass.hInstance = mod;
            ButtonClass.lpszClassName = TEXT("CBUTTON");
            ButtonClass.style=CS_DBLCLKS;


            // store the old WNDPROC of the button window class
            SetProp(parent, buttonpropname, (HANDLE)ButtonClass.lpfnWndProc);
            // replace it with local WNDPROC

            ButtonClass.lpfnWndProc = WndProcButton;
            ButtonClass.hbrBackground = (HBRUSH)GetStockBrush(NULL_BRUSH);

            // register the new window class"
            RegisterClass(&ButtonClass);

            hwnd = CreateWindowEx(
                0,
                TEXT("CBUTTON"),//these must be the same of  ButtonClass.lpszClassName.. registed class
                strCaption.c_str(),
                WS_VISIBLE | WS_CHILD |WS_TABSTOP| BS_OWNERDRAW | BS_NOTIFY | WS_CLIPSIBLINGS, //onerdraw for add images
                intLeft, intTop, intWidth, intHeight,
                parent,
                NULL,
                mod,
                this);

            if (hwnd == NULL)
                MessageBox(NULL, "Can't create the control", "error", MB_OK);

            if (SetProp(hwnd, buttonclassprop, (HANDLE)this) == 0)
                MessageBox(NULL, "can't set the class property", "error", MB_OK);
            clrBackColor= GetBkColor(GetDC(GetParent(hwnd)));
            clrTextColor = GetTextColor(GetDC(hwnd));
        }
        else
        {
            if(hwnd!=NULL)
            {
                SetProp(GetParent(hwnd), buttonpropname, (HANDLE)NULL);
                SetProp(parent, buttonpropname, (HANDLE)ButtonClass.lpfnWndProc);
                SetParent(hwnd,parent);
            }

            //SendMessage(parent,WM_CHILDTOPARENT,(WPARAM)hwnd, (LPARAM)NULL);
        }
    }

like you see i use SetProp() for send the hwnd button to parent procedure.
but i get errors, because can't find the parent window message procedure when i set the parent :(
can anyone explain to me what i'm doing wrong?
the code is correct. except the 'else':
1
2
3
4
5
6
7
else
        {
            if(hwnd!=NULL && GetParent(hwnd)!=parent)
            {
                SetParent(hwnd,parent);
            }
        }

and see these code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
arrays<button> btnarray(8);


int WinMain()
{
    //OutputDebugString("hello world");
    //int arraysize=ArraySize(btnarray, button);//geting the array size
    btnarray.resize(10);
    int arraysize=btnarray.size();
    DebugText(to_string(arraysize));
    int i=0;
    for (i; i<arraysize; i++)
    {
        int inttop=150 + (i*50);

        btnarray[i].setTop(inttop);
        btnarray[i].setLeft(0);
        btnarray[i].setText(to_string(i));
        btnarray[i].MouseClick=[i]()
        {
            MessageBox(btnarray[i].getText());

        };
        string windowtext;
        GetWindowString(btnarray[i].getParent(),windowtext);
        DebugText((string)to_string(inttop) + "\t" + to_string(i) + "\tparent: " + windowtext);
    }

- i can't forget the '[i]()' on lambda or i will get errors or a memory leak;
- i was doing several errors like close the window before send the button to another parent.
what i mean is: the code was fine, i was doing several errors without notice.
thanks for all
Topic archived. No new replies allowed.