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
Solved

Static Control Size

Posted on 2010-09-16
3
712 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
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

809 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