Solved

autoresize

Posted on 2001-09-04
13
1,126 Views
Last Modified: 2013-11-20
How to autosize CStatic and CButton controls? i.e. i want them automatically resize when the text changes
0
Comment
Question by:mykapa
  • 4
  • 3
  • 2
  • +2
13 Comments
 
LVL 4

Expert Comment

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

-=- James.
0
 

Author Comment

by:mykapa
ID: 6453045
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.
0
 
LVL 4

Expert Comment

by:jtwine100697
ID: 6453118
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.
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!

 
LVL 1

Expert Comment

by:TarekEslim
ID: 6468235
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
0
 
LVL 9

Expert Comment

by:Pacman
ID: 6470293
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.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6472333
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.
 
0
 

Author Comment

by:mykapa
ID: 6473594
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
0
 
LVL 1

Expert Comment

by:TarekEslim
ID: 6473702
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.

0
 

Author Comment

by:mykapa
ID: 6473723
If it isn't very large - you can do that
0
 
LVL 9

Expert Comment

by:Pacman
ID: 6474039
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.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6474940
>>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
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6686625
hi mykapa,
Do you have any additional questions?  Do any comments need clarification?

-- Dan
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 200 total points
ID: 7025817
hi mykapa,
Do you have any additional questions?  Do any comments need clarification?

-- Dan
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
Looking for a specific application/software 2 119
move a line in eclipse 3 109
lucky13 challenge 11 157
Is there a simple front-end menu system. 9 118
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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 …
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

679 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