Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 717
  • Last Modified:

Static Control Size

Hiya

I have an interesting problem which I hope somebody can help me with.

I have a static control in which I am displaying text.

I have been trying to get the rectangle in which the text occupies - which is going to be smaller than the static controls rectangle.

I tried to do this using the following:

CStatic* control = (CStatic*)GetDlgItem(IDC_MYSTATIC);
CString s = "This is a long message\n\nwith line breaks";
CRect rect = CRect(CPoint(0,0) ,CPoint(0,0));
CDC* pDC = control.GetDC();
pDC->DrawText( s, rect, DT_CALCRECT );

However, the dimensions of rect are not correct e.g. rect appears too wide.

Can anybody tell me what I'm doing wrong?

John
0
IssacJones
Asked:
IssacJones
  • 2
1 Solution
 
pgnatyukCommented:
If you use DrawText for this task:

1. I think you need to remove \n from the text. It does nothing. Add DT_WORDBREAK if you need. But you can draw the text in the single line and you will know the text size and so can operate with it.

2. Select the correct (needed) font in the DC where you draw the text.

3. You can call DrawText with the correct rectangle (call GetClientRect for the control window). And with all options you need like DT_SINGLELINE | DT_LEFT and in the end add DT_CALCRECT. This function returns the text height, if everything's ok. Compare this height with the rectangle height and you will know if you can draw the text in the static control. If you need, you can take the text width from the rectangle.

4. DT_CALCRECT - you will not see the text drawn. This option says to calculate the rectangle.

5. There are other method to detect the text size. For example, GetTextExtent/GetOutputTextExtent/GetTabbedTextEntent:
http://msdn.microsoft.com/en-us/library/z7e878zz(VS.80).asp
http://msdn.microsoft.com/en-us/library/98e99kxk(v=VS.80).asp
http://msdn.microsoft.com/en-us/library/49d189t4(v=VS.80).asp

In you case you will need to write something like:
CSize size = pDC->GetTextExtent(s);
//size.cx is the text width and size.cy is the text height.

Or GetTextExtentPoint32
http://msdn.microsoft.com/en-us/library/dd144938(VS.85).asp

You always select a font in the DC.
 
I attached 2 examples I found on my computer. It is Win32 API code.
BOOL TextSize(HDC hDC, LPCWSTR lpszText,int nLength, LPSIZE pSize)
{
	if (hDC == NULL || pSize == NULL)
		return FALSE;
	ZeroMemory(pSize, sizeof(SIZE));
	if (lpszText == NULL)
		return TRUE;

	RECT rt1 = {0,0,0,0};
	DrawTextW(hDC,lpszText,nLength,&rt1,DT_LEFT | DT_CALCRECT | DT_NOPREFIX);
	pSize->cx =  rt1.right  + 3;
	pSize->cy =  rt1.bottom + 1;
	return TRUE;
}

	
BOOL TextSize(HFONT font, LPCWSTR lpszText, LPSIZE pSize)
{		
	if (!lpszText || !pSize)
		return FALSE;
	if (!font)
		font = (HFONT)GetStockObject(SYSTEM_FONT);
	HDC hDC = CreateMemDC(NULL);
	HGDIOBJ oldFont = SelectObject(hDC,font);
        BOOL ret = TextSize(hDC,lpszText,pSize);
	SelectObject(hDC,oldFont);

	DestroyMemDC(hDC);
	return ret;
}

Open in new window

0
 
IssacJonesAuthor Commented:
Thanks!
0
 
pgnatyukCommented:
You are welcome
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now