I have a dialog with a static text control
I erase the dialog background (OnearaseBkgrn()) with some color
But the text control's background is not erased.
I'd like to make it the same as the dialog's background color
How can I do this ?
Thanks
Printable View
I have a dialog with a static text control
I erase the dialog background (OnearaseBkgrn()) with some color
But the text control's background is not erased.
I'd like to make it the same as the dialog's background color
How can I do this ?
Thanks
try to get the handle of the static control explicitly and then change the background color...
Thanks,
I have tried
::setbkColor(Getdlgitem(IDC_TXT)->getdc()->m_hdc,RGB(140,141,142));
but I don't see any change at all
Overload the OnCtlColor for your dialog. It will be called when a child window will be drawn. You may return the brush that has the required background color after you identified your static control.
Handle WM_CTLCOLOR inside your application .For more detail have a look in MSDN.
Thanks
Try this:
Code:HBRUSH MyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
//If you just want one static control
if(pWnd->GetDlgCtrlID() == IDC_STATIC_MY_STATIC_CONTROL)
{
// yes, change text color
pDC->SetTextColor(RGB(0,0,255));
}
//or
//if you want all the static controls.
if(nCtlColor == CTLCOLOR_STATIC){
pDC->SetTextColor(RGB(0,0,255));
}
// TODO: Return a different brush if the default is not desired
return hbr;
}
Select one or the other if statements depending on your situation.
I think you mentioned that you wanted only the background color, if that is the case then use the following command.
Code:pDC->SetBkColor(RGB(0,0,255));
Don't think the HBRUSH should be retrieved by calling CFormView::OnCtlColor. Even if the MyDialog was derived from CFormView (what is doubtful) the On... functions are handler functions for messages and rarely should be misused for other purposes. Instead you can use a brush where you hold the handle as member in your dialog class and which was created at first use like that:
The m_hbrStatic can be a private member of type HBRUSH in your dialog class. You can free the m_hbrStatic by calling DeleteObject in the EndDialog member of your dialog or in the destructor.Code:HBRUSH MyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
if (pWnd == GetDlgItem(IDC_MY_STATIC))
{
if (m_hbrStatic == NULL)
{
m_hbrStatic = ::CreateSolidBrush(RGB(100, 120, 140));
}
return m_hbrStatic;
}
return (HBRUSH)(*pDC->GetCurrentBrush());
}
I tested it on my FormView but the statement should be as aboveQuote:
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
This is the edited version
Code:HBRUSH MyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
//If you just want one static control
if(pWnd->GetDlgCtrlID() == IDC_STATIC_MY_STATIC_CONTROL)
{
// yes, change text color
pDC->SetTextColor(RGB(0,0,255));
}
//or
//if you want all the static controls.
if(nCtlColor == CTLCOLOR_STATIC){
pDC->SetTextColor(RGB(0,0,255));
}
// TODO: Return a different brush if the default is not desired
return hbr;
}
The first on is if you want to work with a FormView, change MyDialog to MyFormview.
It's always good to create object in constructor and delete it in destructor .try to avoid creating brush under your WM_CTLCOLOR it will just keep creating new brush every time without deleting them .And you can see performance issue or if application is big may be it can hang your application too.You don't have to do lots of work.
Thanks