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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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