Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Visual C++ and MFC Fundamentals Chapter 20: List-Based Controls



  1. Execute the application to test the radio buttons

  2. Close the dialog box and return to MSVC


19.1.4..Radio Buttons Methods......................................................................


As a Windows control, the radio button is based on the CButton class which, like all
other constrols, is based on CWnd. As far as the Win32 and the MFC libraries are
concerned, a radio button is first of all, a button. Therefore, to programmatically create a
radio button, declare a variable or a pointer to CButton using its default constructor. Like
all other MFC controls, the CButton class provides a Create() method to initialize it. The
syntax of this method for a button is:

BOOL Create(LPCTSTR lpszCaption, DWORD dwStyle, const RECT& rect, CWnd*
pParentWnd, UINT nID);

The lpszCaption argument is the string of the label that accomponies the radio button.
The differences of radio buttons are set using the dwStyle argument. A radio button must
have the BS_RADIOBUTTON value.

When setting the rect argument, provide enough space for the control because, by default,
it will be confined to that rectangle and cannot display beyond that. Here are two
examples:

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

// 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
Free download pdf