Click here to Skip to main content
13,253,507 members (33,151 online)
Click here to Skip to main content
Add your own
alternative version


45 bookmarked
Posted 28 Nov 2000


Rate this:
Please Sign up or sign in to vote.
Extension of Paolo Messina's CResizableDialog concept


The inspiration for this coding project was the result of a) Paolo Messina's article on his CResizableDialog code, and b) me mentioning I had adapted a CFormView to use a CResizablePage in an embedded property sheet.

The CResizablePage in a formview was a minor exercise compared to this. What I've come up with is a CResizableFormView. This code is based entirely on Paolo's CResizableDialog code.

If you're not interested in how I did it, Skip to the bottom of this article to see how to implement it yourself.

The Concept

It seemed to me that if a dialog box could benefit from this technique, then so could a formview, because after all, a formview is nothing more than a view that acts like a dialog box. It even uses a resource template like a dialog box.


The initial coding was fairly straightforward. I created a CFormView-derived class called CResizableFormView. Next, I copied all of the utility functions (functions not contributed by ClassWizard). Then, I evaluated the need/availability of some of the windows messages Paolo's code was using. I ended up only needing to handle the WM_SIZE command. Lastly, I decided that the min/max window size code was not applicable to a formview, so I deleted the code involving that particular aspect of CResizableDialog.


After getting the code to compile, I tried deriving a new class based on my new CResizableFormView. After some experimentation, I discovered that I could not use the AddAnchor function until AFTER the InitialUpdate had completed. If I didn't follow this rule, the controls would not resize correctly.
void CFormview2View::OnInitialUpdate()

    // put our code AFTER the stuff ClassWizard gave us.

Minor Issues

After playing around with resizing for a while, I discovered that if I made the view window smaller than the original dialog template, a couple of bad things happened involving the controls that were "anchored".
  • The listbox disappeared altogether and would only partially appear as I made the view horizontally larger.
  • The groupbox around the radio controls would just keep resizing until nothing was left (of the groupbox).

It was obvious at that point that I had to include some code to restrict the controls from resizing once they reached (or were smaller than) their original size. I also assumed that I had to be mindful of getting the correct original size, meaning I had to determine each control's size before the window was allowed to be resized the first time. Assuming that the programmer could decide to do something other than use a call to MFC's ResizeParentToFit, I had to make sure to get the control's info immediately after the view was created, but before it was resized. I wanted to impact as little of Paolo's original code as I could manage, so the fix was a while coming.

To remedy the situation, I created a CTypedPtrArray that held a new structure. This structure contains the original size and location of each "anchored" control, as well as its HWND and control ID. To support the structure, I had to also write a couple of new functions. Here are the details:

void CResizableFormView::AddResizedControls()

This function is responsible for determining the HWND and window size / location of the specified control, as well as storing its anchor positions. The parameter list is identical to the original AddAnchor function. Once all of this information was gathered, the control was added to the new CTypedPtrArray for safe-keeping.

void CResizableFormView::AnchorControls()

This function actually calls AddAnchor for all of the controls the programmer specified when he/she called the AddResizedControls function. The significance of this will be evident later.


In order to implement the sizing/positioning fix, A new order of execution was required in the CResizableFormView-derived class. The OnInitialUpdate function now looks like this:
void CFormview2View::OnInitialUpdate()
    // jms - 11/28/00 - new code starts
    // We do this here because we need to get the *original* size of the 
    // controls *before* the dialog is sized the first time.  I don't know 
    // what will happen if we do it after, but I didn't want to find out.
    // Since we do this here, I decided to pass the parameters needed by 
    // the AddAnchor() function to eliminate the need to call AddAnchor 
    // from this function (after the view had been resized).
    AddResizedControl(IDC_LIST1, TOP_LEFT, BOTTOM_RIGHT);
    AddResizedControl(IDC_GROUP1, TOP_LEFT, BOTTOM_LEFT);
    // jms - 11/28/00 - new code stops


    // Normally, we would put the calls to AddAnchor AFTER we call 
    // ResizeParentToFit().

    // jms - 11/28/00 - new code starts
    // Now anchor the controls we specified earlier in this function. We call 
    // this function because it cycles thru the list of controls that are to 
    // be resized. This prevents us from having to call AddAnchor (lots of 
    // redundant typing at this point).


But what if you wanted the view to NOT resize itself according to the dialog template. That's easy. Just calculate the size of the parent window of the formview, and call MoveWindow before exiting OnInitialUpdate. Here's an example:

void CFormview2View::OnInitialUpdate()
    AddResizedControl(IDC_LIST1, TOP_LEFT, BOTTOM_RIGHT);
    AddResizedControl(IDC_GROUP1, TOP_LEFT, BOTTOM_LEFT);
    AddResizedControl(IDC_IPADDRESS1, BOTTOM_RIGHT);


    // Here's where the difference lies.  We don't want to call ResizeParentToFit() until
    // AFTER we calculate the true (initial) size of the parent frame. 
    CRect rectFrame;
    CFrameWnd* pFrame = GetParentFrame();
    CSize size = rectFrame.Size();

    // Now, we can call ResizeParentToFit() and anchor the controls.

    // Finally, we need to actually resize the frame to its original dimensions.  This
    // allows the anchor code to work  with its existing code.
    pFrame->MoveWindow(rectFrame, TRUE);

The Final Word

To implement the CResizableFormView, perform the following steps:

  1. Create a CFormView class and its dialog template if necessary.
  2. Change the base class of your CFormView-derived class to CResizableFormView.
  3. #include "ResizableFormView.h" in your derived formview's header file.
  4. If the function isn't already there, use ClassWizard to override the OnInitialUpdate function.
  5. Call AddResizedControls for each control that will be resized/repositioned. Do this BEFORE the call to the base class' OnInitialUpdate.
  6. Call AnchorControls. Do this AFTER the call to ResizeParentToFit


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

John Simmons / outlaw programmer
Software Developer (Senior) Paddedwall Software
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

You may also be interested in...

Comments and Discussions

QuestionUnable to open project file with visual studio 2008 Pin
HazardNguyen18-Apr-17 23:11
memberHazardNguyen18-Apr-17 23:11 
QuestionI want to fit the dialog in form?? Pin
johnthecoder25-Aug-08 20:50
memberjohnthecoder25-Aug-08 20:50 
QuestionMaximized MDI window problem Pin
sdr27-Jun-06 5:10
membersdr27-Jun-06 5:10 
GeneralMissing .dsp file to open the project! Pin
Perryz4-Dec-04 18:25
memberPerryz4-Dec-04 18:25 
GeneralRe: Missing .dsp file to open the project! Pin
batsword10-Sep-10 22:08
memberbatsword10-Sep-10 22:08 
Questionnot resizing? Pin
lagonz4-May-04 17:45
memberlagonz4-May-04 17:45 
AnswerRe: not resizing? Pin
lagonz4-May-04 17:52
memberlagonz4-May-04 17:52 
GeneralGot an Error!! Pin
*Dreamz16-Apr-04 4:24
member*Dreamz16-Apr-04 4:24 
GeneralBad Dialog Template Pin
niallmack4-Nov-03 13:19
memberniallmack4-Nov-03 13:19 
GeneralRe: Bad Dialog Template Pin
*Dreamz16-Apr-04 4:09
member*Dreamz16-Apr-04 4:09 
GeneralBug and Solution - Resizing-problem Pin
Siegmund Frenzel7-Aug-03 23:05
memberSiegmund Frenzel7-Aug-03 23:05 

please try the following:
Open the Demo-Application. Make it small enough, that you can see scrollbars.
Now scroll to the right and bottom.
Now make the app (for example one pixel) bigger. The controls were drawed at the wrong places.

The problem is that GetClientRect returns the Size of the visible area. If you have scrolled to the right bottom the coordinates (0,0) of the area are "somewhere" on the original form. In "AddAnchor" you remember the margins between the Border of the View and the control. After scrolling and resizing the controls are positioned x/y pixels from (0,0) which is not the left top border of the View but of the visible area.

You have to call GetTotalSize() to retrieve the total size of the frame (not the visible one)

In "ArrangeLayout" you have to calculate which is bigger: the actual size or the total size and use that value for calculating. Do not use the initial size. ==> If you have a very small frame, the created view is mostly smaller than the original Formview. (The frame has initial scrollbars)

At the end you have to take the offset into account: The area from the (0,0)-coordinates from the whole view to the (0,0)-coordinates of the visible area.
When you have calculated where a control should be positioned you have to subtrate the difference between the two (0,0) Points.

Enough talked nonsense. Here is the code:
<br />
void CResizableFormView::AddAnchor(HWND wnd, CSize tl_type, CSize br_type)<br />
{<br />
...<br />
...<br />
...<br />
	CSize tl_margin, br_margin;<br />
<br />
	if (br_type == NOANCHOR)<br />
	{<br />
		br_type = tl_type;<br />
	}<br />
<br />
	//Da wird eine Scrollview haben, die maximale Größe nehmen<br />
	CSize TotalSize = GetTotalSize ();<br />
<br />
	// calculate margin for the top-left corner<br /> = objrc.left -  * / 100;<br /> = - * / 100;<br />
 	<br />
	// calculate margin for the bottom-right corner<br /> = objrc.right - * / 100;<br /> = objrc.bottom - * / 100;<br />
<br />
	// add to the list<br />
	m_plLayoutList.AddTail(new Layout(wnd, tl_type, tl_margin, br_type, br_margin, hscroll, refresh));<br />
}<br />

