MFC Quick Start Guide (Part 3): Edit Control and Spin Control

  1. Click Edit Control on the Toolbox panel and drag it to your dialog


    (Fig. 3.1: Edit Control)
  2. Do it three times, and rename the IDs of them to IDC_RED_EDIT, IDC_GREEN_EDIT and IDC_BLUE_EDIT (from top to bottom)


    (Fig. 3.2: Dialog)

  3. Change the Number field in the Property panel to True
  4. Now we are limiting the value bound of the 3 edit boxes to a range from 0 to 255.
    • Double click the first edit box, a function called OnEnChangeRedEdit() would be automatically added to MFC101Dlg.cpp
    • Add the private member variable m_nRedVal to MFC101Dlg.h

      int m_nRedVal;
      


      (Fig. 3.3: private member variable)
    • Back to MFC101Dlg.cpp, initialize the variable inside the function OnInitDialog()

      m_nRedVal = 0;
      
    • Add the following code to the function

      char acStr[5] = "";
      CEdit* pRedBox = (CEdit *)GetDlgItem(IDC_RED_EDIT);
      pRedBox->GetWindowText(LPTSTR(acStr), 256);
      if (!(atoi((const char *)acStr)))
          pRedBox->SetWindowText(_T(""));
      else
      {
          if ((atoi((const char *)acStr)) > 255)
          {
              pRedBox->SetWindowText(_T("255"));
          }
          else if ((atoi((const char *)acStr)) < 0)
          {
              pRedBox->SetWindowText(_T("0"));
          }
          m_nRedVal = atoi((const char *)acStr);
      }
      

      (Fig. 3.4: Code)
    • This segment of code may not work due to encoding. If it happens, Alt+Enter and change the Encoding accordingly


      (Fig. 3.5: Property Page)
  5. Repeat step on the other 2 edit boxes.


Spin Control

  1. Click Spin Control on the Toolbox panel and drag it to somewhere beside your button 
  2. Repeat for the other 2 buttons


    (Fig. 3.6: Spin Control)
  3. Change the IDs of the buttons to IDC_RED_SPIN, IDC_GREEN_SPIN and IDC_BLUE_SPIN
  4. In the Property panel, change Set Buddy Integer to True
  5. Inside the OnInitDialog() function, add the following code:

    CSpinButtonCtrl* pSpinColorCtrl = (CSpinButtonCtrl*)GetDlgItem(IDC_RED_SPIN);
    pSpinColorCtrl->SetRange(0, 255);
    pSpinColorCtrl->SetBuddy((CEdit *)GetDlgItem(IDC_RED_EDIT));
    pSpinColorCtrl->SetPos(m_nRedVal);
    
    UDACCEL AccellValue1;
    AccellValue1.nInc = 1;   // Set how many the value would increase/decrease when a button is clicked
    pSpinColorCtrl->SetAccel(1, &AccellValue1);
    
  6. For the other 2 buttons, add:

    pSpinColorCtrl = (CSpinButtonCtrl*)GetDlgItem(IDC_GREEN_SPIN);
    pSpinColorCtrl->SetRange(0, 255);
    pSpinColorCtrl->SetBuddy((CEdit *)GetDlgItem(IDC_GREEN_EDIT));
    pSpinColorCtrl->SetPos(m_nGreenVal);
    
    AccellValue.nInc = 1;
    pSpinColorCtrl->SetAccel(1, &AccellValue);
    
    pSpinColorCtrl = (CSpinButtonCtrl*)GetDlgItem(IDC_BLUE_SPIN);
    pSpinColorCtrl->SetRange(0, 255);
    pSpinColorCtrl->SetBuddy((CEdit *)GetDlgItem(IDC_BLUE_EDIT));
    pSpinColorCtrl->SetPos(m_nBlueVal);
    
    AccellValue.nInc = 1;
    pSpinColorCtrl->SetAccel(1, &AccellValue);
    
    


    (Fig.3.7: Code )


Build & Run

  1. Press Ctrl+shift+B to build the solution 
  2. Press Ctrl+F5 to run (without debugging).
  3. Try clicking the spin buttons and what happen.

MFC Quick Start Guide (Part 3): Edit Control and Spin Control MFC Quick Start Guide (Part 3): Edit Control and Spin Control Reviewed by Kevin Lai on 4:42:00 AM Rating: 5

No comments:

Powered by Blogger.