• Advertisement
Sign in to follow this  

Win32 Window Resizing Issue

This topic is 1674 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have an issue with a simple Win32 Window. In this small bit of code, I have created a window with a white background. But if you resize it, it'll leave the white background the same size as the window before it got resized and draw black everywhere else. I want to be able to continue to have it be a white background when it gets resized. I know it involves WM_PAINT as one of the window procedures since it gets affected from a resize but I couldnt find the solution in Google. What should I put for WM_PAINT? Thanks in advance and heres the code to the simple window:

 

#include <windows.h>

HWND hWnd;
MSG msg;

LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
{
    WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_VREDRAW|CS_HREDRAW|CS_OWNDC, WindowProcedure, 0, 0, hInstance, NULL, LoadCursor(NULL, IDC_ARROW), NULL, NULL, "Window", NULL};
    RegisterClassEx(&wc);
    hWnd = CreateWindowEx (0, "Window", "Win32", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 330, 250, HWND_DESKTOP, NULL, hInstance, NULL);
    ShowWindow (hWnd, nCmdShow);
    while (1)
    {
        if (PeekMessage(&msg,NULL,0,0,PM_REMOVE) > 0)
        {
            if (WM_QUIT == msg.message) break;
            TranslateMessage (&msg);
            DispatchMessage (&msg);
        }
        else
        {
            //Render code here
        }
        
    }
    return msg.wParam;
}

LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
        case WM_DESTROY:
            PostQuitMessage (0);
            HANDLE Process;
            Process = OpenProcess(PROCESS_ALL_ACCESS , true , GetCurrentProcessId());
            TerminateProcess(Process , 0);
            break;
        case WM_KEYDOWN:
            if(wParam == VK_ESCAPE)
            {
                DestroyWindow(hWnd);
                return(0);
            }
        default:
            return DefWindowProc (hWnd, msg, wParam, lParam);
    }
    return 0;
}

Share this post


Link to post
Share on other sites
Advertisement

Why are you terminating the process manually on WM_DESTROY? You're already returning from WinMain() when the window gets destroyed.

 

Anyway, what Khatharr said. If you set the background brush in your window class to a specific brush, it will automatically be used to draw inside the window when it gets resized. For example, use GetStockObject( BLACK_BRUSH ) or GetStockObject( WHITE_BRUSH ).

Share this post


Link to post
Share on other sites

I was terminating the process because I had a situation in the past where the program remained in the process list after I exited out. But after commenting out the 3 lines that terminated the process, I see it exit ok still. So I went ahead and removed these:

 

            HANDLE Process;
            Process = OpenProcess(PROCESS_ALL_ACCESS , true , GetCurrentProcessId());
            TerminateProcess(Process , 0);

 

So I tried out Khatharrs method and it works like a charm. Thanks a bunch. And heres the new code I have:

#include <windows.h>

HWND hWnd;
MSG msg;
WNDCLASSEX wc;

LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
{

    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = CS_VREDRAW|CS_HREDRAW|CS_OWNDC;
    wc.lpfnWndProc   = WindowProcedure;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = NULL;
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = "Window";
    wc.hIconSm       = NULL;

    RegisterClassEx(&wc);
    hWnd = CreateWindowEx (0, "Window", "Win32", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 330, 250, HWND_DESKTOP, NULL, hInstance, NULL);
    ShowWindow (hWnd, nCmdShow);
    while (1)
    {
        if (PeekMessage(&msg,NULL,0,0,PM_REMOVE) > 0)
        {
            if (WM_QUIT == msg.message) break;
            TranslateMessage (&msg);
            DispatchMessage (&msg);
        }
        else
        {
            //Render code here
        }
        
    }
    return msg.wParam;
}

LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
        case WM_DESTROY:
            PostQuitMessage (0);
            break;
        case WM_KEYDOWN:
            if(wParam == VK_ESCAPE)
            {
                DestroyWindow(hWnd);
                return(0);
            }
            break;
        case WM_PAINT:
            wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);
            break;
        default:
            return DefWindowProc (hWnd, msg, wParam, lParam);
    }
    return 0;
}
Edited by Psychopathetica

Share this post


Link to post
Share on other sites
bool run = true;
while(run) {
  //update game
  while(PeekMessage(&msg,NULL,0,0,PM_REMOVE) > 0) {
    if(WM_QUIT == msg.message) {run = false;}
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement