Solved

Exact dimension of a text string?

Posted on 2000-02-21
8
1,119 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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…
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.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

24 Experts available now in Live!

Get 1:1 Help Now