Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following code which dynamically creates CStatic labels.

[Code]
While(i < 5)
CRect textrect(44,y,110,y+5);
CStatic *text = new CStatic ;
MapDialogRect(&textrect);
text -> Create ( msgs.data(),WS_CHILD | WS_VISIBLE | SS_ LEFT , textrect,this ,0);
[/ Code]

Please tell how to paint rectangle behind it to white

What I have tried:

I have tried ON_WM_CTLCOLORSTATIC
ON_WM_CTLCOLOR,ON_WM_ERASEBKGND but nothing is working ..if i am trying ON_WM_CTLCOLORSTATIC Ithan whole rectangle paints to white but than drawing a text is issue coz it is created dynamically using create. Please tell some simplest approach with code snippet
Posted
Updated 30-Jun-20 14:39pm
Comments
Richard MacCutchan 24-May-20 5:54am    
If you are going to handle color and erase messages with regard to painting the control, then you should also handle the WM_PAINT message for adding the text.
Rick York 25-May-20 14:30pm    
Look for a class at this site called CLabel. It is an owner-drawn replacement control for CStatic. With it, you can set the foreground color, background color, and text font to use for the control. It is very handy and I use it often.

I did the search for you : https://www.codeproject.com/Articles/215/Extended-Use-of-CStatic-Class-CLabel-1-6

1 solution

1. ON_WM_CTLCOLOR_REFLECT
2. ON_WM_ERASEBKGND
3. ON_WM_PAINT

example:
C++
HBRUSH CColorStatic::CtlColor(CDC* pDC, UINT nCtlColor)
{
	pDC->SetBkMode(TRANSPARENT);
	if(m_nPos != -1)
		pDC->SetTextColor(DEF_TEXTCOLOR);
	else
		pDC->SetTextColor(m_TextColor);
	return (HBRUSH)::GetStockObject(NULL_BRUSH);
}

BOOL CColorStatic::OnEraseBkgnd(CDC* pDC)
{
	return TRUE;
}

void CColorStatic::OnPaint()
{
	CRect		Rect;
	CPaintDC	dc(this);
	TCHAR		szText[1024];
	CFont		*pFont, *pOldFont;
	int			w1, w2;

	GetWindowText(szText, sizeof(szText)/sizeof(TCHAR));
	GetClientRect(Rect);

	if(m_nPos == -1)
	{
		dc.FillSolidRect(0, 0, Rect.Width(), Rect.Height(), m_BackColor);
		dc.SetTextColor(m_TextColor);
	}
	else
	{
		w1 = Rect.Width() * m_nPos / m_nPosMax;
		w2 = Rect.Width() - w1;
		dc.FillSolidRect(0, 0, w1, Rect.Height(), m_TextColor);
		dc.FillSolidRect(w1, 0, w2, Rect.Height(), m_BackColor);
		dc.SetTextColor(DEF_TEXTCOLOR);
	}

	dc.SetBkMode(TRANSPARENT);
	pFont = GetFont();
	pOldFont = dc.SelectObject(pFont);
	dc.DrawText(szText, _tcslen(szText), Rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
	pOldFont = dc.SelectObject(pOldFont);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900