Board index » cppbuilder » How to calculate the minimum width of a TControl, such, that it's complete text is displayed?

How to calculate the minimum width of a TControl, such, that it's complete text is displayed?


2004-11-06 06:22:46 PM
cppbuilder86
How to find out minimum width of a TControl, such, that it's complete
text is displayed?
Example:
How can I calculate a TCheckBox's Width minimum value, that the text
of the checkbox is completely visible?
Thanks,
Michael
 
 

Re:How to calculate the minimum width of a TControl, such, that it's complete text is displayed?

M_R <>wrote:
Quote

How to find out minimum width of a TControl, such, that it's
complete text is displayed? [...]
Use the TCanvas::TextWidth method. For example:
SomeControl->Width = SomeControl->Canvas->TextWidth( SomeControl->Caption );
If the control type doesn't have a public Canvas property, use
a TControlCanvas:
TControlCanvas *pCanvas = new TControlCanvas;
pCanvas->Control = SomeControl;
SomeControl->Width = pCanvas->TextWidth( SomeControl->Caption );
delete pCanvas;
or
std::auto_ptr<TControlCanvas*>pCanvas( new TControlCanvas );
pCanvas->Control = SomeControl;
SomeControl->Width = pCanvas->TextWidth( SomeControl->Caption );
~ JD
 

Re:How to calculate the minimum width of a TControl, such, that it's complete text is displayed?

Thanks JD for your aswer:
Quote
Use the TCanvas::TextWidth method. For example:
Is there a general methode to find out how much additional space a
TControl does need beside the text?
For example if I use a TCheckBox CheckBox1 and set CheckBox1 to the
TextWidth()-result, a part of the text is cut, because the box itself
needs some space too.
Thanks,
Michael
 

{smallsort}

Re:How to calculate the minimum width of a TControl, such, that it's complete text is displayed?

M_R <>wrote:
Quote

[...] if I use a TCheckBox CheckBox1 and set CheckBox1 to the
TextWidth()-result, a part of the text is cut, because the
box itself needs some space too.
Look at the win32 API (win32.hlp) GetSystemMetrics using the
SM_CXMENUCHECK flag:
TControlCanvas *pCanvas = new TControlCanvas;
pCanvas->Control = CheckBox1;
CheckBox1->Width = pCanvas->TextWidth(CheckBox1->Caption) + ::GetSystemMetrics(SM_CXMENUCHECK);
delete pCanvas;
~ JD
 

Re:How to calculate the minimum width of a TControl, such, that it's complete text is displayed?

Hello JD and thanks again,
Quote
CheckBox1->Width = pCanvas->TextWidth(CheckBox1->Caption) + ::GetSystemMetrics(SM_CXMENUCHECK);
So it seems one has to find out the correct value for each TControl
explicitely (in case of a TCheckBox it's the value SM_CXMENUCHECK, in
case of another control it's another).
For TCheckBox I've found out (by trying), I had to add some pixels
(~6) to this since there seems there is a gap between the box and the
starting text...
CheckBox1->Width = pCanvas->TextWidth(CheckBox1->Caption) +
::GetSystemMetrics(SM_CXMENUCHECK)+6;
But can I be sure, that this constant value is the same on all
platforms and on all designs?
As far as I know there exist a kind of window-construction-set, where
the skins could be altered!?
Thanks again,
Michael
Quote

M_R <>wrote:
>
>[...] if I use a TCheckBox CheckBox1 and set CheckBox1 to the
>TextWidth()-result, a part of the text is cut, because the
>box itself needs some space too.

Look at the win32 API (win32.hlp) GetSystemMetrics using the
SM_CXMENUCHECK flag:

TControlCanvas *pCanvas = new TControlCanvas;
pCanvas->Control = CheckBox1;
CheckBox1->Width = pCanvas->TextWidth(CheckBox1->Caption) + ::GetSystemMetrics(SM_CXMENUCHECK);
delete pCanvas;

~ JD
 

Re:How to calculate the minimum width of a TControl, such, that it's complete text is displayed?

M_R <>wrote:
Quote

For TCheckBox I've found out (by trying), I had to add some
pixels (~6) to this since there seems there is a gap
between the box and the starting text...
This surprised me so I searched for an answer. You'll have to
derive you're own check box component or use GetSystemMetrics
and set the width to display only the box and use a TLabel
which will dynamically resize it's self.
The only thing you loose using a TLabel is the mouse over when
running under XP and themes are used. Otherwise, you can get
the same results by adding the appropriate events.
~ JD