Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 15: Fundamental Controls Visual C++ and MFC Fundamentals


Imagine a control with its identifier has already been added to a dialog box but you want
to change that identifier, for any reason you judge necessary. To do this, you would call
the CWnd::SetDlgCtrlID() method. Its syntax is:

int SetDlgCtrlID(int nID);

This method assigns or changes the identifier of the control that called it. Imagine the
control variable of a control is named m_Edition, you can assign a new identifier to it as
follows:

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

// TODO: Add extra initialization here

m_Edition.SetDlgCtrlID(0x1882);

return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}

If a control has been created and you need its identifier, you can find it out by calling the
CWnd::GetDlgCtrlID() method. Its syntax is:

int GetDlgCtrlID() const;

This method simply returns the identifier of the control that called it. The value is
returned as an integer. You will mostly use this method to find out if the identifier of a
control matches the one the user has just accessed.

Practical Learning: Using Controls Identifiers



  1. Start a new MFC Application named Identification and create it as Dialog Based
    without an About Box

  2. Delete the TODO line, the OK and the Cancel buttons

  3. From the Controls toolbox, add four Custom Controls and draw them
    horizontally on the dialog box as follows:

  4. Set the Class name of the top three custom controls to ScrollBar

Free download pdf