Solved

Exact dimension of a text string?

Posted on 2000-02-21
8
1,124 Views
Last Modified: 2013-11-20
I have some button control neek change it's caption often, so I need to adjust the button size when the text changed. I use CDC::GetTextExtent or CDC::GetOutputTextExtent to get the text dimension, but it seem's the size is not precision.

Any idea or answer is appreciated!
0
Comment
Question by:gblxj
8 Comments
 

Expert Comment

by:Andrian
ID: 2544886
You should use CFont or something related whit font instead of CDC .And don't forget that when you change the button's caption,you use "SetWindowText" or "SendMessage" from CWnd but not CDC(related with image)!
What can you do then?
//////////////////////////
1.Specify you Font ( set your own size!)
//////////////////////////
e.g:
CFontDialog dlg;
      dlg.DoModal();
      HFONT hFont ;
      hFont=::CreateFont(
            dlg.GetSize()/5,
            dlg.GetSize()/10,
            0,
            0,
            dlg.GetWeight(),
            (BYTE) dlg.IsItalic(),
            (BYTE) dlg.IsUnderline(),
            (BYTE) dlg.IsStrikeOut(),
            ANSI_CHARSET,
            OUT_DEVICE_PRECIS,
            CLIP_CHARACTER_PRECIS,
            PROOF_QUALITY,
            VARIABLE_PITCH,
            dlg.GetFaceName()//LPCTSTR lpszFacename
            );
      SetFont(m_Font.FromHandle(hFont),TRUE);
//////////////////////////
2.Then,you can approximately calculate the length of your text
(len ~ size*count).
e.g : 'i' and 'm' have not the same size!
like you say "it seem's the size is not precision".
//////////////////////////

0
 
LVL 1

Expert Comment

by:RAVID
ID: 2544957
Hi,

Try using
CDC::GetTextMetrics( LPTEXTMETRIC lpMetrics )

This structure will get u the information u want.

Hope this help u out

Cheers
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 2545307
Hi gblxj,

There's an MSDN article ID: Q203099 with code how to calculate the exact extent of a string using CDC::BeginPath, CDC::EndPath and CDC::GetPath.

hope that helps,

ZOPPO
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:gblxj
ID: 2545365
Thanks all!

It's seem ZOPPO's comment is very interesting, I have never used BeginPath before, I'll try it shortly.
0
 
LVL 1

Expert Comment

by:ScottyDawg
ID: 2545393
What about CDC::DrawText(...) using DT_CALCRECT
0
 
LVL 8

Expert Comment

by:VinExpert
ID: 2546280
Hi,

Try something like this
/Get the DC
CClientDC dc(this);

//Have the string
CString str("xwiz");

//Width storage
int PaneWidth = 0;

//Get the length of string
int Len = str.GetLength();

//Calculate pane width from each char width
for(int Index = 0; Index < Len; Index++)
{
char ch = str[Index];
int Temp = 0;
dc.GetCharWidth(ch,ch,&Temp);
PaneWidth += Temp;
}

//PaneWidth will give the size

OR

Do this

TEXTMETRIC txtmat;    
CClientDC dc(this);
dc.GetTextMetrics(&txtmat);

CString str("df5656");
int Len = str.GetLength();
Len *= txtmat.tmMaxCharWidth;

Len will give the width.
Try it out.
VinExpert
0
 
LVL 12

Accepted Solution

by:
migel earned 10 total points
ID: 2546542
Hi!
IMHO
DrawText with DT_CALCRECT parameter give you proper result.
Since Child controls uses parent window font by default you can write something like this:
// In the CDialog derived class member:
CMyDialog::SetVuttonText(CString& str)
{
CClientDC dc(this);
CFont* pFont = GetFont();
CFont*pOldFont = dc.SelectObject(pFont);
RECT rc
dc.DrawText(str, &rc, DT_SINGLELINE|DT_CALCRECT);
dc.SelectObject(pOldFont);
// ajust button size
m_wndButton.MoveWindow(...rc.right-rc.left+10, ...); // some points for text gap :-)
m_wndButton.SetWindowText(str);
}
0
 

Author Comment

by:gblxj
ID: 2548523
>>Child controls uses parent window font

I think this is the point of all.

Thanx!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
function returning CString in a regular win32 DLL 8 176
Include multiple hostnames in this command? 3 55
Perl Awk Need Help 3 116
iSeries DB2 Query 2 95
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 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