Thread: [newbie] repainting window

  1. #1
    Rage7531
    Guest

    [newbie] repainting window

    hey hey... just started windows programming today... looked through some tutorials but alittle lost... wondering if someone can help me with a simple question


    i have a background color in my window but when i resize it the background color is lost... not quite sure how i would keep the background color..

    Do I need to use a PaintStruct? (i don't even know what it is )

    LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
    {
    static int counter = 0;

    switch (iMsg)
    {
    case WM_LBUTTONDOWN:

    HBRUSH hbrush;
    HDC hdc;
    RECT client_window;
    hdc = GetDC(hwnd);
    GetClientRect(hwnd,&client_window);
    counter = counter + 1;

    if (counter == 1)
    {
    hbrush = CreateSolidBrush(RGB(255, 64, 43));
    FillRect(hdc,&client_window,hbrush);
    }
    else if (counter == 2)
    {
    hbrush = CreateSolidBrush(RGB(89, 65, 0));
    FillRect(hdc,&client_window,hbrush);
    }
    else if (counter == 3)
    {
    hbrush = CreateSolidBrush(RGB(55, 0, 56));
    FillRect(hdc,&client_window,hbrush);
    }
    else
    {
    PostQuitMessage(0);
    }
    ReleaseDC(hwnd,hdc);
    DeleteObject(hbrush);
    break;

    case WM_DESTROY:
    PostQuitMessage(0);
    break;
    }

    return DefWindowProc (hwnd, iMsg, wParam, lParam);
    }


    I know i need "case WM_PAINT:" in there but i'm not sure what to put after that. Do i need "case WM_SIZE:" ?

    also did i free memory properly or am i missin' something?

    thnx in advance for any kind of help

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Cut that code out of the callback.

    Put it in a function.

    Call the function in response to any msg you think will clear the background.

    That will 'patch' your problem.

    Watch out. You are freeing a brush you have not created if counter != 1 || 2 || 3
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Rage7531
    Guest
    Ah thnx.. ill give the function a try....

    oh i didnt' realize that the brush wasn't created yet...


    HBRUSH hbrush; <----- this doesn't create the brush?

    hbrush = CreateSolidBrush(RGB(255, 64, 43)); <----- this does?


    am i right..?

  4. #4
    Unregistered
    Guest
    Yes.

    Like

    STRUCT *pStruct=NULL;//this is the pointer to the struct

    pStruct=(STRUCT *)GlobalAlloc(GHND,sizeof(STRUCT));//this 'creates' the mem and must be free'ed later

    Same for your GDI resources. CreateXX() and GetXX() are alloc'ing these resources.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  2. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  5. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM