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.

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.


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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unix / Linux grid computing 5 144
iSeries DB2 Query 2 89
pre4 challenge 19 93
Thin secure Windows 10 5 74
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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

914 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

16 Experts available now in Live!

Get 1:1 Help Now