<br />
void CResizableFormView::ArrangeLayout()<br />
{<br />
...<br />
...<br />
...<br />
<br />
	CSize TotalSize = GetTotalSize ();<br />
<br />
	//What is bigger. Actual Window or Initial Framesize.<br />
	//If the actual Window is bigger, we couldn't have scrollbars<br />
	int nMaxBreiteFenster = max (wndrc.Width(),;<br />
	int nMaxHoeheFenster = max (wndrc.Height(),;<br />
<br />
<br />
	// calculate new top-left corner<br />
	newrc.left   = pl-> + nMaxBreiteFenster * pl-> / 100;<br />    = pl-> + nMaxHoeheFenster * pl-> / 100;<br />
	// calculate new bottom-right corner<br />
	newrc.right  = pl-> + nMaxBreiteFenster * pl-> / 100;<br />
	newrc.bottom = pl-> + nMaxHoeheFenster * pl-> / 100;<br />
	// jms - 11/28/00 - new code starts<br />
	// Restrict controls from being sized smaller than their size on <br />
	// the dialog template. I could have mingled this code with the original<br />
	// four lines above, but I wanted Paolo to be abe to easily find what I <br />
	// did.<br />
//			FORMCONTROL* pCtrl = GetFormControl(pl->hwnd);<br />
//			newrc.left   = max(newrc.left,   pCtrl->rect.left);<br />
//    = max(,    pCtrl->;<br />
//			newrc.right  = max(newrc.right,  pCtrl->rect.right);<br />
//			newrc.bottom = max(newrc.bottom, pCtrl->rect.bottom);<br />
			// jms - 11/28/00 - new code stops<br />
<br />
	//Die Scrollviews stehen irgendwo. Linke obere Ecke berechnen<br />
	//Calculate the offset between (0,0) of Form and visible area<br />
	CPoint ScrollPos = GetScrollPosition ();<br />	-= ScrollPos.y;<br />
	newrc.left	-= ScrollPos.x;<br />
	newrc.bottom-= ScrollPos.y;<br />
	newrc.right -= ScrollPos.x;<br />
<br />
<br />
<br />
		if (!newrc.EqualRect(&objrc))<br />
......<br />
......<br />
<br />
}<br />
<br />

I changed following line for testing. Don't know if it changes something.
if (pl->adj_hscroll ) // changed in ->
if (pl->adj_hscroll && NULL)
You can (if it works) of course delete the whole if () {...} thing

I hope this was at least a little understandably.
GeneralDemo Link Pin
Asha_krishna10-Jul-03 14:27
memberAsha_krishna10-Jul-03 14:27 
GeneralDemo download link not working Pin
Lobster B15-May-03 21:17
memberLobster B15-May-03 21:17 
Questionwhere is formview2.dsp file ??? Pin
ijt7epn30-Dec-02 6:09
memberijt7epn30-Dec-02 6:09 
GeneralFit a dialog into a Window Pin
Anonymous17-Apr-02 7:47
memberAnonymous17-Apr-02 7:47 
GeneralUpdated Article Pin
John Simmons / outlaw programmer7-Jun-01 2:58
memberJohn Simmons / outlaw programmer7-Jun-01 2:58 
Questionis ResizeParentToFit() compulsory ???? Pin
wimel16-May-01 4:15
memberwimel16-May-01 4:15 
AnswerRe: is ResizeParentToFit() compulsory ???? Pin
John Simmons / outlaw programmer17-May-01 3:51
memberJohn Simmons / outlaw programmer17-May-01 3:51 
AnswerRe: is ResizeParentToFit() compulsory ???? Pin
John Simmons / outlaw programmer17-May-01 4:14
memberJohn Simmons / outlaw programmer17-May-01 4:14 
GeneralRe: is ResizeParentToFit() compulsory ???? Pin
wimel22-May-01 22:29
memberwimel22-May-01 22:29 
GeneralRe: is ResizeParentToFit() compulsory ???? Pin
John Simmons / outlaw programmer24-May-01 5:04
memberJohn Simmons / outlaw programmer24-May-01 5:04 
GeneralRe: is ResizeParentToFit() compulsory ???? Pin
Wimel7-Jun-01 2:26
memberWimel7-Jun-01 2:26 
GeneralRe: is ResizeParentToFit() compulsory ???? Pin
John Simmons / outlaw programmer7-Jun-01 2:44
memberJohn Simmons / outlaw programmer7-Jun-01 2:44 
GeneralActiveX controls and scrolling Pin
Shane Warren2-Apr-01 11:18
memberShane Warren2-Apr-01 11:18 
GeneralFix for disappearing radio buttons/checkboxes Pin
Matt Philmon8-Mar-01 20:13
memberMatt Philmon8-Mar-01 20:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 7 Jun 2001
Article Copyright 2000 by John Simmons / outlaw programmer
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid