简体   繁体   中英

win32 controls at Runtime

this may seem like a trivial question and one that the answer serves no apparent purpose but I'm asking purely out of the interest of knowledge and curiosity.

I'm writing a Win32 GUI application with no actual purpose other than to expand my programming knowledge with the Win32 API and I am wondering how I would go about creating controls(text fields and buttons in particular) after the program has been launched and WM_CREATE has been called.

I know that using UpdateWindow will send a WM_CREATE message to the window but if i am to understand correctly, this will just update the stuff already written in WM_CREATE within the source?

What I am after is a button which adds another button upon click and a button which adds a text field on click.

And, although not a control, another button that paints text with TextOut() or something on click.

I've been scouring google for the last 2 hours looking for this and had no luck, i've also viewed UpdateWindow() and RedrawWindow() which have provided no real help.

Im not asking for you guys to write any code for me, although that would be great:P Just a few pointers in the right direction and a bit of guidance would be wonderful

Thanks in Advance, Timmy

Also, I'm using C++ with VS10 and no .NET or MFC please, just pure C++ :D

Edit:

main.cpp, message loop:

case WM_CREATE:
hedit = ctrls->createTextArea(100, 50, 100, 20, ghInstance, hWnd, hEdit1);
hedit2 = ctrls->createBtn(200, 50, 100, 20, ghInstance, hWnd, "button", btn1);
return 0;

ctrls refers to controls class object and createTextArea and createBtn from this class follow:

HWND controls::createTextArea(int x, int y, int width, int height, HINSTANCE hInst, HWND parent, int id)
{
return CreateWindowEx(NULL,
            "Edit",
            "",
            WS_CHILD | WS_VISIBLE | ES_PASSWORD | WS_BORDER,
            x, y,
            width,height,
            parent, (HBRUSH)id,
            hInst,
            NULL);
}



HWND controls::createBtn(int x, int y, int width, int height, HINSTANCE hInst, HWND parent, LPCSTR btnText, int id)
{
    return CreateWindowEx(NULL,
            "Button",
            btnText,
            WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
            x, y,
            width, height,
            parent, (HBRUSH)id,
            hInst,
            NULL);
}

EDIT Two:

WM_COMMAND

    case WM_COMMAND:

        if( LOWORD(wParam) == btn1) // btn1 click
        {
            hBtn = CreateWindowEx(NULL,
                "Button",
                "Button2",
                WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
                400,50,100,20,
                hWnd, NULL,
                ghInstance,
                NULL);
            ShowWindow(hBtn, SW_SHOW);
            UpdateWindow(hWnd);
        }
            return 0;

the 'btn1' in if( LOWORD(wParam) == btn1) refers to the ID of the button im clicking to try and make another which is effectively created by the following code called via function:

CreateWindowEx(NULL,
            "Button",
            btnText,
            WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
            x, y,
            width, height,
            parent, (HMENU)id,
            hInst,
            NULL);

I know that using UpdateWindow will send a WM_CREATE message to the window

No, CreateWindow/Ex() will send that message. UpdateWindow will only force a WM_PAINT message to be dispatched and processed. If your main window doesn't become visible then you probably forgot to call ShowWindow().

Use the default code generated by the Win32 Project template as a guide. Verify it works first, add changes incrementally. Watch out for swallowed SEH exceptions when you write 32-bit code on a 64-bit operating system. Debug + Exceptions, tick the Thrown checkbox for "Win32 Exceptions".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM