Solved

Setting of  Color in MFC

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
unable to delete all specified values regedit 38 155
while loop over for loop 7 79
fizzArray2 challenge 1 60
matchUp  challenge 6 48
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now