MFC Quick Start Guide (Part 2): Buttons

Create UI

  1. Click the Resource View tab
  2. Click the dialog. In this case it is IDD_MFC101_DIALOG

    (Fig. 2.1: Resource view)

  3. In the Property panel, you can see the dialog has a ID which is IDD_MFC101_DIALOG. Remember that
  4. Click on the Toolbox tab on the left hand side of the window

    (Fig 2.2: Toolbox)
  5. A list of selections should appear. Click on button and drag it to your dialog.

    (Fig 2.3 Button)


    (Fig 2.4 Dialog with a button)
  6. Do this three times.

    (Fig 2.5: 3 buttons)
  7. Add a static text to the dialog. Inside the Property panel, change Caption to “Number:”
     (Fig. 2.6: Property)

  8. 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

      (Fig. 2.7: ID)


      (Fig. 2.8: Client Edge)

Create Control Variable

  1. Right click the text box with ID IDC_TEXT_AREA
  2. Choose Add variable

    (Fig. 2.9: Add variables)

  3. 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

  1. Double click Button 1 (ID: IDC_BUTTON1)
  2. The content of MFC101Dlg.cpp should appear. And a new function called “void OnBnClickedButton1()” is added automatically.

    (Fig. 2.11)

  3. Inside MFC101Dlg.h, add a private member variable called m_nNumberCount
    int m_nNumberCount;
    

    (Fig. 2.12)

  4. Back to MDC101Dlg.cpp, inside the function OnInitDialog(), initialize m_nNumberCount
    m_nNumberCount = 0;
    


    (Fig. 2.13)

  5. Inside the function OnBnClickedButton1(), add the following code:
    m_nNumberCount++;
    m_sNumberCount.Format(_T("%d"), m_nNumberCount);
    UpdateData(FALSE);
    

    (Fig. 2.14)

  6. Double click Button 2 (ID: IDC_BUTTON2)
  7. Inside the function OnBnClickedButton2(), add the following code:
    m_nNumberCount = m_nNumberCount + 2;
    m_sNumberCount.Format(_T("%d"), m_nNumberCount);
    UpdateData(FALSE);
    

  8. Double click Button 3 (ID: IDC_BUTTON3)
  9. Inside the function OnBnClickedButton3(), add the following code:
    m_nNumberCount = m_nNumberCount + 3;
    m_sNumberCount.Format(_T("%d"), m_nNumberCount);
    UpdateData(FALSE);
    

Build & Run

  1. Press Ctrl+shift+B to build the solution
  2. Press Ctrl+F5 to run (without debugging). The dialog should pop up.

    (Fig. 2.15)

  3. Try clicking different buttons and see the effect.
MFC Quick Start Guide (Part 2): Buttons MFC Quick Start Guide (Part 2): Buttons Reviewed by Kevin Lai on 3:53:00 AM Rating: 5

No comments:

Powered by Blogger.