Thread: Background Colour in Windows API

  1. #1

    Background Colour in Windows API


    I am writing a program using the Windows API, the whole background of the window is white but when I put in static text the background of the text is the standard text.

    How do I change the background of the window to the standard grey?


  2. #2
    Registered User
    Join Date
    Apr 2002
    Part I - Gray background

    Well to change the background color you need to specify a new brush for your painting. This is done when you are registering your class. Change the "hbrBackground" member of the WNDCLASS or WNDCLASSEX struct to ...

    (HBRUSH)GetStockObject( GRAY_BRUSH );
    That will return to you the standard gray brush. There are other things you can pass to the function but you said you wanted a gray brush.

    Part II - Transparent background text

    If you just use TextOut now to draw your text you will notice that the background of the text is white! This is obviously not what you want! You want the background to be transparent. Don't worry it is a simple function call away.

    SetBkMode( HDC hDC, int iBkMode );
    The only valid modes are:

    So your call might look like this..

    SetBkMode( hDC, TRANSPARENT );
    That should do it.

  3. #3
    train spotter
    Join Date
    Aug 2001
    near a computer
    SetBkColor() will change the colour under the text.

    >>How do I change the background of the window to the standard grey?

    Did you register a window class? If so change the background brush value.

    look up WNDCLASSEX

    WndClass.hbrBackground =(HBRUSH) COLOR_WINDOWFRAME;

    or change the default properties on the fly
    //change teh default colour in the DC to gray
    SetDCBrushColor(hdc, GetStockObject(GRAY_BRUSH);
    GetClientRect(hWnd, &theRect);
    //ask to redraw whole client area so we see new colour
    Else simple and bad paint handler;

    case WM_PAINT:
    //find the area to paint
    GetUpdateRect(hWnd, &theRect, 0);
    if (IsRectEmpty(&theRect))
            GetClientRect(hWnd, &theRect);
    //create the GDI
    BeginPaint(hWnd, &ps);
    EndPaint(hWnd, &ps);
    DeleteObject(hBrush);//no 100% needed but is good practice to clean up GDI's
    "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. Windows API
    By valaris in forum Windows Programming
    Replies: 7
    Last Post: 08-01-2008, 12:37 AM
  2. windows api file functions
    By xixpsychoxix in forum Windows Programming
    Replies: 8
    Last Post: 07-01-2008, 04:26 PM
  3. windows api functions, not working. requirements?
    By Mr.Bit in forum Windows Programming
    Replies: 5
    Last Post: 10-10-2007, 08:20 AM
  4. Calling raw windows API using borland
    By TiMBuS in forum C++ Programming
    Replies: 3
    Last Post: 06-16-2005, 08:46 AM
  5. Help with editor windows with win api
    By NightStalker in forum Windows Programming
    Replies: 1
    Last Post: 03-13-2003, 03:53 AM