Solved

Exact dimension of a text string?

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

 

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

Independent Software Vendors: 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
changeXy challenge 13 93
noX challenge 17 134
firstChar challenge 13 133
Updating statistics with error notification email in SQL server 4 120
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

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