Click here to Skip to main content
15,896,538 members
Articles / Desktop Programming / MFC

Dynamic Items

Rate me:
Please Sign up or sign in to vote.
4.93/5 (15 votes)
24 Feb 20034 min read 200.8K   6.7K   85  
A class and an easy way to dynamically add items stored in a file to a menu
// OptionsDialog.cpp : implementation file
//

#include "stdafx.h"
#include "resource.h"
#include "OptionsDialog.h"

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

/////////////////////////////////////////////////////////////////////////////
// COptionsDialog dialog


COptionsDialog::COptionsDialog(CWnd* pParent /*=NULL*/)
	: CDialog(COptionsDialog::IDD, pParent)

{
	//{{AFX_DATA_INIT(COptionsDialog)
	m_strStaticMinutes = _T("");
	m_strStaticUrlDefaut = _T("");
	m_strStaticVerifier = _T("");
	m_strEditDureeVerif = _T("");
	m_strEditUrlDefaut = _T("");
	//}}AFX_DATA_INIT

	m_bDurationChanged = FALSE;
	m_bSelectedFileChanged = FALSE;
}

/////////////////////////////////////////////////////////////////////////////

void COptionsDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(COptionsDialog)
	DDX_Control(pDX, IDC_EDIT_URL_DEFAUT, m_editSelectFile);
	DDX_Control(pDX, IDC_BUTTON_SELECT_FILE, m_btnSelectFile);
	DDX_Control(pDX, IDC_EDIT_DUREE_VERIFICATION, m_editDureeVerif);
	DDX_Control(pDX, IDC_FRAME_NOTIFICATION, m_btnFrameNotification);
	DDX_Text(pDX, IDC_STATIC_MINUTES, m_strStaticMinutes);
	DDX_Text(pDX, IDC_STATIC_URL_DEFAUT, m_strStaticUrlDefaut);
	DDX_Text(pDX, IDC_STATIC_VERIFIER, m_strStaticVerifier);
	DDX_Text(pDX, IDC_EDIT_DUREE_VERIFICATION, m_strEditDureeVerif);
	DDX_Text(pDX, IDC_EDIT_URL_DEFAUT, m_strEditUrlDefaut);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(COptionsDialog, CDialog)
	//{{AFX_MSG_MAP(COptionsDialog)
	ON_EN_KILLFOCUS(IDC_EDIT_DUREE_VERIFICATION, OnKillfocusEditDureeVerification)
	ON_BN_CLICKED(IDC_BUTTON_SELECT_FILE, OnButtonSelectFile)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COptionsDialog message handlers

void COptionsDialog::SaveDataToRegistry()
{
	CString strKey;
	// Write in the registry only modified values

	UpdateData(TRUE);


	// Duration between each check
	if (atoi(m_strEditDureeVerif) != m_nDuration)
	{
		strKey.LoadString(IDS_REG_KEY_CHECK_TIME);
		m_Registry.WriteKeyToRegistry(strKey, atoi(m_strEditDureeVerif));
		m_bDurationChanged = TRUE;
		m_nDuration = atoi(m_strEditDureeVerif);
	}

	// File to check
	if (m_strEditUrlDefaut != m_strSelectedFile)
	{
		strKey.LoadString(IDS_REG_KEY_PATH);
		m_Registry.WriteKeyToRegistry(strKey, m_strEditUrlDefaut);
		m_bSelectedFileChanged = TRUE;
		m_strSelectedFile = m_strEditUrlDefaut;
	}

}

/////////////////////////////////////////////////////////////////////////////

BOOL COptionsDialog::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	CString strText;

	// Display static text
	strText.LoadString(IDS_OPTIONDLG_FRAME_TITRE);
	m_btnFrameNotification.SetWindowText(strText);
	m_strStaticMinutes.LoadString(IDS_OPTIONSDLG_MINUTES_TEXT);
	m_strStaticUrlDefaut.LoadString(IDS_OPTIONSDLG_URLDEFAUT_TEXT);
	m_strStaticVerifier.LoadString(IDS_OPTIONSDLG_VERIF_TEXT);
	strText.LoadString(IDS_OPTIONSDLG_BUTTON_SELECT_FILE);
	m_btnSelectFile.SetWindowText(strText);
	m_strEditUrlDefaut = m_strSelectedFile;
	UpdateData(FALSE);

	strText.Format("%d",m_nDuration);
	m_editDureeVerif.SetWindowText(strText);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

/////////////////////////////////////////////////////////////////////////////

void COptionsDialog::OnOK() 
{
	// Save datas in registry
	SaveDataToRegistry();

	CDialog::OnOK();
}

/////////////////////////////////////////////////////////////////////////////

void COptionsDialog::OnKillfocusEditDureeVerification() 
{
	//Check the duration is not null
	UpdateData(TRUE);
	
	if (atoi(m_strEditDureeVerif) == 0)
	{
		// Error! zero is not a valid duration
		CString strText;
		strText.LoadString(IDS_ERR_INVALID_DURATION);
		AfxMessageBox(strText);
		// Delete field
		m_editDureeVerif.SetWindowText("");
		///Give focus to  edit box
		m_editDureeVerif.SetFocus();
	}
}

/////////////////////////////////////////////////////////////////////////////

void COptionsDialog::OnButtonSelectFile() 
{
	// Select the file to read
	CFileDialog fileDlg(TRUE, "*.dat", "*.dat", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"Data files (*.dat)", NULL);
	if (fileDlg.DoModal() == IDOK)
	{
		//File selected
		TRACE0(fileDlg.m_ofn.lpstrFile);
		m_strEditUrlDefaut = fileDlg.m_ofn.lpstrFile;
		m_editSelectFile.SetWindowText(m_strEditUrlDefaut);
	}
}

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////

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.


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

Comments and Discussions