Click here to Skip to main content
15,867,568 members
Articles / Desktop Programming / MFC
Article

Expanding and collapsing Dialogs

Rate me:
Please Sign up or sign in to vote.
4.64/5 (15 votes)
9 Jan 2000CPOL 143.8K   4.1K   66   19
This article gives you the ability to make your dialogs expand or contract, depending on how much information the user wishes to view.

Dialog in collapsed state

Dialog in expanded state

Sometimes it may be useful to show only the important part in a dialog and have a button usually labeled "More" that expands the dialog when the user clicks on it. Once the dialog is expanded, the "More" button displays "Less" and as soon as the user clicks on the "Less" button, the dialog will shrink to its initial size.

The CExpandDialog class performs this for you. All you have to to is add the DialogExpander.cpp file to your project and make some minor changes in your dialog class and template.

Modify the header file of your dialog class like this:

/////////////////////////////////////////////////////////////////////////////
// CExpandDialogDlg dialog

#include "DialogExpander.h"    //<<<< add here

...
class CExpandDialogDlg : public CDialog
{
...
protected:
...
    CExpandDialog m_ExpandDialog ;    //<<<< add here
...
}

Modify the OnInitDialog function dialog class like this:

BOOL CExpandDialogDlg::OnInitDialog()
{
    CDialog::OnInitDialog();


    m_ExpandDialog.Initialize(this, FALSE, IDC_MOREBUTTON, 
                    IDC_SHRINKMARK, IDC_EXPANDEDMARK,
                    IDC_LESSTEXT) ; //<<<< add here

    ...
}

With the class wizard, add a handler for the "More" button (e.g. OnMore()) and modify it like this:

void CExpandDialogDlg::OnMore() 
{
    m_ExpandDialog.OnExpandButton() ;
}

The dialog template must include two special static controls. One that indicates the position of the bottom of the dialog when it is in the collapsed state (1 on the legend below) and one that contains the string that should appear on the "More/Less" button when the dialog is in the expanded state (2 on the legend below). These two controls will not be shown because the CExpandDialog class will hide them automatically.

Dialog template

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWant to shrink till Ok button Pin
Member 1072123818-Dec-14 1:43
Member 1072123818-Dec-14 1:43 
GeneralMy vote of 4 Pin
Adi Shavit13-Dec-11 23:50
Adi Shavit13-Dec-11 23:50 
GeneralVertical/horizontal (default) option Pin
varandas7927-Jul-09 23:46
varandas7927-Jul-09 23:46 
GeneralAnother method Pin
Steve Mayfield22-Sep-03 20:06
Steve Mayfield22-Sep-03 20:06 
GeneralRe: Another method Pin
objects2-May-06 5:05
objects2-May-06 5:05 
GeneralAny examples of using the expand feature with CPropertySheet Pin
SuperD1-Aug-03 10:55
SuperD1-Aug-03 10:55 
Generalnew usage Pin
john john mackey12-Feb-03 11:58
john john mackey12-Feb-03 11:58 
This is a great class and simple to understand/use.

I modified the Initialize() function and the method in which this expanding/collapsing dialog can be used.

By changing the 3rd argument to CExpandDialog::Initialize() to recognize a NULL value (or any invalid resource value), you can make the expand/collapse happen programmatically rather than using a button.

You just need to check for a button in Initialize() and in OnExpandButton()

If (IdExpandButton == NULL)
{
// calling expand/collapse without use of "More/Less" button
// don't have to SetWindowText() for the button
}

Don't forget to address your controls that are hidden when in Collapsed Mode. Disabling the hidden button should preserve your TAB sequence.

Cheers!
Johnny


Generalbeauty Pin
laue29-Jan-03 14:20
laue29-Jan-03 14:20 
GeneralClever Pin
Paul M Watt26-Mar-02 8:01
mentorPaul M Watt26-Mar-02 8:01 
GeneralRe: Clever Pin
Dimitris Vasiliadis15-Nov-02 8:48
Dimitris Vasiliadis15-Nov-02 8:48 
GeneralControls in expanded/collapsed area Pin
john john mackey7-Jan-02 10:46
john john mackey7-Jan-02 10:46 
GeneralRe: Controls in expanded/collapsed area Pin
Anonymous5-Jul-03 6:01
Anonymous5-Jul-03 6:01 
GeneralA more efficient way... Pin
LX25-Oct-00 21:43
LX25-Oct-00 21:43 
GeneralRe: A more efficient way... Pin
Dimitris Vasiliadis15-Nov-02 9:03
Dimitris Vasiliadis15-Nov-02 9:03 
GeneralRe: A more efficient way... Pin
Matban9-Jan-04 4:38
Matban9-Jan-04 4:38 
GeneralYou forgot to Pin
Daniel B.10-Apr-00 6:05
Daniel B.10-Apr-00 6:05 
GeneralRe: You forgot to Pin
Uwe Keim30-Dec-01 21:48
sitebuilderUwe Keim30-Dec-01 21:48 
GeneralRe: You forgot to Pin
Dimitris Vasiliadis15-Nov-02 9:10
Dimitris Vasiliadis15-Nov-02 9:10 
GeneralRe: You forgot to Pin
Matban9-Jan-04 4:48
Matban9-Jan-04 4:48 

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.