MFC - Managing the Updown Control



Step 1 − Add a control variable m_spinControl for spin control with the settings as shown in the following snapshot.

Managing Updown Control

Step 2 − Add the control variable m_editControl for Edit control.

m_editControl

Step 3 − Add the event handler for UDN_DELTAPOS event for the spin button.

Managing Updown Control

Step 4 − Update the OnInitDialog() as shown in the following code.

BOOL CMFCSpinButtonDlg::OnInitDialog() {
   CDialogEx::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
   // TODO: Add extra initialization here
   m_spinControl.SetRange(0, 100);
   m_spinControl.SetPos(50);
   m_editControl.SetWindowText(L"50");
   return TRUE; // return TRUE unless you set the focus to a control
}

Step 5 − Here is the implementation of spin control event.

void CMFCSpinButtonDlg::OnDeltaposSpin1(NMHDR *pNMHDR, LRESULT *pResult) {
   LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
   // TODO: Add your control notification handler code here
   // Declare a pointer to a CSpinButtonCtrl;
   CSpinButtonCtrl *Spinner;
   // Get a pointer to our spin button
   Spinner = reinterpret_cast<CSpinButtonCtrl *>(GetDlgItem(IDC_SPIN1));
   // Found out if it is our spin button that sent the message
   // This conditional statement appears useless but so what?
   if (pNMHDR -> hwndFrom == Spinner -> m_hWnd) {
      // Get the current value of the spin button
      int CurPos = pNMUpDown→iPos;
      // Convert the value to a string

      CString str;
      str.Format(L"%d", CurPos);
      // Display the value into the accompanying edit box
      m_editControl.SetWindowText(str);
   }
   *pResult = 0;
}

Step 6 − When the above code is compiled and executed, you will see the following output.

Managing Updown Control
mfc_windows_controls.htm
Advertisements