Solved

Static Control Size

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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need a Shell script to start a service checking the port 6 55
WinWaitActive parameters 12 31
has22 challenge 11 107
fix34  challenge 9 166
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

733 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