Coloring text in an ATL Control
-
I am working on an ATL control that has a simple CStatic text label. While I've managed to change the font in both OnInitDialog and OnPaint I cannot change the color. Basically it is a Property Sheet with a CStatic control on it. In the Constructor m_pArial is defined as: m_pArial->CreateFont(40, 0, 0, 0, 700, 0, 0, 0, 0, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FIXED_PITCH, "Arial"); And it is deleted in the destructor. Here is my code (it sets the font just fine - not the color). void CPropPage::OnPaint() { CPaintDC dc(this); // device context for painting CPen *pOldPen; CPen BluePen; BluePen.CreatePen(PS_SOLID, 1, RGB(0, 0, 255)); CBrush Brush(RGB(0, 0, 255)); CBrush *pOldBrush; pOldPen = dc.SelectObject(&BluePen); pOldBrush = dc.SelectObject(&Brush); m_Static_Title.SetFont(m_pArial); m_Static_Title.SetWindowText("Test"); dc.SelectObject(pOldPen); dc.SelectObject(pOldBrush); } Thank You
-
I am working on an ATL control that has a simple CStatic text label. While I've managed to change the font in both OnInitDialog and OnPaint I cannot change the color. Basically it is a Property Sheet with a CStatic control on it. In the Constructor m_pArial is defined as: m_pArial->CreateFont(40, 0, 0, 0, 700, 0, 0, 0, 0, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FIXED_PITCH, "Arial"); And it is deleted in the destructor. Here is my code (it sets the font just fine - not the color). void CPropPage::OnPaint() { CPaintDC dc(this); // device context for painting CPen *pOldPen; CPen BluePen; BluePen.CreatePen(PS_SOLID, 1, RGB(0, 0, 255)); CBrush Brush(RGB(0, 0, 255)); CBrush *pOldBrush; pOldPen = dc.SelectObject(&BluePen); pOldBrush = dc.SelectObject(&Brush); m_Static_Title.SetFont(m_pArial); m_Static_Title.SetWindowText("Test"); dc.SelectObject(pOldPen); dc.SelectObject(pOldBrush); } Thank You
You need to do it in the WM_CTLCOLORSTATIC message handler From MSDN on WM_CTLCOLORSTATIC: "A static control, or an edit control that is read-only or disabled, sends the WM_CTLCOLORSTATIC message to its parent window when the control is about to be drawn. By responding to this message, the parent window can use the specified device context handle to set the text and background colors of the static control." Cheers, -Erik ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ My thoughts are my own and reflect on no other.