Solved

autoresize

Posted on 2001-09-04
13
1,113 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 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

21 Experts available now in Live!

Get 1:1 Help Now