Click here to Skip to main content
15,911,035 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: ListBox Control Pin
Not Active29-Aug-01 14:18
mentorNot Active29-Aug-01 14:18 
GeneralRe: ListBox Control Pin
Michael Dunn29-Aug-01 14:30
sitebuilderMichael Dunn29-Aug-01 14:30 
GeneralRe: ListBox Control Pin
RobJones30-Aug-01 4:58
RobJones30-Aug-01 4:58 
GeneralOwner draw static control and memory leak Pin
Peter Pearson29-Aug-01 9:38
Peter Pearson29-Aug-01 9:38 
GeneralRe: Owner draw static control and memory leak Pin
Tomasz Sowinski29-Aug-01 9:51
Tomasz Sowinski29-Aug-01 9:51 
GeneralRe: Owner draw static control and memory leak Pin
Peter Pearson29-Aug-01 10:02
Peter Pearson29-Aug-01 10:02 
GeneralRe: Owner draw static control and memory leak Pin
Tomasz Sowinski29-Aug-01 10:01
Tomasz Sowinski29-Aug-01 10:01 
GeneralRe: Owner draw static control and memory leak Pin
Peter Pearson29-Aug-01 10:04
Peter Pearson29-Aug-01 10:04 
Right here's the OnPaint code:

void CCustStatic::OnPaint() 
{
	CPaintDC dc(this);
	
	CString strText;
    GetWindowText(strText);

	CRect rect;
	GetClientRect(rect);

    rect.DeflateRect(CSize(1, 1));
   
    dc.FillSolidRect(rect, m_Colour);

	int nMode = dc.SetBkMode(TRANSPARENT);

	if (m_bSimple == TRUE)
	{
		CFont font;
		
		font.CreateFont(-MulDiv(m_nSize, GetDeviceCaps(CreateCompatibleDC(NULL), LOGPIXELSY), 72),0,0,0,m_nWeight,0,0,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH|FF_DONTCARE, "Arial");

		CFont *oldfont = dc.SelectObject(&font);

		if (m_bCentred == TRUE)
		{
			CSize Extent = dc.GetTextExtent(strText);
			CPoint pt(rect.CenterPoint().x - Extent.cx/2, 
				rect.CenterPoint().y - Extent.cy/2);

			if (!strText.IsEmpty())
			{
				dc.TextOut(pt.x, pt.y, strText);
			}
		}
		else
		{
			CRect rect2 = rect;

			rect2.DeflateRect(CSize(2,2));

			dc.DrawText(strText, strText.GetLength(), rect2, DT_LEFT | DT_WORDBREAK | DT_NOPREFIX);
		}

		dc.SelectObject(oldfont);
	}
	else
	{
		int nX, nY = 0;

		if (m_bCentred == TRUE)
		{
			CString strTextTemp = "";
			//= strText;
			for (int j = 0; j < strText.GetLength(); j++)
			{
				if (strText[j] == '<')
				{
					if (strText[j + 1] == '/')
					{
						j ++;
					}

					j ++;
					j ++;
					j ++;
				}
				else
				{
					strTextTemp += strText[j];
				}
			}

			CFont font;
		
			font.CreateFont(-MulDiv(m_nSize, GetDeviceCaps(CreateCompatibleDC(NULL), LOGPIXELSY), 72),0,0,0,m_nWeight,0,0,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH|FF_DONTCARE, "Arial");

			CFont *oldfont = dc.SelectObject(&font);

			CSize Extent = dc.GetTextExtent(strTextTemp);

			nX = rect.CenterPoint().x - Extent.cx/2;
			nY = rect.CenterPoint().y - Extent.cy/2;

			dc.SelectObject(oldfont);
		}
		else
		{
			nX = rect.left;
			nY = rect.top;
		}

		BOOL bInsideTag = FALSE;
		BOOL bBold = FALSE;
		BOOL bItalic = FALSE;
		BOOL bUnderLined = FALSE;
		BOOL bEndTag = FALSE;

		for (int i = 0; i < strText.GetLength(); i++)
		{
			if (!bInsideTag)
			{
				if (strText[i] == '<')
				{
					bInsideTag = TRUE;

					continue;
				}

				CFont font;

				if (bBold == TRUE)
				{
					font.CreateFont(-MulDiv(m_nSize, GetDeviceCaps(CreateCompatibleDC(NULL), LOGPIXELSY), 72), 0, 0, 0, m_nWeight + 200, bItalic, bUnderLined, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH|FF_DONTCARE, "Arial");
				}
				else
				{
					font.CreateFont(-MulDiv(m_nSize, GetDeviceCaps(CreateCompatibleDC(NULL), LOGPIXELSY), 72), 0, 0, 0, m_nWeight, bItalic, bUnderLined, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH|FF_DONTCARE, "Arial");
				}

				CFont *oldfont = dc.SelectObject(&font);

				CString strChar = strText[i];

				CSize sSize = dc.GetTextExtent(strChar);

				dc.TextOut(nX, nY, strChar);

				nX += sSize.cx;
		//		nY += sSize.cy;

				dc.SelectObject(oldfont);
			}
			else
			{
				if (strText[i] == 'b')
				{
					if (strText[i - 1] == '/')
					{
						bBold = FALSE;
					}
					else
					{
						bBold = TRUE;
					}

					continue;
				}
				else if (strText[i] == 'i')
				{
					if (strText[i - 1] == '/')
					{
						bItalic = FALSE;
					}
					else
					{
						bItalic = TRUE;
					}

					continue;
				}
				else if (strText[i] == 'u')
				{
					if (strText[i - 1] == '/')
					{
						bUnderLined = FALSE;
					}
					else
					{
						bUnderLined = TRUE;
					}

					continue;
				}
				else if (strText[i] == '>')
				{
					bInsideTag = FALSE;
					continue;
				}
			}
		}
	}

	dc.SetBkMode(nMode);
}



m_nSize, m_nWeight and m_Colour are all member variables of the control.

Thanks,
Peter
GeneralRe: Owner draw static control and memory leak Pin
Tomasz Sowinski29-Aug-01 10:13
Tomasz Sowinski29-Aug-01 10:13 
GeneralRe: Owner draw static control and memory leak Pin
Peter Pearson29-Aug-01 11:32
Peter Pearson29-Aug-01 11:32 
GeneralDifference between 2 CTime objects Pin
duggie29-Aug-01 9:24
duggie29-Aug-01 9:24 
GeneralRe: Difference between 2 CTime objects Pin
Tomasz Sowinski29-Aug-01 9:44
Tomasz Sowinski29-Aug-01 9:44 
GeneralRe: Difference between 2 CTime objects Pin
duggie29-Aug-01 10:06
duggie29-Aug-01 10:06 
GeneralRe: Difference between 2 CTime objects Pin
Tomasz Sowinski29-Aug-01 10:21
Tomasz Sowinski29-Aug-01 10:21 
GeneralRe: Difference between 2 CTime objects Pin
duggie29-Aug-01 10:47
duggie29-Aug-01 10:47 
GeneralCHTMLView And ActiveX Documents Pin
AJ12329-Aug-01 8:37
AJ12329-Aug-01 8:37 
GeneralCOleDateTime in CRecordset Pin
duggie29-Aug-01 8:16
duggie29-Aug-01 8:16 
General(ISAPI) SAVING SESSION VARIABLES Pin
29-Aug-01 7:56
suss29-Aug-01 7:56 
GeneralDialog Box problems Pin
29-Aug-01 7:41
suss29-Aug-01 7:41 
GeneralRe: Focus Problem Pin
29-Aug-01 9:59
suss29-Aug-01 9:59 
GeneralRe: Focus Problem Pin
Paolo Messina29-Aug-01 12:30
professionalPaolo Messina29-Aug-01 12:30 
GeneralRe: Focus Problem Pin
29-Aug-01 14:34
suss29-Aug-01 14:34 
GeneralFinding address Pin
cyrilbdt29-Aug-01 7:40
cyrilbdt29-Aug-01 7:40 
GeneralRe: Finding address Pin
Derek Waters29-Aug-01 13:31
Derek Waters29-Aug-01 13:31 
GeneralRe: Finding address Pin
29-Aug-01 13:43
suss29-Aug-01 13:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.