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

CTreeOptionsCtrl v1.21

, 3 Mar 2000
A freeware MFC class to provide a tree options control.
#include "stdafx.h"
#include "app.h"
#include "appDlg.h"
#include "DowComboBox.h"
#include "DemoEditBox.h"

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

CAppDlg::CAppDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAppDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAppDlg)
	//}}AFX_DATA_INIT

  // Change these values to change the state of items initially checked
  m_bAlt1 = TRUE;
  m_bAlt2 = FALSE;
  m_nSearchIndex = 0;
  m_sCombo = _T("Monday");
  m_sEdit = _T("Your Name");

  // Other initialisation
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  m_hAccessibility = NULL;
  m_hAlt1 = NULL;
  m_hAlt2 = NULL;
  m_hSearch = NULL;
  m_hRadio1 = NULL;
  m_hRadio2 = NULL;
  m_hRadio3 = NULL;
  m_hRadio4 = NULL;
  m_hComboItem = NULL;
  m_hEditItem = NULL;
}

void CAppDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAppDlg)
	DDX_Control(pDX, IDC_TREE1, m_ctrlTreeOptions);
	//}}AFX_DATA_MAP

  if (!pDX->m_bSaveAndValidate)
  {
    //Remove any items which may be in the control already
    m_ctrlTreeOptions.DeleteAllItems();

    //Add some combo items to the control
    m_hAccessibility = m_ctrlTreeOptions.InsertGroup(_T("Accessibility (Example of check options)"), 8, NULL);
    m_hAlt1 = m_ctrlTreeOptions.InsertCheckBox(_T("Always expand ALT text for images"), m_hAccessibility, m_bAlt1);
    m_hAlt2 = m_ctrlTreeOptions.InsertCheckBox(_T("Move system caret with focus/selection changes"), m_hAccessibility, m_bAlt2);

    //Add some radio items to the control
    m_hSearch = m_ctrlTreeOptions.InsertGroup(_T("When Searching (Example of radio options)"), 9, NULL);
    m_hRadio1 = m_ctrlTreeOptions.InsertRadioButton(_T("Display results, and go to the most likely site"), m_hSearch, m_nSearchIndex == 0);
    m_hRadio2 = m_ctrlTreeOptions.InsertRadioButton(_T("Do not search from the Address bar"), m_hSearch, m_nSearchIndex == 1);
    m_hRadio3 = m_ctrlTreeOptions.InsertRadioButton(_T("Just display the results in the main window"), m_hSearch, m_nSearchIndex == 2);
    m_hRadio4 = m_ctrlTreeOptions.InsertRadioButton(_T("Just go to the most likely site"), m_hSearch, m_nSearchIndex == 3);

    //Add an edit box to the control
    m_hEditItem = m_ctrlTreeOptions.InsertItem(_T("Your First Name (Example of edit box)"), 10, 10);
    m_ctrlTreeOptions.AddEditBox(m_hEditItem, RUNTIME_CLASS(CDemoEdit));

    //Add a combo box item to the control
    m_hComboItem = m_ctrlTreeOptions.InsertItem(_T("Day of the Week (Example of combo box)"), 11, 11);
    m_ctrlTreeOptions.AddComboBox(m_hComboItem, RUNTIME_CLASS(CDowComboBox));

    //Initially have all the items expanded
    m_ctrlTreeOptions.Expand(m_hAccessibility, TVE_EXPAND);
    m_ctrlTreeOptions.Expand(m_hSearch, TVE_EXPAND);
  }

  //Try out the DDX functions
  DDX_TreeCheck(pDX, IDC_TREE1, m_hAlt1, m_bAlt1);
  DDX_TreeCheck(pDX, IDC_TREE1, m_hAlt2, m_bAlt2);
  DDX_TreeRadio(pDX, IDC_TREE1, m_hSearch, m_nSearchIndex);
  DDX_TreeEdit (pDX, IDC_TREE1, m_hEditItem, m_sEdit);
  DDX_TreeCombo(pDX, IDC_TREE1, m_hComboItem, m_sCombo);
}

BEGIN_MESSAGE_MAP(CAppDlg, CDialog)
	//{{AFX_MSG_MAP(CAppDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_DISABLE_CHECK, OnDisableCheck)
	ON_BN_CLICKED(IDC_DISABLE_GROUP, OnDisableGroup)
	ON_BN_CLICKED(IDC_DISABLE_RADIO, OnDisableRadio)
	ON_BN_CLICKED(IDC_ENABLE_CHECK, OnEnableCheck)
	ON_BN_CLICKED(IDC_ENABLE_GROUP, OnEnableGroup)
	ON_BN_CLICKED(IDC_ENABLE_RADIO, OnEnableRadio)
	ON_BN_CLICKED(IDC_DISABLE_GROUP2, OnDisableGroup2)
	ON_BN_CLICKED(IDC_ENABLE_GROUP2, OnEnableGroup2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BOOL CAppDlg::OnInitDialog()
{
  //Let the parent do its thing
	CDialog::OnInitDialog();

  //Setup the dialog icons
	SetIcon(m_hIcon, TRUE);
	SetIcon(m_hIcon, FALSE);

	return TRUE;
}

void CAppDlg::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();
	}
}

HCURSOR CAppDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


void CAppDlg::OnEnableGroup() 
{
	m_ctrlTreeOptions.SetGroupEnable(m_hSearch, TRUE);
}

void CAppDlg::OnDisableGroup() 
{
	m_ctrlTreeOptions.SetGroupEnable(m_hSearch, FALSE);
}

void CAppDlg::OnEnableGroup2() 
{
  m_ctrlTreeOptions.SetGroupEnable(m_hAccessibility, TRUE);  
}

void CAppDlg::OnDisableGroup2() 
{
  m_ctrlTreeOptions.SetGroupEnable(m_hAccessibility, FALSE);
}

void CAppDlg::OnEnableCheck() 
{
  m_ctrlTreeOptions.SetCheckBoxEnable(m_hAlt1, TRUE);	
}

void CAppDlg::OnDisableCheck() 
{
  m_ctrlTreeOptions.SetCheckBoxEnable(m_hAlt1, FALSE);
}

void CAppDlg::OnEnableRadio() 
{
	m_ctrlTreeOptions.SetRadioButtonEnable(m_hRadio1, TRUE);
}

void CAppDlg::OnDisableRadio() 
{
	m_ctrlTreeOptions.SetRadioButtonEnable(m_hRadio1, FALSE);
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

PJ Naughter

United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 4 Mar 2000
Article Copyright 2000 by PJ Naughter
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid