?
Solved

How to get Text Width and Height?

Posted on 2003-03-02
13
Medium Priority
?
444 Views
Last Modified: 2012-08-14
Hi, all

How do you get the exact width and height (even if the user has increased or decreased the DPI settings) of a single-font multi-line text without drawing it? Do you use GetCharWidth32 for this process or are there better ways? Could you please demonstrate this process in code please =)
0
Comment
Question by:Aidman
[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
  • 4
  • 3
  • 2
  • +4
13 Comments
 

Expert Comment

by:BlueTrin
ID: 8054164
Um you should tell what language + platform you are using
0
 
LVL 86

Expert Comment

by:jkr
ID: 8054374
You could use "GetTextExtentPoint32()". See also http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/fontext_8smq.asp (API ref.).
0
 

Expert Comment

by:daknight2000
ID: 8055063
have u tried using GetCharABCWidths(.....) ???
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 48

Accepted Solution

by:
AlexFM earned 300 total points
ID: 8055403
Use DrawText with DT_CALCRECT flag.
0
 

Expert Comment

by:mnf
ID: 8055948
In c++builder there are TextWidth and TextHeight functions (Microsoft will probably have equivalents)

So roughly:

maxWidth = 0
for each line of text
     width = canvas->TextWidth(line)
     if width > maxWidth maxWidth = width
height = canvas->TextHeight(" ") * number of lines

where canvas is has the desired font etc



0
 
LVL 49

Expert Comment

by:DanRollins
ID: 8055954
What does this mean?
>> (even if the user has increased or decreased the DPI settings)

Is your question "what size will a textbox be" or "what size will a certain string of text be"
?

-- Dan
0
 

Author Comment

by:Aidman
ID: 8056498
>>Um you should tell what language + platform you are using
I am using WinXP and VC++ 6 Compiler, why do you need to know the current language I am using? The answer to my question should not be language dependent. But if you must know it is English.

>> have u tried using GetCharABCWidths(.....) ???
I don't know how to use it? Could you demonstrate please

>>What does this mean?
>>>> (even if the user has increased or decreased the DPI settings)
DPI is a varaible that can adjust all fonts size to be displayed bigger or smaller depending on the DPI value. You can adjust your DPI setting at "Start Button -> Control Panel -> Display -> Settings Tab -> Advanced Button"

>>Is your question "what size will a textbox be" or "what size will a certain string of text be"
?
Yes, my question is "what size will a certain string of text be?"

I am currently testing both "GetTextExtentPoint32()" and "DrawText()" with DT_CALCRECT flag. Which one is better?
0
 

Author Comment

by:Aidman
ID: 8056552
>>Um you should tell what language + platform you are using
I am using WinXP and VC++ 6 Compiler, why do you need to know the current language I am using? The answer to my question should not be language dependent. But if you must know it is English.

>> have u tried using GetCharABCWidths(.....) ???
I don't know how to use it? Could you demonstrate please

>>What does this mean?
>>>> (even if the user has increased or decreased the DPI settings)
DPI is a varaible that can adjust all fonts size to be displayed bigger or smaller depending on the DPI value. You can adjust your DPI setting at "Start Button -> Control Panel -> Display -> Settings Tab -> Advanced Button"

>>Is your question "what size will a textbox be" or "what size will a certain string of text be"
?
Yes, my question is "what size will a certain string of text be?"

I am currently testing both "GetTextExtentPoint32()" and "DrawText()" with DT_CALCRECT flag. Which one is better?
0
 

Author Comment

by:Aidman
ID: 8056570
Ahha! I noticed that GetTextExtentPoint32() does not support multi-line it calculates it as a complete line.
I know you can simple make a function for it to work with multi-line but that is not necessery for the DrawText()
0
 

Author Comment

by:Aidman
ID: 8056576
Great answer AlexFM, shot and simple but I would have liked an code demonstration.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 8056706
RECT rect;
rect.left = 0;
rect.top = 0;
rect.right = 100;
rect.bottom = 10;

int nHeight = DrawText(hDC, sText, -1, &rect, DT_CALCRECT | DT_WORDBREAK);

Now you have the height of formatted text in nHeight variable. rect.bottom is changed also.
0
 

Expert Comment

by:daknight2000
ID: 8058610
here is a small code that i have used to find the the rectangle size in a custom tool tip. This will only give  you the width of a line of characters. You may modify it as per requirement.

CString szMyString ="Hello world!!";
int iLen = szMyString.GetLength();
int iWidth=0;
ABC *pABC = new ABC[iLen];
ABC abc;
ZeroMemory(pABC, iLen);

pDC->GetCharABCWidths(0, iLen, pABC)

for (int iCount =0; iCount<iLen; iLen++)
{
 abc = *pABC++;
 iWidth += abc.abcA + abc.abcB + abc.abcC;
}
// iWidth will be the length...

to get the text height,,, u can simply get the text metrix..and the tmHieght will give u the height
?????
0
 

Expert Comment

by:daknight2000
ID: 8058622
sorry, pDC should be the device context of the windown in which ur text has to be drawn...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

752 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