Solved

Setting of  Color in MFC

Posted on 2001-07-12
4
792 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Detect CR LF to each line 12 176
WinWaitActive parameters 12 31
MaxSpan challenge 9 124
mapBully challenge 6 178
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

726 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