Initialize Controls With Specific Background Color

Hi! I'm working on an MFC program. I have static text controls that have the standard ugly gray background which I would like to change to something else (like to white or red or blue!). How can I initialize these controls and edit controls to have a given background color in OnInitDialog(). Thanks so much I !!!

I found what looks like a good example here but I can't get it to work for me in just the initialization...
http://forums.codeguru.com/showthread.php?t=168927
In this example he uses this line: if(pWnd->GetDlgCtrlID() == IDC_EDIT1) I don't understand what -&gt means and I can't find any info on it or why he is able to put 2(?) statements in an if clause...

This is the code I currently have for this purpose in OnInitDialog(). I only have 1 main dialog right now:
1
2
HDC hdcStatic = (HDC)GetDlgItem(IDC_NAME_ONE);
SetBkColor(hdcStatic, RGB(255,0,0));


Thanks !!!!!!!!!!!!!!!!!!!!!!!
Last edited on
Tip: read html symbols table and you will know what > (greater than) means.
Regarding your question, process WM_CTLCOLORSTATIC or MFC equivalent message in your parent dialog procedure.
Last edited on
Thanks modoran I'm getting there~
The problem now is that I can't seem to get one specific static control to be the color I want. This code changes the background of every static/edit control I have). Here is what I have so far:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
BOOL CDialog::OnInitDialog()
{ 
    //...
    m_backColor = RGB(245, 245, 0);
    m_backBrush.CreateSolidBrush(m_backColor);
}


HBRUSH CDialogDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

	if (pWnd->GetDlgCtrlID() == IDC_NAME_ONE)
	{
		HDC hdc = pDC->GetSafeHdc();
		SetBkColor(hdc, m_backColor);
		return (HBRUSH)m_backBrush.GetSafeHandle();
	}

	// TODO:  Return a different brush if the default is not desired
	return hbr;  
}


OK I got it ! This will change the color of whatever control I specify.
For anybody else that comes across this here is the updated code:

1
2
3
4
5
6
7
8
9
10
11
12
HBRUSH CDialogDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

	if (pWnd->GetDlgCtrlID() == IDC_NAME_ONE)
	{
		pDC->SetBkColor(m_backColor);
		return (HBRUSH)m_backBrush.GetSafeHandle();
	}
	return 0; 
        //'return hbr;' will make every control the color of the brush...
}
Last edited on
Topic archived. No new replies allowed.