Unable to change the windows background color

This is a discussion on Unable to change the windows background color within the Windows Programming forums, part of the Platform Specific Boards category; Hi there, well I'll just get to the point: I tried two methods, Code: SetClassLong(hwnd, GCL_HBRBACKGROUND, (LONG)CreateSolidBrush(RGB(255,255,255))); InvalidateRect(hwnd,0,TRUE); and Code: ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    68

    Unable to change the windows background color

    Hi there,

    well I'll just get to the point:
    I tried two methods,
    Code:
    SetClassLong(hwnd, GCL_HBRBACKGROUND, (LONG)CreateSolidBrush(RGB(255,255,255)));
    InvalidateRect(hwnd,0,TRUE);
    and
    Code:
    case WM_ERASEBKGND:
    {
    	HDC hdc;
    	RECT rc;
    	hdc = (HDC)wParam;
    	GetClientRect(hwnd,&rc);
    	FillRect(hdc,&rc,(HBRUSH)CreateSolidBrush(RGB(255,255,255)));
    	return 0;
    }
    Both methods leave my client area windows-grayish, also when I refresh/update/redraw the window.

    I use CreateDialog with resources to create my window handle.

    Code:
    STYLE DS_CENTER | WS_MINIMIZEBOX | WS_POPUP | WS_CAPTION | WS_SYSMENU | DS_3DLOOK
    EXSTYLE WS_EX_APPWINDOW
    Maybe I just forgot a style needed to change background? Can anyone give advice? Tell me if you need the full code.

    Thanks in advance, Hawk.

  2. #2
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    This page might help [URL="http://www.winprog.org/tutorial/dlgfaq.html"]. I prefer to not to use resources as they prevent the window from being able to resize, even though the background colour defaults to white.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    68
    Thanks, this method works

    I don't think I can change my style of creating windows, because I've gotten used to it and it's still the fastest way to add stuff to my programs...

    I just set the WS_THICKFRAME style and had to notice that my code in WM_PAINT stopped BitBlt-ing. Well I need to find a way around that when I come to writing a program which profits from being able to be resized.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Sorry to butt in....

    >>FillRect(hdc,&rc,(HBRUSH)CreateSolidBrush(RGB(25 5,255,255)));

    This is a GDI leak. You have allocated memory for the GDI object but not deleted it.

    Unless you are using a .NET version IDE, this will crash your app.
    [ie in MSVC v6 this is a leak, but in MSVC 2002 (or later) all GDI objects created as 'default' and so auto cleaned up by system]


    In this case (as the brush was not selected into a Device Context);
    Create the GDI object
    Use GDI object
    Clean up with DeleteObject()

    OR

    Use a 'Stock' object (which does not require deletion)
    GetStockObject(WHITE_BRUSH);



    If you use SelectObject() always catch the returned GDI object (currently selected or default).
    Use.
    Return to original GDI objects.
    Clean up (RestoreDC(), DeleteDC(), DeleteObject() etc)
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do i change backround color and text color?
    By Nathan the noob in forum C++ Programming
    Replies: 1
    Last Post: 02-17-2009, 03:39 AM
  2. Change RichTextBox Color
    By C_ntua in forum C# Programming
    Replies: 8
    Last Post: 12-18-2008, 02:44 PM
  3. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 09:00 AM
  4. Windows background color
    By Exile in forum Windows Programming
    Replies: 2
    Last Post: 01-23-2005, 06:55 AM
  5. background color
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 11-26-2001, 05:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21