Dialog Resize Helper

Environment: Windows 9x, Windows NT/2000

A DlgResizeHelper object helps you keeping a layout while resizing a dialog or - more generally - a window with child windows. For this class I was inspired by an article by John Keogh in DDJ 06/2000 ("Layout Management") and a look at Code Guru revealed some solutions as well. Nevertheless I find my solution more handy (of course ;-): it's C++ (in contrast to Keogh's code) and the class is not derived from CDialog (as most other solutions are) so it can be used with every window.

In the dialog above you some controls want to tell you the meaning of life, but what is it? Now you can simply resize the dialog to read the desired info. Note that you can adjust the resize behaviour of the controls: the lists are resized in height and width, the edit and combo are just resized in width and the buttons stay fixed on the right side where the radio buttons belong to the lower list.

On initialization a DlgResizeHelper stores handles to all child windows of a parent window. Also you can optionally add other windows which shall be resized with the parent window. On resize the DlgResizeHelper resizes the child windows according to the size changes of the parent. By default a child is resized proportional to the parent's size but you can optionally fix the horizontal and/or vertical dimension/position of a child window. E.g. it's reasonable to fix the height of single line edit controls.

Usage (here e.g. with a dialog class):

1. Make your dialog resizable (give it a resizable border)

2. Add a DlgResizeHelper member to your dialog class:

#include "DlgResizeHelper.h"

class CMyDialog : public CDialog
{
 //...
 DlgResizeHelper m_resizeHelper;
 //...
};

3. Initialize the resize helper in OnInitDialog():

BOOL CMyDialog::OnInitDialog()
{
 //...
 m_resizeHelper.Init(m_hWnd);

 // default behaviour is to resize sub windows proportional to the 
 // resize of the parent you can fix horizontal and/or vertical 
 // dimensions for some sub windows by either specifying the 
 // sub window by its hwnd:
 m_resizeHelper.Fix(m_buttonCtrl, 
                    DlgResizeHelper::kLeft, 
                    DlgResizeHelper::kNoVFix);

 // its item id:
 m_resizeHelper.Fix(IDC_PRESS_ME, 
                    DlgResizeHelper::kWidth, 
                    DlgResizeHelper::kNoVFix);

 // or all sub windows with a common class name (especially useful 
 // for single line edit controls)
 m_resizeHelper.Fix("Edit", 
                    DlgResizeHelper::kNoHFix, 
                    DlgResizeHelper::kHeight);

 //...
}

4. Tell the resize helper when the size has changed:

//...
BEGIN_MESSAGE_MAP(CDlg_resizerDlg, CDialog)
 //{{AFX_MSG_MAP(CDlg_resizerDlg)
 //...
 ON_WM_SIZE()
 //...
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

void CMyDialog::OnSize(UINT nType, int cx, int cy) 
{
 CDialog::OnSize(nType, cx, cy);
 m_resizeHelper.OnSize();
}

That's all. In most cases it is ok if the user enlarges a dialog, but shrinking it would destroy the layout. So you might choose to enforce a minimal dialog size by handling the WM_GETMINMAXINFO message (see demo software for an example).

The demo application is a dialog with a bunch of buttons. The name of the buttons reveal their fixed dimensions.

Downloads

Download demo project - 66 Kb
Download source - 3 Kb


Comments

  • License?

    Posted by sandeep on 09/16/2014 06:53am

    Is this licensed under the The Code Project Open License (CPOL)? Your article does not specify, and your code says "COPYRIGHT: Stephan Keil " I can't find a way to contact you directly.

    • Re:License?

      Posted by on 09/18/2014 07:53am

      When code is posted on CodeGuru, the assumption is that it is being made publicly available. If using that code, you should reference the original source of the code (the author).Having said that, we cannot offer legal advice around using someone else's code.

      Reply
    Reply
  • enums

    Posted by Amaravathi on 06/27/2012 04:49am

    what are the enums indicate in EHFix, EVFix

    Reply
  • DlgResizeHelper to resize NTGraph from TabControl

    Posted by wdarsono on 09/19/2011 05:56am

    Hi, I got some problem to resize NTGraph Control (please refer to: http://www.codeproject.com/KB/miscctrl/ntgraph_activex.aspx?msg=4027575#xx4027575xx) from within TabControl. Basically, I am having a Tab control, and inside the Tab control I have different kind of control. For example: For tab 1, I have NTGraph (Active X control); while for tab 2, I have List Control. For some reason, I can't get the NTGraph Control resized properly (the other tab component, List Control, can be resized fine). I have also tried to resize NTGraph Control as a stand-alone (NOT as a component in a Tab Control) and it works fine. To be more specific about the problem, using the DlgResizeHelper I expected to be able to resize all the components inside my dialog window (including those inside my Tab Control). I am able to resize the List Control (as an element in the second Tab of my TabCtrl), however, I am NOT able to "OnSize" to resize the NTGraph Control (as an element in the first Tab of my TabCtrl). It always gives me the "initialized" (OnInit) size of NTGraph Control. Did you ever have any related experience to what I'm facing now, or any idea that might enlighten me on this problem? Thanks and best regards, Willie

    Reply
  • Resizing WebBrowser

    Posted by Legacy on 01/13/2003 12:00am

    Originally posted by: Alexey

    It's seems that this class doesn't work with IE.
    I think that the problem in ::GetNextWindow function which didn't returns WebBrowser control.

    Reply
  • Resize dialog problem

    Posted by Legacy on 10/28/2002 12:00am

    Originally posted by: Satya

    i haev used for the Tab control i haev a list ctrl int the tabs so i have used fo rthat also. it works fine but the The rightside edge of the listctrl is joined with the tabctrl it does not look pretty at all . is ther a solution
    to this

    Reply
  • Doesn't seem to work with Web Browser Control

    Posted by Legacy on 10/18/2002 12:00am

    Originally posted by: alan

    I added a web browser control to the project which is a regsitered active x control.

    It is inherited from CWnd (Webbrowser2.cpp)

    When I tried to fix the top it wouldn't stay fixed.

    Not sure why?

    It resizes ok but you cant fix any of the sides.

    Please verify this for me.

    Reply
  • Sizing problem

    Posted by Legacy on 08/16/2002 12:00am

    Originally posted by: David Hubbard

    Has anyone tried using DlgResizeHelper with a dialog box that has 3 list boxes across, the center list box being a fixed width. I have not been able to get this one to work. Any help would be cool...

    Reply
  • Use in FormWindow

    Posted by Legacy on 08/05/2002 12:00am

    Originally posted by: Normen M�ller

    He folks!

    First of all, that class is a really good one, but i have one problem:

    I use that class in a CFormView, like

    // =====================================================
    void CMyView::OnSize(UINT nType, int cx, int cy)
    {
    //CFormView::OnSize(nType, cx, cy);
    if (!m_bFirstTime) {
    m_resizeHelper.OnSize();
    }
    m_bFirstTime = false;
    }
    // =====================================================

    I have a CListCtrl in that View and everything works fine, but when i resize a single column in that list control, there is no horizontal scrollbar.
    Or if i add some items into that list control and the items "scroll" out of the view, there is no vertical scroll bar.

    What am i doing wrong?

    greetings
    Normen

    Reply
  • How to resize the font of the control?

    Posted by Legacy on 06/18/2002 12:00am

    Originally posted by: yy

    Who can tell me?

    Reply
  • How to manage 2 dialogs at once

    Posted by Legacy on 06/03/2002 12:00am

    Originally posted by: DANIEL

    hello, i have a CTabControl, and would like to know how to make the child dialog on the first tab of the tab control ajust itself along with the parent dialog.
    any way to do this?
    thanks
    -DANIEL

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • This paper introduces IBM Java on the IBM PowerLinux 7R2 server and describes IBM's implementation of the Java platform, which includes IBM's Java Virtual Machine and development toolkit.

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds