Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

CResizableDialog

, 25 Jul 2012
A CDialog derived class to implement resizable dialogs with MFC
// SecondDialog.cpp : implementation file
//

#include "stdafx.h"
#include "demo.h"
#include "SecondDialog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSecondDialog dialog


CSecondDialog::CSecondDialog(CWnd* pParent /*=NULL*/)
	: CResizableDialog(CSecondDialog::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSecondDialog)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CSecondDialog::DoDataExchange(CDataExchange* pDX)
{
	CResizableDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSecondDialog)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSecondDialog, CResizableDialog)
	//{{AFX_MSG_MAP(CSecondDialog)
	ON_WM_CREATE()
	ON_COMMAND(ID_BUTTON1, OnButton1)
	ON_COMMAND(ID_BUTTON2, OnButton2)
	ON_COMMAND(ID_BUTTON3, OnButton3)
	ON_COMMAND(ID_BUTTON4, OnButton4)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSecondDialog message handlers

BOOL CSecondDialog::OnInitDialog() 
{
	CResizableDialog::OnInitDialog();

	// move toolbar to its position
	// before adding it to the layout
	CSize size;
	CRect rc1, rc2;
	GetDlgItem(IDOK)->GetWindowRect(&rc1);
	GetDlgItem(IDC_LINE1)->GetWindowRect(&rc2);
	rc1.left = rc2.left;
	ScreenToClient(&rc1);

	m_tbToolbar.GetToolBarCtrl().GetMaxSize(&size);
	m_tbToolbar.MoveWindow(rc1.left,rc1.top,size.cx,size.cy,FALSE);

	// set layout
	AddAnchor(IDC_PICTURE1, TOP_LEFT, CSize(30,100));
	AddAnchor(IDC_STATIC1, CSize(30,0), CSize(80,100));
	AddAnchor(IDC_STATIC2, CSize(80,0), BOTTOM_RIGHT);

	AddAnchor(IDC_LINE1, BOTTOM_LEFT, BOTTOM_RIGHT);
	AddAnchor(IDOK, BOTTOM_RIGHT);
	AddAnchor(IDCANCEL, BOTTOM_RIGHT);

	// these should be equivalent, as long as you leave the
	// default control ID for the toolbar in the Create() call
	// AddAnchor(m_tbToolbar.GetSafeHwnd(), BOTTOM_LEFT);
	AddAnchor(AFX_IDW_TOOLBAR, BOTTOM_LEFT);

	ResetMinTrackSize();

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

int CSecondDialog::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CResizableDialog::OnCreate(lpCreateStruct) == -1)
		return -1;

	// create toolbar, with just buttons and no border
	m_tbToolbar.Create(this);
	m_tbToolbar.LoadToolBar(IDR_TOOLBAR1);
	m_tbToolbar.SetBorders(0,0,0,0);
	m_tbToolbar.SetBarStyle(m_tbToolbar.GetBarStyle() & ~CBRS_BORDER_TOP);

	// tooltips don't work! (see note below)

	return 0;
}

void CSecondDialog::OnButton1() 
{
	// set background color
	((CDemoApp*)AfxGetApp())->SetDialogBkColor(RGB(250,200,200));
	Invalidate();

	// NOTE: for details on how you could put a toolbar in a dialog
	// box and use ON_UPDATE_COMMAND_UI, see KB article Q141751
	// "SAMPLE: Adding Control Bars to Dialog Boxes in MFC"
	// with the sample project DLGCBR32 and some hints
}

void CSecondDialog::OnButton2() 
{
	// set background color
	((CDemoApp*)AfxGetApp())->SetDialogBkColor(RGB(200,200,250));
	Invalidate();
}

void CSecondDialog::OnButton3() 
{
	// set background color
	((CDemoApp*)AfxGetApp())->SetDialogBkColor(RGB(190,250,190));
	Invalidate();
}

void CSecondDialog::OnButton4() 
{
	// set background color
	((CDemoApp*)AfxGetApp())->SetDialogBkColor(RGB(250,250,180));
	Invalidate();
}

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)

Share

About the Author

Paolo Messina
Software Developer RoboTech srl
Italy Italy
Paolo began programming at the age of 9 with a glorious 8086 and GW-BASIC, then he played a bit with C, ASM and Pascal. He tought himself MFC and Windows programming, to exploit his studies of C++. Always attracted by low-level programming and Assembly, he's beginning to appreciate the joys of templates and STL. At work he changed his mind about Java, discovered Eclipse IDE, and now think it's cool.
 
He lives in Follonica, Italy.
 
He has been abroad in the U.S. to work on his final thesis before graduating. For seven months he was playing with airplanes and automatic control at the Unversity of Illinois at Urbana-Champaign.
 
He graduated in Computer Science Engineering at the University of Pisa, Italy, in December 2003.
 
Currently working for an edutainment robotics company (www.robotechsrl.com).
Follow on   Google+

| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 25 Jul 2012
Article Copyright 2000 by Paolo Messina
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid