MFC Quick Start Guide (Part 2): Buttons
Create UI
- Click the Resource View tab
- Click the dialog. In this case it is IDD_MFC101_DIALOG
- In the Property panel, you can see the dialog has a ID which is IDD_MFC101_DIALOG. Remember that
- Click on the Toolbox tab on the left hand side of the window
- A list of selections should appear. Click on button and drag it to your dialog.
- Do this three times.
- Add a static text to the dialog. Inside the Property panel, change Caption to “Number:”
(Fig. 2.6: Property)
- Add another static text to the dialog, inside the Property panel:
- delete the text in the Caption field
- Change the IDD to IDC_TEXT_AREA
- Change Client Edge to True
Create Control Variable
- Right click the text box with ID IDC_TEXT_AREA
- Choose Add variable
- This window should appear
- Change Access to private
- Set Category to control
- Set variable name to be m_sNumberCount
(Fig. 2.10: Control) - Click Finish
Add Event handler
- Double click Button 1 (ID: IDC_BUTTON1)
- The content of MFC101Dlg.cpp should appear. And a new function called “void OnBnClickedButton1()” is added automatically.
- Inside MFC101Dlg.h, add a private member variable called m_nNumberCount
int m_nNumberCount;
- Back to MDC101Dlg.cpp, inside the function OnInitDialog(), initialize m_nNumberCount
m_nNumberCount = 0;
- Inside the function OnBnClickedButton1(), add the following code:
m_nNumberCount++; m_sNumberCount.Format(_T("%d"), m_nNumberCount); UpdateData(FALSE);
(Fig. 2.14) - Double click Button 2 (ID: IDC_BUTTON2)
- Inside the function OnBnClickedButton2(), add the following code:
m_nNumberCount = m_nNumberCount + 2; m_sNumberCount.Format(_T("%d"), m_nNumberCount); UpdateData(FALSE);
- Double click Button 3 (ID: IDC_BUTTON3)
- Inside the function OnBnClickedButton3(), add the following code:
m_nNumberCount = m_nNumberCount + 3; m_sNumberCount.Format(_T("%d"), m_nNumberCount); UpdateData(FALSE);
Build & Run
- Press Ctrl+shift+B to build the solution
- Press Ctrl+F5 to run (without debugging). The dialog should pop up.
- Try clicking different buttons and see the effect.
MFC Quick Start Guide (Part 2): Buttons
Reviewed by Kevin Lai
on
3:53:00 AM
Rating:
No comments: