how use vectors with constructor?

i have these class 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
form( const form &forminstance)
        {
            setParent(GetParent(forminstance.hwnd));
            height=forminstance.height;
            left=forminstance.left;
            top=forminstance.top;
            width=forminstance.width;
            /*setTextAlignment(forminstance.intTextAlignment);
            intMouseHover=forminstance.intMouseHover;
            setAutoSize(forminstance.blnAutoSize);
            setEnable(forminstance.blnEnable);
            setTabStop(forminstance.blnTabStop);
            setTextWrap(forminstance.blnTextWrap);
            setTransparent(forminstance.blnTransparent);
            setVisible(forminstance.blnVisible);
            setBackColor(forminstance.clrBackColor);
            setTextColor(forminstance.clrTextColor);
            strCaption=forminstance.strCaption;
            hCursor=forminstance.hCursor;
            SetClassLong(hwnd,    // window handle
                GCL_HCURSOR,      // change cursor
                (LONG) hCursor);
            HFONT a=(HFONT)SendMessage(forminstance.hwnd, WM_GETFONT, 0, 0);
            setFont(a);*/
        }

and these:
1
2
3
4
5
6
7
8
9
10
11
12
form(string caption = "", HWND parent=HWND_DESKTOP)
        {
            if(hwnd==NULL)
            {
                ++FormCount;
                if(caption=="")
                    strCaption = strCaption + to_string(FormCount);
                else
                    strCaption=caption;
                setParent(parent);
            }
        }

heres using the vectors:
1
2
3
vector<form> frmmdi;
frmmdi.push_back("hello world");
    frmmdi.parent=fb;

the form are showed and then deleted.
but the:
vector<form> frmmdi(2);
so when i use the push_back(), can i use the form constructor?
No you cannot, however you can use vector::emplace_back

http://www.cplusplus.com/reference/vector/vector/emplace_back/
Last edited on
done:
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
vector<std::unique_ptr<form>> frmmdi;

void CreateDMI(vector<std::unique_ptr<form>> &frmmdiwindow, HWND parent)
{

    frmmdiwindow.emplace_back(new form("hello world"));
    frmmdiwindow[frmmdiwindow.size()-1]->Parent=parent;
    frmmdiwindow[frmmdiwindow.size()-1]->Close=[&frmmdiwindow]()
    {
        HWND hwndMDI=GetForegroundWindow();

        for(int i=0; i<=frmmdiwindow.size()-1; i++)
        {
            HWND hwndMDITemp=frmmdiwindow[i]->GetHWND();
            if (hwndMDITemp==hwndMDI)
            {
                frmmdiwindow.erase(frmmdiwindow.begin()+i);
                break;
            }
        }
    };
}

//....
btnarray[0].MouseClick=[]()
    {
        fb.setAutoScroll(true);
        CreateDMI(frmmdi,fb);
    };

now see when a form is closed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
case WM_CLOSE:
                {
                    inst->Close();
                    FormCount-=FormCount;
                    DebugText((string)"form count: " + to_string(FormCount));
                    DestroyWindow(inst->hwnd);
                }
                break;

                case WM_DESTROY:
                {
                    inst->Destroy();
                    joyReleaseCapture(JOYSTICKID1);
                    KillTimer(inst->hwnd, JoystickTimer);
                    KillTimer(inst->hwnd,KeyBoardTimer);
                    KillTimer(inst->hwnd,timerid);


                    if(FormCount<0)
                        End();
                }
                break;

when i create a form:
1
2
3
4
5
6
7
8
9
10
11
12
13
form(const string caption = "", const HWND parent=HWND_DESKTOP)
        {
            if(hwnd==NULL)
            {
                ++FormCount;
                DebugText((string)"form count: " + to_string(FormCount));
                if(caption=="")
                    strCaption = strCaption + to_string(FormCount);
                else
                    strCaption=caption;
                setParent(parent);
            }
        }

i'm debugging the FormCount. when the form it's created seems ok. but when it's destroyed, the results aren't correct :(
can you advice me?
resolved with 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
28
29
30
31
32
33
34
35
36
37
38
39
~form()
        {
            if(hwnd!=NULL)
            {
                FormCount=FormCount-1;
                DestroyWindow(hwnd);
            }

        }
////.............
case WM_CLOSE:
                {
                    inst->Close();
                    if(IsWindowVisible(inst->hwnd)==TRUE)
                        FormCount=FormCount-1;
                    if(FormCount>=1)
                    {
                        ChildWindows ws(inst->hwnd);
                        for(int i=0; i<ws.count(); i++)
                        {
                            SendMessage(ws[i], WM_CLOSE,0,0);
                        }
                    }
                    DestroyWindow(inst->hwnd);
                }
                break;

                case WM_DESTROY:
                {
                    inst->Destroy();
                    joyReleaseCapture(JOYSTICKID1);
                    KillTimer(inst->hwnd, JoystickTimer);
                    KillTimer(inst->hwnd,KeyBoardTimer);
                    KillTimer(inst->hwnd,timerid);
                    if(FormCount<1)
                        End();
                    inst->hwnd=NULL;
                }
                break;

give me 1 advice: is good, after close all forms, end the program automatic or is better using the command for end it?
Topic archived. No new replies allowed.