Jump to content

  • Log In with Google      Sign In   
  • Create Account


Win32 Window Resizing Issue


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Psychopathetica   Members   -  Reputation: 186

Like
0Likes
Like

Posted 22 July 2013 - 11:32 PM

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;
}


Sponsor:

#2 Khatharr   Crossbones+   -  Reputation: 2937

Like
1Likes
Like

Posted 23 July 2013 - 02:46 AM

Set a background brush in your window class.

 

http://msdn.microsoft.com/en-us/library/windows/desktop/ms633577%28v=vs.85%29.aspx

 

wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);


Edited by Khatharr, 23 July 2013 - 02:50 AM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#3 mhagain   Crossbones+   -  Reputation: 7610

Like
2Likes
Like

Posted 23 July 2013 - 03:00 AM

Also considering handling WM_ERASEBKGND.


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#4 Amr0   Members   -  Reputation: 1067

Like
1Likes
Like

Posted 23 July 2013 - 03:30 AM

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 ).



#5 Psychopathetica   Members   -  Reputation: 186

Like
0Likes
Like

Posted 23 July 2013 - 01:38 PM

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, 23 July 2013 - 01:46 PM.


#6 Khatharr   Crossbones+   -  Reputation: 2937

Like
0Likes
Like

Posted 24 July 2013 - 05:50 AM

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);
  }
}

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS