A Simple Dialog Layout Manager

Thanks to Marc Flerackers for updating the Article.


If you often use Dialogs and want to resize them you will have noticed, that there is no feature in the MFC that helps you. You have to do it by hand. Maybe you have seen the GridBagLayout in Java(tm), then this one will look familiar to you. The idea behind is to split the dialog in Panes. Each pane has some attributes an can also contain Panes itself. Resizing your Dialog can now be as easy as requesting the root pane to fit in the current client area! Actually this is done automatically in the OnSize handler, so you just have to define the layout.

Instructions:

  1. Add DialogMgr.cpp to your current project (e.g. Menu Project->Add to project->Add files)
  2. Create a new dialog or skip to the next point if you already have one. I assume this class to be named CMyDialog. (Note: Do not forget to make the Dialog resizable, i.e. in the Dialog Editor change the border style to resizing)
  3. Change the Base of your Dialog from CDialog to CDialogMgr (simple search for all references to CDialog and replace them), do not forget to include the header.
  4. In your class Declaration add a DECLARE_LAYOUT like this:
    
    class CMyDialog : public CDialogMgr
    {
    public:
    	CMyDialog(CWnd* pParent = NULL);   // standard constructor
    	DECLARE_LAYOUT();
    
    	[...]
        
  5. Now comes the really important step: You have to define what your Layout looks like. Assume this to be your Dialog template (I added the ControlIDs as red text because I will refer to them later):



    If we want to have this Dialog layouted automatically, we have to define the panes. In this example I decided to define the panes like this:



    The root pane is marked red and contains 5 Items. The 2nd and 4th Items are Panes themself (marked blue), which contain respectively 2 and 3 Items. To define this layout you would have an OnInitDialog like this:
    
    BOOL CMyDialog::OnInitDialog() 
    {
    	CDialogMgr::OnInitDialog();
    
    	// define the Layout
    	CPane *newItemPane=new CPane ( this, HORIZONTAL );
    
    First of all the first Subpane is created. It is a horizontal pane. Next you add the Items:
    	newItemPane->addItem ( IDC_NEW_ITEM, GREEDY );
    	newItemPane->addItem ( IDC_ADD_ITEM, NORESIZE );
    
    The second SubPane is created similar:
    
    	CPane *bottomPane=new CPane ( this, HORIZONTAL );
    	bottomPane->addItem ( paneNull, GREEDY );
    	bottomPane->addItem ( IDOK, NORESIZE );
    	bottomPane->addItem ( IDCANCEL, NORESIZE );
Note the use of paneNull, this is not an actual Pane but a placeholder. The Term greedy in this context means that it will eat up all space that is left. The second Item is the OK Button, the CPaneBase::NORESIZE means, that it's  size is fixed. The actuall size could be appended as additional parameters, but if you leave them it will take the size from the current size of the control.    An Item can have several mode flags. A flag of 0 like in the first line  denotes a greedy Item, you could also add Items with an absolute size or some items with a relative size in percent. The Layout Engine first allocates space to ABSOLUTE_* items. What is left will  be allocated to RELATIVE items and the remaining space is divided into equal pieces for greedy items.

	m_pRootPane=new CPane ( this, VERTICAL );
Now the root pane is created (as denoted by m_pRootPane).
	m_pRootPane->addItem ( IDC_NEW_ITEM_STATIC, NORESIZE );
	m_pRootPane->addPane ( newItemPane, ABSOLUTE_VERT );
	m_pRootPane->addItem ( IDC_ITEM_LIST_STATIC, NORESIZE );
	m_pRootPane->addItem ( IDC_ITEM_LIST, GREEDY );
	m_pRootPane->addPane ( bottomPane, ABSOLUTE_VERT );
We want the 4th item (the ListBox) to grow and shrink. The 2nd and 5th item will preserve their height, but the panes will size in the horizontal direction. We use a special feature here: ABSOLUTE_VERT denotes that the vertical  size is fixed. As we did not provide one, the maximum height of all Items of the panes are computed and used as the fixed height.    
	UpdateLayout ();
Do not forget to call this one or the Layout will only get effective when  you resize the Border.    

	return TRUE;  // return TRUE unless you set the focus to a control
		      // EXCEPTION: OCX Property Pages should return FALSE
}
    
  1. Optionally: Add a call to Load() and Save() to (Re-)Store the Dimensions in/from the registry.
  2. That's it! Now the Dialog is automatically resized. Note that it also computes a minumum tracking size! Please take a look at the headerfile (DialogMgr.h) as all the features and functions are described there.

Version 1.2

Reworked the support for TabCtrls and added some more constants. Added the ability for somewhat more complicated layouts. Consider this example:



What is different here? First of all we would like to have the ListCtrl placed inside the tab, which is a pane. So I derived a new class from CPane named CPaneTab, which also resizes a TabCtrl along with its own size and places all childs within the tab (see CTabCtrl::AdjustRect() ). It also manages the minimum tracking size so that the Tabs are always visible! The code to describe the layout above looks like:


	// define the layout
	////////////////////

	// the bottom pane
	CPane*	paneBottom = new CPane(CPane::HORIZONTAL);
	paneBottom->addItem( new CPaneItem( NULL, CPaneBase::GREEDY));			// Greedy
	paneBottom->addItem( new CPaneItem( IDOK, this, CPaneBase::NORESIZE));
	paneBottom->addItem( new CPaneItem( IDCANCEL, this, CPaneBase::NORESIZE));


	// (green) holding a spacer and the ListCtrl
	CPane*	paneList = new CPane(CPane::VERTICAL);
	paneList->addItem( new CPaneItem( &m_list, CPaneBase::GREEDY));

	// (blue) the pane with the tab attached with an ExtraBorder
	CPane* paneTab  = new CPaneTab(&m_tab, CPane::HORIZONTAL);
	paneTab->addItem( paneList );

	// (red) the root pane 
	m_pRootPane = new CPane(CPane::VERTICAL);
	m_pRootPane->addItem( paneTab );
	m_pRootPane->addItem( paneBottom, CPaneBase::ABSOLUTE_VERT );

	UpdateLayout();

Download demo project - 81 KB

Date Last Updated: April 5, 1999



Comments

  • Whitened Mother of Pearl Dial having Diamonds Watch.

    Posted by Ricsninny on 06/21/2013 09:57am

    レディースウォッチ異なるデ [url=http://www.watchjpyoi.com/]カシオ 腕時計[/url] ザイン。で見つけることができますに表示から構成されてまた、構成 [url=http://www.watchjpyoi.com/]シチズン 腕時計[/url] されています含むもずっと等 [url=http://www.watchjpyoi.com/]グッチ 時計[/url] 同じ多機能メンズ腕時 [url=http://www.watchjpyoi.com/]www.watchjpyoi.com/[/url] シチズン 腕時計 計を。これらの時計は、 [url=http://www.oakleburanndo.com/]オークリー サングラス[/url] さらにもなる銀メッキま [url=http://www.oakleburanndo.com/]オークリー ゴーグル[/url] たは金メッキそれがあるように活気望 [url=http://www.oakleburanndo.com/]オークリー レンズ[/url] ましいすべてへのいずれ [url=http://www.oakleburanndo.com/]www.oakleburanndo.com/[/url] オークリー サングラス かまたはすべてにすべてま [url=http://www.seikowatchannka.com/]セイコー 腕時計[/url] たはいずれかへ。 Oneフェミニンスタ [url=http://www.seikowatchannka.com/]セイコー 時計[/url] イルカシオの時計はあるかもしれない有 [url=http://www.seikowatchannka.com/]seiko 腕時計[/url] 名なカシオWaveseptorデザイナ時計。時 [url=http://www.seikowatchannka.com/]www.seikowatchannka.com/[/url] セイコー 腕時計 計の次のカテゴリがされるがありますでしょう [url=http://www.watchkeiya.com/]シチズン 時計[/url] カシオの高級時計順序。 ものこれら [url=http://www.watchkeiya.com/]セイコー 腕時計[/url] は最も高価です。非常に一般 [url=http://www.watchkeiya.com/]カシオ 腕時計[/url] 的なケースカシオス [url=http://www.watchkeiya.com/]www.watchkeiya.com/[/url] カシオ 腕時計 トリートライダーです [url=http://www.poloralphlaurenya.com/]ラルフローレン[/url] それはだの最高の仲間に関してへジ [url=http://www.poloralphlaurenya.com/]ポロラルフローレン[/url] ョギングやスポーツマンに [url=http://www.poloralphlaurenya.com/]ラルフローレン ポロシャツ[/url] 関係|を取得する。また、す [url=http://www.poloralphlaurenya.com/]www.poloralphlaurenya.com/[/url] ポロラルフローレン ることができますで利用岩または都市地形登山者やバイカー中。

    Reply
  • The brand new Arrivals of Chloe Identical Totes.

    Posted by Gadbalach on 05/13/2013 02:14pm

    概要グッチ女性Chiodo約見観察 YA122505グッチ腕 [url=http://www.guccikabanya.com/]グッチ アウトレット[/url] 時計策定氏グッチによ [url=http://www.guccikabanya.com/]グッチ 長財布[/url] ってその内部いくつかの重 [url=http://www.guccikabanya.com/]グッチ 財布[/url] 要な資質を持っている。そのうち [url=http://www.guccikabanya.com/]www.guccikabanya.com/[/url] グッチ アウトレット のいくつかはになりがちその精 [url=http://www.guccisweethome.com/]グッチ アウトレット[/url] 度、スタイル、方法 [url=http://www.guccisweethome.com/]グッチ 財布[/url] それがあった設 [url=http://www.guccisweethome.com/]gucci アウトレット[/url] 計。グッチの時計は、それらのいくつか時 [url=http://www.jaguccibags.com/]グッチ アウトレット[/url] 計はその持ってきで設計 [url=http://www.jaguccibags.com/]GUCCI アウトレット[/url] に属する|に|から| |中の最も耐久性のある材料世界。そのコレク [url=http://www.jaguccibags.com/]グッチ 財布[/url] ションは、それぞれ提供 創作時計ま [url=http://www.jaguccibags.com/]www.jaguccibags.com/[/url] グッチ アウトレット たは石英機械式の選 [url=http://www.tokuyuugucci.com/]gucci 財布 アウトレット[/url] 択を。それはあっで作らた様 [url=http://www.tokuyuugucci.com/]gucci アウトレット[/url] 々な材料などなどゴールド [url=http://www.tokuyuugucci.com/]gucci 財布 レディース[/url] 、ステンレス鋼と魔法。貴 [url=http://www.tokuyuugucci.com/]www.tokuyuugucci.com/[/url] gucci 財布 レディース 重な石これはまた、半貴 [url=http://www.designguccijp.com/]Gucci アウトレット[/url] 石もが含まれて [url=http://www.designguccijp.com/]グッチ アウトレット[/url] います。グッチ時計値?数ル [url=http://www.designguccijp.com/]グッチ 財布[/url] ピーから様々な膨大な数の多数のルピー.絶対的な豪 [url=http://www.designguccijp.com/]www.designguccijp.com/[/url] グッチ 財布 華さと、それが使用をさて、これらの時計が同一視されている最大の販売ブランド地球上。

    Reply
  • ivtyaa,lv outlet online,ijikrg

    Posted by zh0ggj on 03/20/2013 10:03am

    Ucwkucp Nyuygcu louis vuitton wallet for men Oodnunf Hkbkf http://slouisvuittonwallet.webs.com Mdwifl Libyxty authentic used louis vuitton Suobdaa Ietwifd http://lvhandbagswholesale.webs.com/ Wbghjna Fkggobj louis vuitton wallets for men Sbexnuu Yyifblh http://louisvuittonwalletl.webs.com Bcywcrw Yrknola cheap louis vuitton wallets for men Yoooabp Xfyvv http://cheaplouisvuittonl.webs.com Syaxouw Jdmhsoc louis vuitton handbags cheap Afrafex Htzjehg http://louisvuittonbagl.webs.com/ http://www.as3z.cn/oa/web/review.asp?NewsID=328 http://wayneross.net/CGI-BIN/yabb/YaBB.pl?num=1363737896/0 http://fastapp.baison.com.cn/ http://ggamerz.money-slave.biz/forum/viewtopic.php?p=4782348#4782348 http://www.wanyiad.cn/bbs/thread-281230-1-1.html

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

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • In this webinar, IDC featured speaker Steve Conway, Vice President of High Performance Computing, will present an update on the global x86 HPC cluster market. The presentation will include IDC's five-year forecast for the medium- to large-scale technical computing and data analysis emerging markets by systems, processors and application middleware. Cray's featured speaker, John Lee, Vice President of Cray Cluster Advanced Technology Systems, will present the new Cray® CS400™ cluster series based on …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds