CTRLTEST

The CTRLTEST sample demonstrates several techniques for implementing and using custom controls.

Implementing and Using Custom Controls

You can implement a custom control from scratch by deriving from CWnd, but it is much easier if you can borrow functionality from one of the standard Windows controls by deriving from its control class in the library. CTRLTEST does this to implement a specialized edit control, CParsedEdit. This edit control accepts only specified sets of characters as user input -- numeric, alphabetic, and other non-control characters. CParsedEdit is derived from CEdit. It has an OnChar message handler to filter the characters.

The implementation of the commands in the Simple menu illustrates two methods for using a custom control. The methods are distinguished according to how the application associates instances of the control in the dialog box with the CParsedEdit class. Each Simple menu command displays a dialog box with four instances of the CParsedEdit control. Data entered in the dialog box is sent to the debug port as TRACE output. The two Simple menu commands are:

Test C++ Derived Class:

The CParsedEdit controls are data members of the dialog class. The controls are explicitly created in the dialog's OnInitDialog function by calling CParsedEdit::CreateSet. See DERTEST.CPP.

Test Dynamic Subclassing:

The controls are laid out in a dialog template resource (IDD_SUB_EDIT in CTRLTEST.RC) as standard edit controls. The controls are declared as CParsedEdit data members in the dialog class. The dialog's OnInitDialog function calls CParsedEdit::SubClassEdit, which in turn calls CWnd::SubclassDlgItem, to associate each specific instance of the edit control with the CParsedEdit class. See PAREDIT.CPP.

Example: Spin Control

The CTRLTEST sample includes an implementation of a spin control. The spin control has a small up-arrow and a small down-arrow button for incrementing or decrementing a value.

The Spin Control command calls a dialog box that has four CParsedEdit controls, each associated with a spin control. You can enter numeric data by either typing the value into the CParsedEdit control and selecting OK or by selecting the up or down arrow. Data entered in the CParsedEdit controls in this dialog box is filtered to accept only nonnegative integers. The CParsedEdit control treats all invalid values as 0. If you enter a negative or invalid number, an error message pops up when you select OK.

Example: Bitmap Button

The implementation of the following Custom menu commands illustrates ways to use CBitmapButton:

This sample demonstrates the following keywords:

CBitmapButton::AutoLoad CWnd::MessageBox
CBitmapButton::LoadBitmaps CWnd::OnChar
CBitmapButton::SizeToContent CWnd::SetFocus
CDialog::DoModal CWnd::SetWindowPos
CDialog::EndDialog CWnd::ShowWindow
CDialog::OnOK AfxGetInstanceHandle
CEdit::SetSel AfxMessageBox
CFrameWnd::AddAdornments AfxThrowResourceException
CString::Format CallWindowProc
CString::LoadString GetClassInfo
CWnd::Attach GetDlgItemInt
CWnd::Create IsCharAlpha
CWnd::EnableWindow IsCharAlphaNumeric
CWnd::GetDlgItem MessageBeep
CWnd::GetWindowRect SetDlgItemInt
CWnd::GetWindowText SetWindowLong
CWnd::IsWindowEnabled  

You can find the CTRLTEST project files in the Wce\Samples\Mfc\CTRLTEST directory.

For information on the Windows CE platforms that support this sample, see MFC for Windows CE Samples.