Redraw window after DestroyWindow

Willard 45 Reputation points
2023-04-15T06:39:01.6433333+00:00

Currently I am trying to destroy a window (button) and redraw the main window so that I may redraw new buttons as if changing menus. I found on stackoverflow someone mention using the BeginPaint to redraw the window so it can show the button being deleted after using DeleteWindow. Right now I do not fully understand handles (as far as I understand are identifiers) and I haven't found anything in MSDN to expand on PAINTSTRUCT outside of the window creation walkthrough. The code is something for me to mess with to learn win32 so I have not implemented functions to deal with buttons on their own.

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{

    switch (uMsg)
    {
    case WM_CREATE:
    {
        HWND hwndButton = CreateWindow(
            L"BUTTON",  // Predefined class; Unicode assumed 
            L"Start",      // Button text 
            WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // Styles 
            25,         // x position 
            375,         // y position 
            100,        // Button width
            50,        // Button height
            hwnd,     // Parent window
            (HMENU) 1000,
            NULL,       // No menu.
            (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE),
            NULL);      // Pointer not needed.

        HWND hwndButton2 = CreateWindow(
            L"BUTTON",  // Predefined class; Unicode assumed 
            L"Second",      // Button text 
            WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // Styles 
            150,         // x position 
            375,         // y position 
            100,        // Button width
            50,        // Button height
            hwnd,     // Parent window
            (HMENU) 1002,
            NULL,       // No menu.
            (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE),
            NULL);      // Pointer not needed.
    }
        break;

    case WM_COMMAND:
        {
        switch (LOWORD(wParam))
            {
            case 1000:
                MessageBox(hwnd, L"Do you wish to return?", L"Warning!", MB_YESNO | MB_ICONINFORMATION);
                return 0;

            case 1002:

                //Bool DestroyWindow(hwnd);
                //SendMessage(hwnd, WM_CLOSE,0 ,0 );

                HWND hwndButton3 = CreateWindow(
                    L"BUTTON",  // Predefined class; Unicode assumed 
                    L"Third",      // Button text 
                    WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // Styles 
                    250,         // x position 
                    375,         // y position 
                    100,        // Button width
                    50,        // Button height
                    hwnd,     // Parent window
                    (HMENU)1003,
                    NULL,       // No menu.
                    (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE),
                    NULL);      // Pointer not needed.

//Paint here?

                return 0;

            }

        }
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;

    case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);

        // All painting occurs here, between BeginPaint and EndPaint.

        FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));

        EndPaint(hwnd, &ps);
    }
    return 0;

Windows development | Windows API - Win32
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2023-04-15T16:22:57.9633333+00:00

    I think that if you destroy or hide a button, then usually the parent window will be updated automatically. (The WM_PAINT message will be sent by the system). What button do you want to hide?

    For example, to remove the second button when you click it:

    . . .
    case 1002:
       DestroyWindow( (HWND)lParam );
    
       HWND hwndButton3 = CreateWindow( . . .
    

    You can also write DestroyWindow( hwndButton2 ) if hwndButton2 is not a local variable.

    3 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.