Solved

Exact dimension of a text string?

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

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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 demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

19 Experts available now in Live!

Get 1:1 Help Now