Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Setting of  Color in MFC

Posted on 2001-07-12
4
Medium Priority
?
809 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
[X]
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
4 Comments
 
LVL 10

Accepted Solution

by:
makerp earned 200 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

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…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

722 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