Solved

Static Control Size

Posted on 2010-09-16
3
707 Views
Last Modified: 2013-11-20
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
Comment
Question by:IssacJones
  • 2
3 Comments
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 250 total points
Comment Utility
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
 

Author Closing Comment

by:IssacJones
Comment Utility
Thanks!
0
 
LVL 33

Expert Comment

by:pgnatyuk
Comment Utility
You are welcome
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now