Click here to Skip to main content
15,885,435 members
Articles / Desktop Programming / MFC

Improved CEdit control

Rate me:
Please Sign up or sign in to vote.
4.86/5 (30 votes)
7 Dec 2014Zlib7 min read 60K   4.2K   63  
CEdit derived control with additional editing options and multilevel undo/redo.
// EditExAppDlg.cpp : implementation file
//

#include "stdafx.h"
#include "EditExApp.h"
#include "EditExAppDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CEditExAppDlg dialog


CEditExAppDlg::CEditExAppDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CEditExAppDlg::IDD, pParent)
	, m_strEditExSingleLine(_T("Single-line"))
	, m_strEditExMultiLine(_T("Multi-line"))
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CEditExAppDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDITEX_SINGLELINE, m_ctlEditExSingleLine);
	DDX_Text(pDX, IDC_EDITEX_SINGLELINE, m_strEditExSingleLine);
	DDX_Control(pDX, IDC_EDITEX_MULTILINE, m_ctlEditExMultiline);
	DDX_Text(pDX, IDC_EDITEX_MULTILINE, m_strEditExMultiLine);
}

BEGIN_MESSAGE_MAP(CEditExAppDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_INSERT_SINGLELINE, &CEditExAppDlg::OnBnClickedSingleLine)
	ON_BN_CLICKED(IDC_BUTTON_INSERT_MULTILINE, &CEditExAppDlg::OnBnClickedMultiLine)
END_MESSAGE_MAP()


// CEditExAppDlg message handlers

BOOL CEditExAppDlg::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)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		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

	// TODO: Add extra initialization here

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

// 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 CEditExAppDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CEditExAppDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CEditExAppDlg::OnBnClickedSingleLine()
{
	UpdateData(TRUE);
	m_strEditExSingleLine = _T("Text");
	UpdateData(FALSE);
}

void CEditExAppDlg::OnBnClickedMultiLine()
{
	UpdateData(TRUE);
	m_strEditExMultiLine = _T("Text");
	UpdateData(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, along with any associated source code and files, is licensed under The zlib/libpng License


Written By
Software Developer (Senior)
Croatia Croatia
Graduated at the Faculty of Electrical Engineering and Computing, University of Zagreb (Croatia) and received M.Sc. degree in electronics. For several years he was research and lecturing assistant in the fields of solid state electronics and electronic circuits, published several scientific and professional papers, as well as a book "Physics of Semiconductor Devices - Solved Problems with Theory" (in Croatian).
During that work he gained interest in C++ programming language and have co-written "C++ Demystified" (in Croatian), 1st edition published in 1997, 2nd in 2001, 3rd in 2010, 4th in 2014.
After book publication, completely switched to software development, programming mostly in C++ and in C#.
In 2016 coauthored the book "Python for Curious" (in Croatian).

Comments and Discussions