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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.


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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Expand data scrubbing tool 13 32
has22 challenge 11 93
Execute multiple curl cmds with sleep and send output to file 10 97
sumHeights  challenge 17 70
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: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

776 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