Question for Answer2000

Posted on 1998-12-06
Last Modified: 2013-11-20
Dear Sir,

I have asked you how to resize the interface components in my application before.

Your suggested solution is:

1. Add a handler for WM_SIZE messages and put the code in there.
2. Use ::IsWindow to check the controls are valid before working on them (OnSize will get called once before the controls are valid)
3. Use MoveWindow to move the controls around.  

Here is an example from one of my form view app's (CTComply derives off CFormView)

void CTComply::OnSize(UINT nType, int cx, int cy)
    CFormView::OnSize(nType, cx, cy);

    if ( !::IsWindow(m_edit.m_hWnd) ) return ;
    if ( !::IsWindow(m_list.m_hWnd) ) return ;

    SizeControls() ;

void CTComply::SizeControls()
   // get client area
   CRect rcClient ;
   GetClientRect( &rcClient ) ;

   // position edit control
   CRect rcEdit ;
   m_edit.GetWindowRect( &rcEdit ) ;
   ScreenToClient( (LPPOINT)(&rcEdit.left) ) ;
   ScreenToClient( (LPPOINT)(&rcEdit.right) ) ;

   m_edit.MoveWindow( rcEdit.left,, rcClient.right - rcEdit.left - 10, rcClient.bottom - - 10 ) ;

    // position list control
    CRect rcList ;
    m_list.GetWindowRect( &rcList ) ;
    ScreenToClient( (LPPOINT)(&rcList.left) ) ;
    ScreenToClient( (LPPOINT)(&rcList.right) ) ;

    m_list.MoveWindow( rcList.left,, rcClient.right - rcList.left - 10, rcList.bottom - ) ;

But I still have a problem in re-sizing the interface components.

Eg. when the width of the window is 600
             height of the window id 400

    a edit box is placed at the window with the upper left co-ordinates is (60,40);

Now, I am resizing the window to width 500, heigth 300.
In this suitation, the edit box need to place at the window with upper-left coordinates (50,30) and the size of the edit box need to be changed also.

From your suggested solution before, I found it only resize the edit box without getting a new upper-left coordinate.

How can I get this?


Question by:kkuu
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2

Expert Comment

ID: 1325807
This bit gets the size of the size of the form (in pixels)
   // get client area
       CRect rcClient ;
       GetClientRect( &rcClient ) ;

This bit gets the previous coordinates of the edit control in screen units (relative to top left of screen)
  CRect rcEdit ;
       m_edit.GetWindowRect( &rcEdit ) ;

This bit converts the screen coordinates to coordinates to coordinates on the form
   ScreenToClient( (LPPOINT)(&rcEdit.left) ) ;
       ScreenToClient( (LPPOINT)(&rcEdit.right) ) ;

This bit moves/resizes the edit control
       m_edit.MoveWindow( rcEdit.left,, rcClient.right - rcEdit.left - 10, rcClient.bottom - - 10 ) ;

The coordinates are in client coordinates (0,0 is top left of the form)

The parameters are top left (x then y), then width, then height

In my example I leave the edit control at the same position and size it to cover the rest of the client area of the form (minus 10 pixels in each of the X and Y coordinates)

Now if you want the top left to always be at 50,30 then change these parameters in the edit control (first two).  You don't specify how you want the size changed.  

Here's an example
m_edit.MoveWindow( 50, 30, 200, 300 ) ;

This will make the top left 50,30
The width 200 and the height 300

Is this clear ?

m_list. stuff was for a 2nd control (my form also had a list on it), so you can ignore this if you don't have a 2nd (or additional controls).


Author Comment

ID: 1325808
Sorry Sir,

I don't know how to get the change of the size of the window......

I want to have the older size of the window, and the new size of the window...
Can I have these two data at the same time in order to specify the size of the interface component change?


Expert Comment

ID: 1325809
I don't understand your 5:47pm comment

The old size/position is given in by the rectangle resulting from
  CRect rcEdit ;
       m_edit.GetWindowRect( &rcEdit ) ;

The new size/position is whatever you specify in the m_edit.MoveWindow( ...etc..) line

If you want to find the change in size, just subtract the difference

If you want to change the size by a certain amount use

m_editMoveWindow( rcEdit.left,, rcEdit.Width() + dx, rcEdit.Height + dy ) ;

where dx and dy are the amount to change the x,y sizes by respectively (positive = increase in size, negative is a decrease in size)

Does this answer your question ?  If not you better explaining your difficulty a bit more clearly for me.

Technology Partners: 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!


Expert Comment

ID: 1325810
Ooops to make the old size meaningful add in the 2 ScreenToClient lines after the 1st code fragment, that is to say :-

   CRect rcEdit ;
   m_edit.GetWindowRect( &rcEdit ) ;
   ScreenToClient( (LPPOINT)(&rcEdit.left) ) ;
   ScreenToClient( (LPPOINT)(&rcEdit.right) ) ;

Author Comment

ID: 1325811
Dear Answers2000,

Sorry for replying you late!!!

For the previous question, my problem is:

Before resizing the window, for example the size of the window is 400x400....
after resizing the window, the size of the window becomes 450x350.....

My question is how to get these two data in my program....and I want to use these two data at the same time..Can I do like this? Do you understand?

Also, I have another question in re-sizeing the edit box......
In my edit box. I have delcared a CString type variable m_edit1.I
I tried to resize the edit box by using the method you suggested but the followiing error occurs:

error C2039: 'GetWindowRect' : is not a member of 'CString'
error C2039: 'MoveWindow' : is not a member of 'CString'
Can I resize a edit box....Until now, I can reszie a button and a listctrl.



Accepted Solution

Answers2000 earned 50 total points
ID: 1325812
OK first off the CString problem.

When you create a member variable you get a choice of what type

When you go to the member variables dialog, and double clickon your edit's id, say IDC_EDIT, a little dialog pops up.  You can set either a string or window type variable.  Your problem is you picked a CString, and you should have picked a CEdit.

To fix that problem
1. Ctrl-W bring up class wiz for your dialog
2. Go to member variables
3. You should see something like, in the list box
4. Highlight this line
5. Click Delete Variable
6. Now make sure that line is highlighted again
7. Pick Add Variable
8. When the dialog popups up entitled "Add Member Variable", it should have fields for Member Variable Name (an edit control), Category (a combo box) and Variable Type (a combo box)

In Member Variable name, type "m_edit"  - without the quotes
In Category pick "Control"
In Variable Type pick "CEdit"

This fixes the 2 compile errors you're getting

Your code should now compile, if you look at the previous comments in
[this question]
this explains the sizing code.

Remember the window can only one size at a time.  The reason the fix didn't work before, is it wouldn't compile, but if you re-read, I think it should make sense.

If not ask another follow up question.  

By the way, you don't need to keep opening new questions, I'll keep an eye on your progress.


Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
substring method in java 1 136
fizzArray2 challenge 1 102
EvenOdd challenge 10 146
matchUp  challenge 9 124
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

749 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