CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Guest

    How to change the background color??

    Hi,
    I am unable to change the background color of a window.
    Please help me out of this.




  2. #2
    Join Date
    May 1999
    Posts
    123

    Re: How to change the background color??

    First let me advise against doing this at all. The user sets their preferred background color in the control panel, and you're going against their expressed wishes when/if you use another color.

    If you insist on doing this anyway, you simply draw a rectangle the size of the window's client area in response to the WM_ERASEBKGND message, filled the color you want.

    Let me reiterate: this is usually a BAD idea. Don't do it unless you really have a good reason to think the usual rules don't apply to you in this particular case.


    The universe is a figment of its own imagination.

  3. #3
    Join Date
    Apr 1999
    Posts
    12

    Re: How to change the background color??

    You want to do it in handler of WM_ERASEBKGND. Override CWnd::OnEraseBkgnd()
    to do it in MFC.

    Look at following article on MSDN:
    HOWTO: Change Window Background Color with Foundation Classes
    Last reviewed: May 28, 1997
    Article ID: Q103786

    If you are doing it for a MDI main window, you will have to do this for
    MDIClient area. Look at:

    HOWTO: SubClass the MDIClient by Using MFC
    Last reviewed: July 10, 1997
    Article ID: Q129471

    This article also shows how to erase the background of MDI client area

    If you are doing it for controls, you have to handleOnCtlColor(WM_CTLCOLOR).
    Look at following article:

    HOWTO: Change the Color of an MFC Child Control Class
    Last reviewed: June 26, 1997
    Article ID: Q132080

    Another relevant MSDN article:
    HOWTO: Change the Background Color of an MFC Edit Control
    Last reviewed: March 2, 1998
    Article ID: Q117778


    Ajay


  4. #4
    Join Date
    Apr 1999
    Posts
    12

    Re: How to change the background color??

    Its really not as drastic as you portray it to be. There are several scenarios that require a different color:

    1.What if a user wants to have windows of different color.

    2. You may want to display a state of data visually. In an edit control, enter wrong data etc.

    3. Just for fun to see how it looks/works. Not all apps have to be shipped to users.

    Ajay


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured