Thread: How to clear the window content and print new content ?

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    84

    How to clear the window content and print new content ?

    Hi,

    I'm trying to develop a simple program to do the following:
    1. Develop main menu, then list some buttons and each button print a content.
    2. There should be a 'main_menu' button on page1 and page2 to get me back to the 'main_menu'.


    The problem is that when I press the 'main_menu' button, I get content from page1 and page2 on the 'main_menu' page.

    I just need to know how to clear the window and print new content.


    Here's my code:
    Code:
    #include <windows.h>
    #include <stdint.h>
    
    #define FILE_MENU_NEW   1   // File menu item to be used as window numbered functions
    #define FILE_MENU_OPEN  2   // File menu item to be used as window numbered functions
    #define FILE_MENU_EXIT  3   // File menu item to be used as window numbered functions
    
    #define HOME_PAGE       4
    #define PAGE1           5
    #define PAGE2           6
    
    LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
    void main_page(HWND hWnd);
    void page1(HWND hWnd);
    void page2(HWND hWnd);
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR args, int nCmdShow){
    
        WNDCLASSW wc = {0};
    
        wc.hbrBackground = (HBRUSH)COLOR_WINDOW;    // color of the window
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);   // shape of the cursor
        wc.hInstance = hInstance;                   // pass it windows main system handler
        wc.lpszClassName = L"myWindowClass";        // name of the window class
        wc.lpfnWndProc = WindowProcedure;
    
        if(!RegisterClassW(&wc)){
            return -1;
        }
    
        CreateWindowW(L"myWindowClass", L"My Window", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 1920/4, 1080/8, 320, 480,
            NULL,       // Parent window
            NULL,       // Menu
            NULL,       // Instance handle
            NULL        // Additional application data
            );
    
        MSG msg = {0};
    
        while(GetMessage(&msg, NULL, (uint64_t)NULL, (uint64_t)NULL)){
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    
        return 0;
    }
    
    LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp){
        switch(msg){
    
        case WM_COMMAND:
            switch(wp){
    
            case FILE_MENU_NEW:
                MessageBeep(MB_ICONINFORMATION);
                break;
            case FILE_MENU_OPEN:
                MessageBeep(MB_OK);
                break;
            case FILE_MENU_EXIT:
                break;
    
            case HOME_PAGE:
                main_page(hWnd);
                break;
    
            case PAGE1:
                page1(hWnd);
                break;
            case PAGE2:
                page2(hWnd);
                break;
            }
    
            break;
    
        case WM_CREATE:
            main_page(hWnd);
            break;
    
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
    
        case WM_QUIT:
            break;
    
        default:
            return DefWindowProcW(hWnd, msg, wp, lp);
    
        }
        return 0;
    }
    
    void main_page(HWND hWnd){
        CreateWindowW(L"Static", L"main page", WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER
                      , 65, 10, 180, 20, hWnd, NULL, NULL, NULL);
    
        CreateWindowW(L"Button", L"Page1", WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER
                      , 65, 50, 180, 30, hWnd, (HMENU)PAGE1, NULL, NULL);
        CreateWindowW(L"Button", L"Page2", WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER
                      , 65, 90, 180, 30, hWnd, (HMENU)PAGE2, NULL, NULL);
    }
    
    void page1(HWND hWnd){
        CreateWindowW(L"Static", L"Page1", WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER
                      , 65, 10, 180, 20, hWnd, NULL, NULL, NULL);
    
        CreateWindowW(L"Button", L"P1 - button1", WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER
                      , 65, 50, 180, 30, hWnd, NULL, NULL, NULL);
        CreateWindowW(L"Button", L"P1 - button2", WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER
                      , 65, 90, 180, 30, hWnd, NULL, NULL, NULL);
        CreateWindowW(L"Button", L"P1 - button3", WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER
                      , 65, 130, 180, 30, hWnd, NULL, NULL, NULL);
    
        CreateWindowW(L"Button", L"Main Page - P1", WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER
                      , 65, 310, 180, 30, hWnd, (HMENU)HOME_PAGE, NULL, NULL);
    }
    
    void page2(HWND hWnd){
        CreateWindowW(L"Static", L"Page2", WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER
                      , 65, 10, 180, 20, hWnd, NULL, NULL, NULL);
    
        CreateWindowW(L"Button", L"P2 - button1", WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER
                      , 65, 50, 180, 30, hWnd, NULL, NULL, NULL);
        CreateWindowW(L"Button", L"P2 - button2", WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER
                      , 65, 90, 180, 30, hWnd, NULL, NULL, NULL);
        CreateWindowW(L"Button", L"P2 - button3", WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER
                      , 65, 130, 180, 30, hWnd, NULL, NULL, NULL);
    
        CreateWindowW(L"Button", L"Main Page - P2", WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER
                      , 65, 310, 180, 30, hWnd, (HMENU)HOME_PAGE, NULL, NULL);
    }
    Last edited by wolfrose; 11-22-2023 at 04:49 AM. Reason: editing the code

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    I haven't used windows in forever, but if I remember correctly, a properly-designed window procedure has a WM_PAINT event to draw the window, and a function called InvalidateRect is used to "clear the screen" and redraw the window.
    So something like this:
    Code:
    LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) {
     
        static int current_page = 0;
     
        switch (msg) {
     
        case WM_COMMAND:
            switch(wp){
     
            ...
     
            case HOME_PAGE:
                current_page = 0;
                InvalidateRect(hwnd, NULL, TRUE);
                break;
            case PAGE1:
                current_page = 1;
                InvalidateRect(hwnd, NULL, TRUE);
                break;
            case PAGE2:
                current_page = 2;
                InvalidateRect(hwnd, NULL, TRUE);
                break;
            }
            break;
     
        case WM_PAINT:
            {
                PAINTSTRUCT ps;
                HDC hdc = BeginPaint(hwnd, &ps);
    
    
                switch (current_page) {
                case 0:
                    main_page(hwnd);
                    break;
                case 1:
                    page1(hwnd);
                    break;
                case 2:
                    page2(hwnd);
                    break;
                }
                EndPaint(hwnd, &ps);
            }
            break;
         ...
     
        }
    }
    Last edited by john.c; 11-22-2023 at 09:24 AM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    638
    What you need to do OP is save the all the handles returned by the CreateWindows, then when switching pages, ShowWindow(handle, SW_HIDE) all the handles for the to-be-hidden page and ShowWindow(handle, SW_SHOW) all the handles for the new page. You can either create all your controls hidden at startup (no WS_VISIBLE) or check in your page functions whether they are already created before creating them.

    Usually people use a Tab control or some container window for applications with pages of controls, because then you only have to hide the container rather than all the controls within individually. Both of those make your app more complex though, the container approach requires subclassing or a second window class and the tab control has its own quirks.

    I haven't used windows in forever
    It shows. That's horrendous advice

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    It shows. That's horrendous advice

    I realized it wasn't a good idea after I posted that it since it would cause the windows to be created over and over again. I thought of tabs but couldn't remember anything about how it was done.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    Jan 2016
    Posts
    84
    Quote Originally Posted by john.c View Post
    I haven't used windows in forever, but if I remember correctly, a properly-designed window procedure has a WM_PAINT event to draw the window, and a function called InvalidateRect is used to "clear the screen" and redraw the window.
    So something like this:
    Code:
    LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) {
     
        static int current_page = 0;
     
        switch (msg) {
     
        case WM_COMMAND:
            switch(wp){
     
            ...
     
            case HOME_PAGE:
                current_page = 0;
                InvalidateRect(hwnd, NULL, TRUE);
                break;
            case PAGE1:
                current_page = 1;
                InvalidateRect(hwnd, NULL, TRUE);
                break;
            case PAGE2:
                current_page = 2;
                InvalidateRect(hwnd, NULL, TRUE);
                break;
            }
            break;
     
        case WM_PAINT:
            {
                PAINTSTRUCT ps;
                HDC hdc = BeginPaint(hwnd, &ps);
    
    
                switch (current_page) {
                case 0:
                    main_page(hwnd);
                    break;
                case 1:
                    page1(hwnd);
                    break;
                case 2:
                    page2(hwnd);
                    break;
                }
                EndPaint(hwnd, &ps);
            }
            break;
         ...
     
        }
    }
    Didn't work, when I press any button the content disappears, and it doesn't print anything after.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-07-2006, 03:38 PM
  2. Receiving content
    By maxorator in forum Windows Programming
    Replies: 2
    Last Post: 12-18-2005, 03:17 AM
  3. Capturing window content
    By leojose in forum Windows Programming
    Replies: 4
    Last Post: 06-30-2005, 08:30 AM
  4. Replies: 0
    Last Post: 06-24-2004, 02:03 PM

Tags for this Thread