MFC - Gestion du contrôle de mise à jour

Step 1 - Ajouter une variable de contrôle m_spinControl pour le contrôle de rotation avec les paramètres comme indiqué dans l'instantané suivant.

Step 2 - Ajoutez la variable de contrôle m_editControl pour le contrôle d'édition.

Step 3 - Ajoutez le gestionnaire d'événements pour l'événement UDN_DELTAPOS pour le bouton de rotation.

Step 4 - Mettez à jour OnInitDialog () comme indiqué dans le code suivant.

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 - Voici l'implémentation de l'événement de contrôle de rotation.

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 - Lorsque le code ci-dessus est compilé et exécuté, vous verrez la sortie suivante.