Problem with SetTextColor
-
Hi, I have a dialog with a few edit controls. I want to set the text of one control to red and the rest should not change. Now if I call the pDC->SetTextColor(RGB(255,0,0)) in the OnCtlColor message handler of the dialog this changes the text color for all the edit controls in the dialog. If I call the following code: CDC *pDC = EditToChangeTextColor.GetDC(); pDC->SetTextColor(RGB(255,0,0)); nothing changes. Does anyone know if there is a way to change the text collor only for one control in the dialog? Thanks. avivhal
-
Hi, I have a dialog with a few edit controls. I want to set the text of one control to red and the rest should not change. Now if I call the pDC->SetTextColor(RGB(255,0,0)) in the OnCtlColor message handler of the dialog this changes the text color for all the edit controls in the dialog. If I call the following code: CDC *pDC = EditToChangeTextColor.GetDC(); pDC->SetTextColor(RGB(255,0,0)); nothing changes. Does anyone know if there is a way to change the text collor only for one control in the dialog? Thanks. avivhal
-
Hi, I have a dialog with a few edit controls. I want to set the text of one control to red and the rest should not change. Now if I call the pDC->SetTextColor(RGB(255,0,0)) in the OnCtlColor message handler of the dialog this changes the text color for all the edit controls in the dialog. If I call the following code: CDC *pDC = EditToChangeTextColor.GetDC(); pDC->SetTextColor(RGB(255,0,0)); nothing changes. Does anyone know if there is a way to change the text collor only for one control in the dialog? Thanks. avivhal
it is not correct. you must call SetTextColor() inside of OnPaint() of your control includeh10
-
Hi, I have a dialog with a few edit controls. I want to set the text of one control to red and the rest should not change. Now if I call the pDC->SetTextColor(RGB(255,0,0)) in the OnCtlColor message handler of the dialog this changes the text color for all the edit controls in the dialog. If I call the following code: CDC *pDC = EditToChangeTextColor.GetDC(); pDC->SetTextColor(RGB(255,0,0)); nothing changes. Does anyone know if there is a way to change the text collor only for one control in the dialog? Thanks. avivhal
Hi Check the MSDN fot CWnd::OnCtlColor, it even has a example of what you are asking. You just have to verify the message is for the control you want to change, otherwise do nothing. Extracted form the MSDN
// This OnCtlColor handler will change the color of a static control
// with the ID of IDC_MYSTATIC. The code assumes that the CMyDialog
// class has an initialized and created CBrush member named m_brush.
// The control will be painted with red text and a background
// color of m_brush.HBRUSH CZilchDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
// Call the base class implementation first! Otherwise, it may
// undo what we're trying to accomplish here.
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);// Are we painting the IDC_MYSTATIC control? We can use
// CWnd::GetDlgCtrlID() to perform the most efficient test.
if (pWnd->GetDlgCtrlID() == IDC_MYSTATIC)
{
// Set the text color to red
pDC->SetTextColor(RGB(255, 0, 0));// Set the background mode for text to transparent // so background will show thru. pDC->SetBkMode(TRANSPARENT); // Return handle to our CBrush object hbr = m\_brush;
}
return hbr;
}Fabian
-
Hi Check the MSDN fot CWnd::OnCtlColor, it even has a example of what you are asking. You just have to verify the message is for the control you want to change, otherwise do nothing. Extracted form the MSDN
// This OnCtlColor handler will change the color of a static control
// with the ID of IDC_MYSTATIC. The code assumes that the CMyDialog
// class has an initialized and created CBrush member named m_brush.
// The control will be painted with red text and a background
// color of m_brush.HBRUSH CZilchDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
// Call the base class implementation first! Otherwise, it may
// undo what we're trying to accomplish here.
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);// Are we painting the IDC_MYSTATIC control? We can use
// CWnd::GetDlgCtrlID() to perform the most efficient test.
if (pWnd->GetDlgCtrlID() == IDC_MYSTATIC)
{
// Set the text color to red
pDC->SetTextColor(RGB(255, 0, 0));// Set the background mode for text to transparent // so background will show thru. pDC->SetBkMode(TRANSPARENT); // Return handle to our CBrush object hbr = m\_brush;
}
return hbr;
}Fabian
Thanks now its working fine, I dont know how I missed this example in the MSDN (should have thought of it myself anyway), I only found a very general one with no reference as to the caller. Is it possible maybe to call SetTextColor from outside OnCtlColor? I tried it in various places and handlers but it did not seem to work(including in the OnPaint handler). Maybe the attributes set for the DC fonts color are blocked when called from outside this segment of code... Thanks again. avivhal