Click here to Skip to main content
15,894,460 members
Articles / Desktop Programming / MFC

Dynamically switchable multi-purpose control

Rate me:
Please Sign up or sign in to vote.
4.43/5 (9 votes)
5 Mar 2002CPOL10 min read 149.7K   2.2K   57  
Control which allows run-time switching between a number of control types, eg combo, edit etc
// MultiCtrlDemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MultiCtrlDemo.h"
#include "MultiCtrlDemoDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMultiCtrlDemoDlg dialog

CMultiCtrlDemoDlg::CMultiCtrlDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMultiCtrlDemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMultiCtrlDemoDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMultiCtrlDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMultiCtrlDemoDlg)
	DDX_Control(pDX, IDC_BTN_READONLY, m_btnReadOnly);
	DDX_Control(pDX, IDC_BTN_HIDDEN, m_btnHidden);
	DDX_Control(pDX, IDC_BTN_LABEL_AUTO, m_btnAutoFitLabel);
	DDX_Control(pDX, IDC_FLD, m_ctrl);
	DDX_Control(pDX, IDC_COMBO_TYPE, m_comboType);
	DDX_Control(pDX, IDC_BTN_NORMAL, m_btnNormal);
	DDX_Control(pDX, IDC_EDIT_VALUE, m_editValue);
	DDX_Control(pDX, IDC_EDIT_TOOLTIP, m_editTooltip);
	DDX_Control(pDX, IDC_EDIT_LABEL, m_editLabel);
	DDX_Control(pDX, IDC_BTN_NO_LABEL, m_btnNoLabel);
	DDX_Control(pDX, IDC_BTN_BROWSE_BTN, m_btnBrowseBtn);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMultiCtrlDemoDlg, CDialog)
	//{{AFX_MSG_MAP(CMultiCtrlDemoDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_CBN_SELENDOK(IDC_COMBO_TYPE, OnSelendokComboType)
	ON_BN_CLICKED(IDC_FLD, OnFld)
	ON_BN_CLICKED(IDC_BTN_SET_VALUE, OnBtnSetValue)
	ON_BN_CLICKED(IDC_BTN_SET_TOOLTIP, OnBtnSetTooltip)
	ON_BN_CLICKED(IDC_BTN_SET_LABEL, OnBtnSetLabel)
	ON_BN_CLICKED(IDC_BTN_READONLY, OnBtnReadonly)
	ON_BN_CLICKED(IDC_BTN_NORMAL, OnBtnNormal)
	ON_BN_CLICKED(IDC_BTN_NO_LABEL, OnBtnNoLabel)
	ON_BN_CLICKED(IDC_BTN_HIDDEN, OnBtnHidden)
	ON_BN_CLICKED(IDC_BTN_DISABLED, OnBtnDisabled)
	ON_BN_CLICKED(IDC_BTN_BROWSE_BTN, OnBtnBrowseBtn)
	ON_BN_CLICKED(IDC_BTN_LABEL_AUTO, OnBtnLabelAuto)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMultiCtrlDemoDlg message handlers

const char szLabel[] = "My label";
const char szValue[] = "My value text";
const char szTooltip[] = "This is a multi control";

#define AddComboItem(combo, text, data) \
		{ int n = combo.AddString(text); combo.SetItemData(n, data); }

// control type strings
#define CTS_NONE		"(None)"
#define CTS_STATIC		"Static"
#define CTS_STATICBTN		"Static Button"
#define CTS_EDIT		"Edit"
#define CTS_UPDOWNEDIT		"Up/down"
#define CTS_DROPLIST		"Droplist"
#define CTS_COMBO		"Combo"
#define CTS_CHECKBOX		"Checkbox"
#define CTS_DATETIME		"Date/Time"
#define CTS_RICHEDIT		"Rich Edit"

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

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	AddComboItem(m_comboType, CTS_NONE, MCT_NONE);
	AddComboItem(m_comboType, CTS_STATIC, MCT_STATIC);
	AddComboItem(m_comboType, CTS_STATICBTN, MCT_STATICBTN);
	AddComboItem(m_comboType, CTS_EDIT, MCT_EDIT);
	AddComboItem(m_comboType, CTS_UPDOWNEDIT, MCT_UPDOWNEDIT);
	AddComboItem(m_comboType, CTS_DROPLIST, MCT_DROPLIST);
	AddComboItem(m_comboType, CTS_COMBO, MCT_COMBO);
	AddComboItem(m_comboType, CTS_CHECKBOX, MCT_CHECKBOX);
	AddComboItem(m_comboType, CTS_DATETIME, MCT_DATETIME);
	AddComboItem(m_comboType, CTS_RICHEDIT, MCT_RICHEDIT);
	m_comboType.SetCurSel(m_comboType.FindStringExact(-1, CTS_EDIT));
	m_btnNormal.SetCheck(TRUE);

	m_ctrl.SetType(MCT_EDIT);
	m_ctrl.SetLabelText(szLabel);
	m_btnAutoFitLabel.SetCheck(TRUE);
	m_ctrl.SetLabelWidth(MCSLW_AUTO);
	m_editLabel.SetWindowText(szLabel);
	m_ctrl.SetValueText(szValue);
	m_editValue.SetWindowText(szValue);
	m_ctrl.SetHelpString(szTooltip);
	m_editTooltip.SetWindowText(szTooltip);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMultiCtrlDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMultiCtrlDemoDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMultiCtrlDemoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMultiCtrlDemoDlg::OnSelendokComboType() 
{
  // change the control type
  int n = m_comboType.GetCurSel();
  if (n == -1)
    return;
  m_ctrl.SetType((MULTICTRLTYPE)m_comboType.GetItemData(n));
}

void CMultiCtrlDemoDlg::OnFld() 
{
  AfxMessageBox("You clicked on the control");
}

void CMultiCtrlDemoDlg::OnBtnSetValue() 
{
  CString sValue;
  m_editValue.GetWindowText(sValue);
  m_ctrl.SetValueText(sValue);
}

void CMultiCtrlDemoDlg::OnBtnSetTooltip() 
{
  CString sTooltip;
  m_editTooltip.GetWindowText(sTooltip);
  m_ctrl.SetHelpString(sTooltip);
}

void CMultiCtrlDemoDlg::OnBtnSetLabel() 
{
  CString sLabel;
  m_editLabel.GetWindowText(sLabel);
  m_ctrl.SetLabelText(sLabel);
  if (m_btnAutoFitLabel.GetCheck())
    m_ctrl.SetLabelWidth(MCSLW_AUTO);
}

void CMultiCtrlDemoDlg::OnBtnReadonly() 
{
  int nAttrib = m_ctrl.GetAttributes();
  m_ctrl.SetAttributes((nAttrib | MCA_READONLY) & ~MCA_DISABLED);
}

void CMultiCtrlDemoDlg::OnBtnNormal() 
{
  int nAttrib = m_ctrl.GetAttributes();
  m_ctrl.SetAttributes(nAttrib & ~(MCA_READONLY | MCA_DISABLED));
}

void CMultiCtrlDemoDlg::OnBtnNoLabel() 
{
  int nAttrib = m_ctrl.GetAttributes();
  if (m_btnNoLabel.GetCheck())
    m_ctrl.SetAttributes(nAttrib | MCA_NOLABEL);
  else
    m_ctrl.SetAttributes(nAttrib & ~MCA_NOLABEL);
}

void CMultiCtrlDemoDlg::OnBtnHidden() 
{
  int nAttrib = m_ctrl.GetAttributes();
  if (m_btnHidden.GetCheck())
    m_ctrl.SetAttributes(nAttrib | MCA_HIDDEN);
  else
    m_ctrl.SetAttributes(nAttrib & ~MCA_HIDDEN);
}

void CMultiCtrlDemoDlg::OnBtnDisabled() 
{
  int nAttrib = m_ctrl.GetAttributes();
  m_ctrl.SetAttributes((nAttrib | MCA_DISABLED) & ~MCA_READONLY);
}

void CMultiCtrlDemoDlg::OnBtnBrowseBtn() 
{
  int nAttrib = m_ctrl.GetAttributes();
  if (m_btnBrowseBtn.GetCheck())
    m_ctrl.SetAttributes(nAttrib | MCA_BROWSEBTN);
  else
    m_ctrl.SetAttributes(nAttrib & ~MCA_BROWSEBTN);
}

void CMultiCtrlDemoDlg::OnBtnLabelAuto() 
{
  if (m_btnAutoFitLabel.GetCheck())
    m_ctrl.SetLabelWidth(MCSLW_AUTO);
  else
    m_ctrl.SetLabelWidth();
}

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
Software Developer (Senior)
United Kingdom United Kingdom
Originally from an electronics background, I moved into software in 1996, partly as a result of being made redundant, and partly because I was very much enjoying the small amount of coding (in-at-the-deep-end-C) that I had been doing!

I swiftly moved from C to C++, and learned MFC, and then went on to real-time C on Unix. After this I moved to the company for which I currently work, which specialises in Configuration Management software, and currently program mainly in C/C++, for Windows. I have been gradually moving their legacy C code over to use C++ (with STL, MFC, ATL, and WTL). I have pulled in other technologies (Java, C#, VB, COM, SOAP) where appropriate, especially when integrating with third-party products.

In addition to that, I have overseen the technical side of the company website (ASP, VBScript, JavaScript, HTML, CSS), and have also worked closely with colleagues working on other products (Web-based, C#, ASP.NET, SQL, etc).

For developing, I mainly use Visual Studio 2010, along with an in-house-designed editor based on Andrei Stcherbatchenko's syntax parsing classes, and various (mostly freeware) tools. For website design, I use Dreaweaver CS3.

When not developing software, I enjoy listening to and playing music, playing electric and acoustic guitars and mandolin.

Comments and Discussions