Question for Answer2000

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?


Who is Participating?
Answers2000Connect With a Mentor Commented:
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.

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).

kkuuAuthor Commented:
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?

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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.

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) ) ;
kkuuAuthor Commented:
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.


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.