building my arrays class

Pages: 12
see my debug info:

"8
150 0 parent: two
200 1 parent:
250 2 parent:
300 3 parent: two
350 4 parent: two
400 5 parent: two
450 6 parent: two
500 7 parent: two"

like you see, the button 1 and 2 have a diferent parent or NULL. so maybe i lose the parent on constructor or something. what you can tell me?
That means your button class is likely to have a bug somewhere in default constructor.

Your class looks fine (as you use it now. There are problems, but you won't encounter them in your sample program). The problem must be in button class.
can you help me find the problem?
but why only 2 buttons have problem? why not all of 8?
why not all of 8?
You ahve probably triggered undefined behavior somewhere and now your class is relying on memory being in specific state on construction.
For example, what is the value of hwnd member on construction?
when the 1st form is created:
1
2
3
4
case WM_CREATE:
                {
                    if(WindowMain == NULL || WindowMain ==GetDesktopWindow())
                        WindowMain = inst->hwnd;

on button constructor:
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
button(string caption="", HWND parent=WindowMain)
    {
        ++ButtonCount;
        if(caption=="")
            strCaption=strCaption + to_string(ButtonCount);
        else
            strCaption=caption;
        setParent(parent);
        altkey=GettingAltKey(strCaption);
        if(altkey!=-1)
            RegisterHotKey(hwnd,altmessage,MOD_ALT,altkey);
    }
void setParent(HWND parent=WindowMain)
    {
        if (hwnd==NULL)
        {
            WNDCLASS ButtonClass;
            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
        {
            SetParent(hwnd,parent);
        }

        /*RECT a;
        GetClientRect(hwnd,&a);
        intTop=a.top;
        intLeft=a.left;
        intWidth=a.right-a.left;
        intHeight=a.bottom-a.top;
        InvalidateRect(hwnd,NULL,true);//i add these line for fix that
        UpdateWindow(hwnd);
        SetWindowPos(hwnd, 0,intLeft ,  intTop, intWidth, intHeight,
                SWP_NOZORDER|SWP_NOACTIVATE|
                SWP_DRAWFRAME | SWP_FRAMECHANGED|SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOSIZE);*/


        mousestoped.timerprocedure=[this]()
        {
            static POINT Location, PreviousLocation={0};
            if(dcaButton==DrawControlsAction::MouseEnter)
            {
               GetCursorPos(&Location);
                //testing is previous position is igual to location
                //if they are the same, so the mouse is stop
                if ((Location.x == PreviousLocation.x) && (Location.y == PreviousLocation.y))
                {
                    MouseStoped();
                }
                else
                {
                    MouseButtons MBButtons;
                    bool blControl=false;
                    bool blShift=false;


                    if(GetKeyState(MK_CONTROL) & 0x8000)
                    {
                        blControl=true;
                    }

                    if(GetKeyState(MK_SHIFT) & 0x8000)
                    {
                        blShift=true;
                    }

                    if(GetKeyState(MK_LBUTTON) & 0x8000)
                    {
                        MBButtons=Left;
                        dcaButton=DrawControlsAction::MouseClick;
                        InvalidateRect(hwnd,nullptr,FALSE);
                    }
                    else if(GetKeyState(MK_RBUTTON) & 0x8000)
                        MBButtons=Right;
                    else if(GetKeyState(MK_MBUTTON) & 0x8000)
                        MBButtons=Middle;
                    else  if(GetKeyState(MK_XBUTTON1) & 0x8000)
                        MBButtons=X1;
                    else  if(GetKeyState(MK_XBUTTON2) & 0x8000)
                        MBButtons=X2;
                    MouseMove(MBButtons,blControl,blShift,Location.x,Location.y);
                }
            }
            InvalidateRect(hwnd,NULL,TRUE);
            PreviousLocation = Location;
        };
        mousestoped.Interval=100;
        mousestoped.Start();
        setAutoSize(true);
    }
thanks for all
i fix it ;)
i inicialite the hwnd to NULL then i did:
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
class button
{
private:
    static int ButtonCount;
    image imgtest;
    HCURSOR hCursor=(HCURSOR)LoadCursor(GetModuleHandle(NULL),IDC_APPSTARTING);
    string strCaption="Button ";
    string strCursor="";
    color clrTextColor=0;
    color clrBackColor=0;
    bool blnTransparent=false;
    bool blnAutoSize=false;
    bool blnTabStop=true;
    bool blnTextWrap=false;
    bool blnMouseEnter=false;
    int intTop=0;
    int intLeft=0;
    int intWidth=70;
    int intHeight=30;
    int intBorder=0;
    int intMouseHover=HOVER_DEFAULT;
    HWND hwnd=NULL;//the diference
    bool blnVisible=true;
    bool blnEnable=true;
    int intTextAlignment=0;
    char altkey=-1;
    UINT altmessage=0;
    Timer animation;
    Timer mousestoped;
    DrawControlsAction dcaButton=DrawControlsAction::Normal;
    CHOOSEFONT chFont;

and:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
button(string caption="", HWND parent=WindowMain)
    {
        if(hwnd==NULL)
        {
            ++ButtonCount;
            if(caption=="")
                strCaption=strCaption + to_string(ButtonCount);
            else
                strCaption=caption;
            setParent(parent);
            altkey=GettingAltKey(strCaption);
            if(altkey!=-1)
                RegisterHotKey(hwnd,altmessage,MOD_ALT,altkey);
        }

    }

correct me too: is the best change these too right?
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
button( const button &buttoninstance)
    {
        setParent(GetParent(buttoninstance.hwnd));
        setHeight(buttoninstance.intHeight);
        setLeft(buttoninstance.intLeft);
        setTop(buttoninstance.intTop);
        setWidth(buttoninstance.intWidth);
        setTextAlignment(buttoninstance.intTextAlignment);
        intMouseHover=buttoninstance.intMouseHover;
        setAutoSize(buttoninstance.blnAutoSize);
        setEnable(buttoninstance.blnEnable);
        setTabStop(buttoninstance.blnTabStop);
        setTextWrap(buttoninstance.blnTextWrap);
        setTransparent(buttoninstance.blnTransparent);
        setVisible(buttoninstance.blnVisible);
        setBackColor(buttoninstance.clrBackColor);
        setTextColor(buttoninstance.clrTextColor);
        strCaption=buttoninstance.strCaption;
        hCursor=buttoninstance.hCursor;
        SetClassLong(hwnd,    // window handle
            GCL_HCURSOR,      // change cursor
            (LONG) hCursor);
        //HFONT a=(HFONT)SendMessage(buttoninstance.hwnd, WM_GETFONT, 0, 0);
//        setFont(a);
    }


now, i belive, that i understand why some buttons was showed and others don't. sometimes the hwnd was valid and others don't
Last edited on
If it works, then it is better than before.
sometimes the hwnd was valid and others don't
Yes. most of the time memory where hwnd resides is not null and huge check in setParent will fail, sending you to the else branch where parent is being set. Rarely it will land in zero memory area and condition passes.
yes and now the vectors work too ;)
about button( const button &buttoninstance), it's ok too?
maybe needs more work for all properties, but what you think?
I have no idea. It is impossible to say without extensive knowledge of button class.
Just make sure that all members are copied, classes do not share any dynamic data and other resources.
thanks for all. thank you
Topic archived. No new replies allowed.
Pages: 12