Visual C++ and MFC Fundamentals Chapter 15: Fundamental Controls
is its Caption. By default, after adding a button to a form, the Caption property has
focus. Therefore, if you start typing, the caption would be changed. At design time, you
can set the caption with the necessary string on the Caption field of the Properties
window. At run time, to change the caption of a button, call the
CWnd::SetWindowText() method and pass it the necessary string as argument. Here is
an example:
BOOL CDialog5Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_Submit.SetWindowText("Submit");
return TRUE; // return TRUE unless you set the focus to a control
}
If you want to access the properties of a control without using an associated variable, you
may have to call the CWnd::GetDlgItem() method. It comes in two versions as follows:
CWnd* GetDlgItem(int nID) const;
void CWnd::GetDlgItem(int nID, HWND* phWnd) const;
When calling this method, the first version allows you to assign a CWnd (or derived
class) to it. The second returns a handle to the window passed as pointer. In both cases,
you must pass the identifier of the control that you want access to. When using the first
version, if the control is not a CWnd object, you must cast it its native class. Then you
can manipulate the property (or properties) of your choice. Here is an example that
accesses a button and changes its caption:
BOOL CDialog5Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_Submit.SetWindowText("Submit");
CButton *btnWhatElse = reinterpret_cast<CButton
*>(GetDlgItem(IDC_BUTTON3));
btnWhatElse->SetWindowText("What Else?");
return TRUE; // return TRUE unless you set the focus to a control
}
The second version requires a pointer to a child window that you want to access.
The most popular button captions are OK and Cancel.
The OK caption is set for a dialog box that informs the user of an error, an intermediary
situation, or an acknowledgement of an action that was performed on the dialog that hosts
the button. Visual C++ makes it easy to add an OK button because in Windows