|
-
September 16th, 2010, 06:49 AM
#1
Color for static text control
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
-
September 16th, 2010, 07:22 AM
#2
Re: Color for static text control
try to get the handle of the static control explicitly and then change the background color...
-
September 16th, 2010, 07:27 AM
#3
Re: Color for static text control
Thanks,
I have tried
::setbkColor(Getdlgitem(IDC_TXT)->getdc()->m_hdc,RGB(140,141,142));
but I don't see any change at all
-
September 16th, 2010, 07:53 AM
#4
Re: Color for static text control
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.
-
September 16th, 2010, 10:13 AM
#5
Re: Color for static text control
Handle WM_CTLCOLOR inside your application .For more detail have a look in MSDN.
Thanks
-
September 16th, 2010, 10:53 AM
#6
Re: Color for static text control
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));
Last edited by ADSOFT; September 16th, 2010 at 10:59 AM.
Rate this post if it helped you.
-
September 16th, 2010, 11:31 AM
#7
Re: Color for static text control
 Originally Posted by ADSOFT
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.
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:
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());
}
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.
-
September 16th, 2010, 01:03 PM
#8
Re: Color for static text control
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
I tested it on my FormView but the statement should be as above
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.
Rate this post if it helped you.
-
September 16th, 2010, 01:15 PM
#9
Re: Color for static text control
 Originally Posted by itsmeandnobodyelse
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());
}
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.
To much work, have to create member variables and deleting objects.
I prefer to just look up the Static's ID and let the FRAMEWORK handle the rest. Thats how OnCtlColor was intened to be used. The framework handles all the details.
Rate this post if it helped you.
-
September 16th, 2010, 02:10 PM
#10
Re: Color for static text control
 Originally Posted by ADSOFT
To much work, have to create member variables and deleting objects.
I prefer to just look up the Static's ID and let the FRAMEWORK handle the rest. Thats how OnCtlColor was intened to be used. The framework handles all the details.
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
Last edited by humptydumpty; September 16th, 2010 at 03:03 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|