Resize
classResize contains methods to adjust the size and positions of controls within a window as the the size or shape of the window is changed.
The class is defined in the header file Resize.h and implemented in Resize.cpp.
The resizing object can be defined via a pointer:
-
classResize* m_pResize; // dialog window resizing
-
-
if( (m_pResize = new classResize(
-
DIALOG_WIDTH, DIALOG_HEIGHT, // initial size of dialog window
-
0, 0 // width and height adjustments per resize
-
)) == NULL )
-
AfxMessageBox( "Out of memory" );
-
or directly:
-
classResize m_Resize; // dialog window resizing
-
-
, m_Resize( DIALOG_WIDTH, DIALOG_HEIGHT, 0, 0 /*no adjustment per resize*/ )
-
- the latter method fits in better with the object being local to this dialog
- there will not be any need to pass a pointer on to another dialog.
The dialog window dimension parameters above are calculated from the window size defined in the resource file.
e.g. If XXX.RC contains:
-
IDD_XXX_DLG DIALOG DISCARDABLE 0, 130, 640, 145
-
STYLE WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_THICKFRAME
-
then the parameters are:
-
#define DIALOG_WIDTH 960 // width in xxx.rc (640), * 1.5
-
#define DIALOG_HEIGHT 236 // height in xxx.rc (145), * 1.625 (why? (= 13/8))
-
and the conversion factors are from the Microsoft Department of Black Arts.
The WS_THICKFRAME item implies borders which can be grabbed and moved.
When the window is moved or resized, the OnMove or OnSize functions are called.
These are overridden as follows:
-
afx_msg void OnMove(int x, int y);
-
afx_msg void OnSize(UINT nType, int cx, int cy);
-
-
BEGIN_MESSAGE_MAP(xxxDlg, CDialog)
-
//{{AFX_MSG_MAP(xxxDlg)
-
ON_WM_MOVE()
-
ON_WM_SIZE()
-
...
-
-
void VsumrDlg::OnMove( // this dialog window has been moved
-
int x, // new x position of window
-
int y // new y position of window
-
){
-
CDialog::OnMove( x, y );
-
m_Resize.DoOnMove( x, y );
-
}
-
void VsumrDlg::OnSize( // window has been resized by dragging border or by Maximise button
-
UINT nType, // type of resizing
-
int cx, // new window width
-
int cy // new window height
-
){
-
CDialog::OnSize( nType, cx, cy );
-
m_Resize.DoOnResize( cx, cy );
-
PostMessage( WM_COMMAND, IDC_XXX_FRAME_RESIZED, 0 );
-
}
-
- the OnSize function cannot resize the controls directly, but must send a message to the dialog to do it
(the IDC_XXX_FRAME_RESIZED parameter can be any value) -
-
BOOL VsumrDlg::OnCommand( WPARAM wParam, LPARAM lParam ){
-
switch( LOWORD( wParam ) ){
-
case IDC_XXX_FRAME_RESIZED:{
-
m_Resize.SizeCtrlNewY( &m_ctlyyy ); // ... for each control to be moved or resized
-
return TRUE;
-
}...
-
}
-
If resizing is to be done as part of the dialog initialisation (i.e. in OnInitDialog),
then the control resizing statements should be moved to a separate function. e.g.
-
void xxxDlg::PositionControls( // move and resize controls, depending on frame size
-
bool bOriginal // true if original dialog size & position
-
){
-
m_Resize.SizeDlgNewXY( this, bOriginal );
-
m_Resize.SizeCtrlNewY( &m_ctlyyy, bOriginal ); // ...
-
}
-
which is called from within OnInitDialog as well as from OnCommand.
The call to SizeDlgNewXY is included because in this case the window itself must be being resized
(e.g. to fit in some outer frame).
In the Resource File the STYLE statement will probably include WS_CHILD instead of WS_POPUP,
and WS_BORDER instead of WS_THICKFRAME (the resizing being done via the outer window).
There is an extra bOriginal parameter to indicate if this new size is the base size for the dialog.
It should be true in the call from OnInitDialog, and false in the call from OnCommand. i.e.
-
BOOL VsumrDlg::OnInitDialog(){
-
CDialog::OnInitDialog();
-
PositionControls( true );
-
...
-
m_bInitDialogDone = true;
-
}
-
-
BOOL VsumrDlg::OnCommand( WPARAM wParam, LPARAM lParam ){
-
switch( LOWORD( wParam ) ){
-
case IDC_XXX_FRAME_RESIZED:{
-
if( m_bInitDialogDone )
-
PositionControls( false );
-
return TRUE;
-
}...
-
}
-