Link to home
Start Free TrialLog in
Avatar of mykapa
mykapaFlag for Lithuania

asked on

autoresize

How to autosize CStatic and CButton controls? i.e. i want them automatically resize when the text changes
Avatar of jtwine100697
jtwine100697
Flag of United States of America image

You may be able to have the control capture/handle the WM_SETTEXT message, and then determine the required width for the control.

-=- James.
Avatar of mykapa

ASKER

That sounds rather easy, but how to determine required width and height of control?

Note: don't forget the height of control. The height is even more important, because i want to layout controls in dialog.
Obtaining a DC for the control/window should give you access to the font information used for drawing the text for the control; see CDC::GetTextExtent(...) for more information.  

Once you have the required size for the text, you determine the frame, border, edge, etc. width/height using ::GetSystemMetrics(...).  Using those values, you should be able to provide a correct size for the control to resize itself to.

BTW: There are layout libraries out there (some of them free) that may be able to handle this for you.  If possible, you might not want to reinvent the wheel.

-=- James.
Avatar of TarekEslim
TarekEslim

Well,

To resize the CStatic/CButton use:
1. GetTextWidthEx to know the text width in points.
2. MoveWindow to resize the CWnd of the control to the new size suitable.

To let that occures automatically, you may derive a class from the CStatic/CButton and overwrtite the SetWindowText method to set the text and do steps 1 & 2.

You should take care that MoveWindow can resize the window despite the z order of the other controls. i.e it may cover other controls in the client area.

Regards

Tarek
TarekEslim,

please don't post answers since other comments can also be right answers. Especially when your answer has the same content as jtwine's comments.
Avatar of DanRollins
TarekEslim,
Please post comments, not answers.  ESPECIALLY when your post does not answer the question.

mykapa,
You can learn how large to make a static control by using code like this:

CString sText="Text to Draw";

CWnd* p= GetDlgItem(IDC_MyStatic);
CDC* pDC= p->GetDC();
CRect rc;
p->GetClientRect( &rc );
pFC->DrawText( sText, &rc, DT_CALCRECT );

As the docs on DrawText say,...
If there are multiple lines of text, DrawText will use the width of the rectangle pointed to by lpRect and extend the base of the rectangle to bound the last line of text. If there is only one line of text, DrawText will modify the right side of the rectangle so that it bounds the last character in the line. In either case, DrawText returns the height of the formatted text, but does not draw the text.

So, after calling this, the rc.bottom may be larger.  At that time, you may use MoveWindow to increase the size.

Also note that if you add the DT_WORDBREAK flag

   pFC->DrawText( ...., DT_CALCRECT | DT_WORDBREAK  );
   
The fn will perform wordwrapping while attempting to keep the window the same width.  See the docs on DrawText for more details.

=-=-=-=-=-
Assuming this is in a dialog box... you will need to ensure that the new height of the window does not extend so far down that it overlaps some controls beneath it.  A Recent article in MSDN magazine described a class that lets a window or dialog box do its own layout automatically.  It is overkill if all you need to do is resize a CStatic and there is no chance that doing so will cover up another windows.  But if that is a concern, see

http://msdn.microsoft.com/msdnmag/issues/01/07/winmgr/winmgr.asp

-- Dan

P.S. Feel free to click [Reject Answer] on TarekEslim's post since it is far from a complete answer.
 
Avatar of mykapa

ASKER

I agree with Dan. TarekEslim's just gives an idea, but it is far from complete answer.

I'll look through that link, Dan provided, maybe i'll smth
Helo Dan & mykapa,

sorry for posting an answer instead of comment. In fact I'm new to this community, and do not have good experiences with it.

Now back to Dan comment:
1. You use DrawText, and this draws text on the DC, not using CStatic as mykapa asked in his question.
2. You did not comment on the part of the CButton!

To auto enlarge the CStatic/CButton, you must resize the CWnd rectangle associated with the control. Thus you should use MoveWindow. example:

CStatic *MyCStatic;
MyCStatic = (CStatic *)GetDlgItem(CStatic control ID);

//now calculate the new text width in this CDC and say its //x points
// You can use pCDC->GetTextWidthEx("The new text")
// or you can use any other way

RECT newSize;
// set in this rect coordinates the new size/locations for //the control. It should be suitable to include the //rectangle of the new text.

MyCStatic->MoveWindow (newSize cooredinates);

// You should be aware of the syntex of each command and
// the data format passed to it.

The same can be held for the CButton.

Sorry again for posting answer.

Regards

Tarek

if mykapa wants real sample code, I can paste you.

Avatar of mykapa

ASKER

If it isn't very large - you can do that
What if the static uses a own font, say Arial 24 pt ?
Is this font automatically selected in the given DC ?

Maybe you've to check this.
>>Now back to Dan comment:
>>1. You use DrawText, and this draws text on the DC, not
>> using CStatic as mykapa asked in his question.
>>2. You did not comment on the part of the CButton!

The DT_CALCRECT flag causes the fn to *not do any drawing* only to modify the rect parmater with the new size.  The CButton is the same ... calculate the desired size, then use MoveWindow to change its size.

>>What if the static uses a own font, say Arial 24 pt ?

That's why you should use pwnd->GetDC() so that you get the DC that has already had the correct font selected.

-- Dan
hi mykapa,
Do you have any additional questions?  Do any comments need clarification?

-- Dan
ASKER CERTIFIED SOLUTION
Avatar of DanRollins
DanRollins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial