Click here to Skip to main content
15,881,600 members
Articles / Desktop Programming / MFC

Expanding and collapsing Dialogs

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

// Expanding and collapsing dialogs
//
// Michael Walz (walz@epsitec.ch)
// Dec. 99
//

#include "stdafx.h"
#include "DialogExpander.h"


void ShrinkDialog(CWnd *pDlg, int idmark)
{   
  ASSERT(pDlg != NULL) ;  
  CWnd *pWndMark = pDlg->GetDlgItem(idmark) ;
  ASSERT(pWndMark != NULL) ;
  CRect markrect ;
  CRect dlgrect ;
  CRect clientrect ;
  CWnd *pParentWnd = pDlg->GetParent() ;
  int offset ;

  pDlg->GetClientRect(&clientrect) ;  // clientrect of the dialog
  pDlg->GetWindowRect(&dlgrect) ;	  // rectangle of the dialog window

  // get height of the title bar
  offset = dlgrect.Height() - clientrect.bottom ;

  pWndMark->GetWindowRect(&markrect) ;
 

  pDlg->ScreenToClient(&markrect) ;

  // calculate the new rectangle of the dialog window
  dlgrect.bottom = dlgrect.top + markrect.bottom + offset;

  pDlg->MoveWindow (dlgrect.left, dlgrect.top, dlgrect.Width(), dlgrect.Height()) ;
}


CExpandDialog::CExpandDialog()
{
  m_bIsInitialized = FALSE ;
}


void CExpandDialog::Initialize(CWnd *pDialog, BOOL bInitiallyExpanded,
						  int IdExpandButton, int IdCollapsedMark,
						  int IdCollapsedText)
{
  m_IdExpandButton = IdExpandButton;
  m_IdCollapsedMark = IdCollapsedMark;
  m_IdCollapsedText = IdCollapsedText;
  m_bIsInitialized = TRUE ;
  m_pDialog = pDialog ;
  m_bIsExpanded = TRUE ;

  m_pDialog->GetWindowRect(&m_dialogrect) ;	  // rectangle of the dialog window
  m_pDialog->GetDlgItemText(IdExpandButton, m_sTextMore) ; 
  m_pDialog->GetDlgItemText(IdCollapsedText, m_sTextLess) ; 

  CWnd *pWndMark = m_pDialog->GetDlgItem(m_IdCollapsedText) ;
  pWndMark->ShowWindow(SW_HIDE);	// hide the "delimiting" control

  pWndMark = m_pDialog->GetDlgItem(m_IdCollapsedMark) ;
  pWndMark->ShowWindow(SW_HIDE);	// hide the "delimiting" control

  if (bInitiallyExpanded)
  {
	CWnd *pButton = m_pDialog->GetDlgItem(m_IdExpandButton) ;
	pButton->SetWindowText(m_sTextLess ) ;
  }
  else
	OnExpandButton() ;
}


void CExpandDialog::OnExpandButton()
{
  ASSERT(m_bIsInitialized) ;

  CWnd *pButton ;
  m_bIsExpanded = !m_bIsExpanded ;	

  if (m_bIsExpanded)
  {
	// ShrinkDialog(m_pDialog, m_IdExpandedMark) ;
	CRect rect ;
	m_pDialog->GetWindowRect(&rect) ;
	rect.bottom = rect.top + m_dialogrect.Height() ;
	rect.right = rect.left + m_dialogrect.Width() ;
	m_pDialog->MoveWindow (&rect) ;
  }
  else
	ShrinkDialog(m_pDialog, m_IdCollapsedMark ) ;

  
  pButton = m_pDialog->GetDlgItem(m_IdExpandButton) ;
  pButton->SetWindowText(!m_bIsExpanded ? m_sTextMore : m_sTextLess ) ;

}


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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