Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Setting of  Color in MFC

Posted on 2001-07-12
4
787 Views
Last Modified: 2013-11-20
I have a dialog box. i want to know how to set background and foreground colors in a dialog box in MFC. i don't want the default grey colour.Please advise.
0
Comment
Question by:abalkur
4 Comments
 
LVL 10

Accepted Solution

by:
makerp earned 50 total points
ID: 6277341
put this the main application objects ::InitInstance()

SetDialogBkColor(RGB(150,170,199),RGB(0,0,0));  

i have not tried putting it in each dialogs Init function so i dont know if you can give each dialog it own settings
0
 
LVL 4

Expert Comment

by:captainkirk
ID: 6277350
You can set the background color by using CWinApp::SetDialogBkColor() in your InitInstance()....

To set both, you may need to override OnCtlColor() for your dialog and call CDC::SetTextColor() and CDC::SetBkColor() there...
0
 
LVL 2

Expert Comment

by:Slordak
ID: 6277469
In my experience, sometimes just calling the functions to change the color is not sufficient.  This may depend on whether you're trying to color the whole dialog or just dialog controls.  Everything will appear to work correctly, but the dialog controls will still be the default color.  In some cases, this is because one must provide a brush to draw the dialog background with.

Consider this code:

class CSomeClass : public CDialog
{
public:
  CSomeClass();
  ~CSomeClass();

  // Overrides
  // ClassWizard generated virtual function overrides
  //{{AFX_VIRTUAL(CSomeClass)
    virtual BOOL OnInitDialog();
  //}}AFX_VIRTUAL

  protected:
    // Generated message map functions
    //{{AFX_MSG(CSomeClass)
      afx_msg HBRUSH OnCtlColor(CDC* pDC,
        CWnd* pWnd, UINT nCtlColor);
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()

    HBRUSH m_backBrush;
};

BEGIN_MESSAGE_MAP(CSomeClass, CDialog)
  //{{AFX_MSG_MAP(CSomeClass)
  ON_WM_CTLCOLOR()
  //}}AFX_MSG_MAP
END_MESSAGE_MAP()

BOOL CSomeClass::OnInitDialog()
{
  CDialog::OnInitDialog();

  m_backBrush = CreateSolidBrush(0x00A0C080);
}

HBRUSH CCallingWnd::OnCtlColor(CDC* pDC, CWnd* pWnd,
                               UINT nCtlColor)
{
  HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
     
  pDC->SetBkColor(0x00A0C080);
  hbr = m_backBrush;
  return(hbr);
}

  Basically, this code creates a brush for the dialog when the dialog is created, and then when the system goes to color the dialog controls (say the background for a static text box), it uses SetBkColor() to set the background color for that control *and* also changes the brush to match.  You can also have it check which control is being colored and you can use/return separate colors for each control in the same dialog, which is kind of nice.
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9490299
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by : makerp

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Roshan Davis
EE Cleanup Volunteer
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Different colored text in ComboBox without Subclassing 8 65
Separate into files by filename 12 87
unable to delete all specified values regedit 38 538
if loop in java 3 152
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seveā€¦
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question