Windows 10 How to run multiple pages on the same HWND in win32 api C code

eagle

New Member
Hi,

I'm trying to develop a simple project on a win32 api using C compiler.

I've been trying to draw multiple pages on the same window object, and whenever I press a button, it redraw the new page. But it turned out that win32 programming is much complex than I thought.

The problem is that the page isn't erased

My complete code, in the attachment.

Also I want to understand the main functionality of "WindowProcedure" and how it works.

There are different tools and I don't know how to use them correctly. And whether I should use one rather than the other. Like:

InvalidateRect(hWnd, NULL, TRUE);
UpdateWindow(hWnd);

Also how I should use "WM_PAINT", and whether I should use it or not according to my project application.

For example:

Here's my "WindowProcedure" content:

C:
#include <windows.h>
#include <stdint.h>
#include <stdbool.h>
#include "system_setup.h"
#include "control_page.h"

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:
            InvalidateRect(hWnd, NULL, TRUE);
            main_menu_page(hWnd);
            UpdateWindow(hWnd);
            break;

        case MOTORS_PAGE:
            InvalidateRect(hWnd, NULL, TRUE);
            motors_page(hWnd);
            UpdateWindow(hWnd);
            break;
        case ELECTRICAL_PAGE:
            InvalidateRect(hWnd, NULL, TRUE);
            electrical_page(hWnd);
            UpdateWindow(hWnd);
            break;
        case WEATHER_PAGE:
            InvalidateRect(hWnd, NULL, TRUE);
            weather_page(hWnd);
            UpdateWindow(hWnd);
            break;
        case LED_CONTROL_PAGE:
            InvalidateRect(hWnd, NULL, TRUE);
            led_control_page(hWnd);
            UpdateWindow(hWnd);
            break;
        case SERVO_MOTOR_PAGE:
            InvalidateRect(hWnd, NULL, TRUE);
            servo_motor_page(hWnd);
            UpdateWindow(hWnd);
            break;
        case DC_MOTOR_PAGE:
            InvalidateRect(hWnd, NULL, TRUE);
            dc_motor_page(hWnd);
            UpdateWindow(hWnd);
            break;
        }

        break;

    case WM_CREATE:
        main_menu_page(hWnd);
        break;

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

//            switch (current_page) {
//                case HOME_PAGE:
//                    main_menu_page(hWnd);
//                    break;
//                case MOTORS_PAGE:
//                    motors_page(hWnd);
//                    break;
//                case ELECTRICAL_PAGE:
//                    electrical_page(hWnd);
//                    break;
//                case WEATHER_PAGE:
//                    weather_page(hWnd);
//                    break;
//                case LED_CONTROL_PAGE:
//                    led_control_page(hWnd);
//                    break;
//                case SERVO_MOTOR_PAGE:
//                    servo_motor_page(hWnd);
//                    break;
//                case DC_MOTOR_PAGE:
//                    dc_motor_page(hWnd);
//                    break;
//            }


            EndPaint(hWnd, &ps);
        }
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    case WM_QUIT:
        break;

    default:
        return DefWindowProcW(hWnd, msg, wp, lp);
    }
    return 0;
}


As in the above code, I tried to run the page functions either in "WM_COMMAND" and "WM_PAINT", but couldn't solve this issue.
 

Attachments

  • windows_api_example_c.rar
    82.5 KB · Views: 0
From the code you provided, it seems like you are trying to draw different pages on the same window using the Win32 API in C. The problem you are facing is that the previous page is not being erased when a button is pressed to display a new page.

To solve this issue, you need to handle the WM_PAINT message correctly in your WindowProcedure function. The WM_PAINT message is sent to a window procedure when its client area needs to be repainted. In this case, you need to paint the current page when the window is being repainted.

Here's how you can modify your WM_PAINT message handling code:

```c
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);

// Clear the entire client area before drawing the new page
RECT rect;
GetClientRect(hWnd, &rect);
FillRect(hdc, &rect, (HBRUSH)(COLOR_WINDOW + 1));

// Draw the current page
switch (current_page) {
case HOME_PAGE:
main_menu_page(hWnd);
break;
case MOTORS_PAGE:
motors_page(hWnd);
break;
case ELECTRICAL_PAGE:
electrical_page(hWnd);
break;
case WEATHER_PAGE:
weather_page(hWnd);
break;
case LED_CONTROL_PAGE:
led_control_page(hWnd);
break;
case SERVO_MOTOR_PAGE:
servo_motor_page(hWnd);
break;
case DC_MOTOR_PAGE:
dc_motor_page(hWnd);
break;
}

EndPaint(hWnd, &ps);
}
break;
```

In this modified code, the first step is to clear the entire client area using the FillRect function. This ensures that the previous page is erased before drawing the new page.

After clearing the client area, you can then draw the current page based on the value of the current_page variable.

Regarding the usage of InvalidateRect and UpdateWindow functions, you only need to call these functions when you want to force an immediate update of the window. In your case, since you handle the WM_PAINT message to redraw the pages, you can remove the calls to these functions in the WM_COMMAND messages.

Additionally, the main functionality of the WindowProcedure function is to handle various messages sent to the window, such as mouse clicks, keyboard input, window resizing, etc. The function receives the HWND (handle to the window), the message code (msg), and additional parameters (wp and lp) that depend on the specific message. You need to switch on the message code and handle the messages that are relevant to your application.

I hope this helps you understand the main functionality of the WindowProcedure function and how to correctly handle the WM_PAINT message to redraw the pages in your application.
 
Back
Top