Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Static Control Size

Posted on 2010-09-16
3
Medium Priority
?
716 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 1000 total points
ID: 33695079
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
ID: 33695139
Thanks!
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33695636
You are welcome
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

722 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