Thread: Windows background color

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    77

    Windows background color

    The progam I am using has multiple windows with that use the same window class. I would like to be able to individually change their background colors. However, the only way I've been successful is by changing the background color in the window class:

    Code:
    HBRUSH hBrush;
    LOGBRUSH sBrush;
    
    sBrush.lbStyle = BS_SOLID;
    sBrush.lbColor = RGB(0, 0, 255);
    sBrush.lbHatch = 0;
    
    hBrush = CreateBrushIndirect(&sBrush);
    SetClassLong(Handle, GCL_HBRBACKGROUND, (long)hBrush);
    DeleteObject(hBrush);
    That changes the colors in all the windows with that class. I've read a few things that suggest the WM_ERASEBKGND message might be useful here, but I haven't found any examples of it in use. How can this be generated on demand, InvalidateRect(Handle, NULL, TRUE)? And how would it be used to change the background color?

    Is there any other way to change the background color of a window without impacting any other windows? I was expecting an API call of some sort for this, considering how easy it is in VB, guess that'll teach me to try to compare VB and C functionality.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>I've read a few things that suggest the WM_ERASEBKGND message might be useful here, but I haven't found any examples of it in use.<<

    Here's one:
    Code:
    case WM_ERASEBKGND:
      {
      HDC  hdc
      RECT rc;
    
      hdc=(HDC)wParam;
      GetClientRect(hwnd,&rc);
      FillRect(hdc,&rc,(HBRUSH)GetStockObject(BLACK_BRUSH));
      return TRUE;
      }
    If you're using a non-system, non-shared brush then create it once and don't forget to DeleteObject on it when done.

    InvalidateRect justs adds the given rectangle to the update region for rendering with the next WM_PAINT message which is usually okay. If you need to force a WM_PAINT then use UpdateWindow; RedrawWindow. may also be of some interest.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    77
    That works, I figured there was an API to do it. Drawing a big rectangle works. Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to change background color in console mode
    By smore in forum C++ Programming
    Replies: 1
    Last Post: 12-02-2003, 01:44 PM
  2. Background Colour in Windows API
    By Vicked in forum Windows Programming
    Replies: 2
    Last Post: 04-14-2003, 03:49 AM
  3. background color
    By volk in forum C Programming
    Replies: 4
    Last Post: 03-10-2003, 06:30 PM
  4. Text and background Color
    By Goof Program in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-29-2002, 06:59 PM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 10:19 AM