MFC Quick Start Guide (Part 3): Edit Control and Spin Control
- Click Edit Control on the Toolbox panel and drag it to your dialog
- 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)
- Change the Number field in the Property panel to True
- 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
- Repeat step on the other 2 edit boxes.
Spin Control
- Click Spin Control on the Toolbox panel and drag it to somewhere beside your button
- Repeat for the other 2 buttons
- Change the IDs of the buttons to IDC_RED_SPIN, IDC_GREEN_SPIN and IDC_BLUE_SPIN
- In the Property panel, change Set Buddy Integer to True
- 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);
- 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);
Build & Run
- Press Ctrl+shift+B to build the solution
- Press Ctrl+F5 to run (without debugging).
- Try clicking the spin buttons and what happen.
MFC Quick Start Guide (Part 3): Edit Control and Spin Control
Reviewed by Kevin Lai
on
4:42:00 AM
Rating:
The MFC Quick Start Guide (Part 3) covers essential components like Edit Control and Spin Control, crucial for developing user-friendly applications. Pair your development efforts with tsoHost for reliable hosting that supports seamless application performance.
ReplyDelete