Implementing AutoClik’s Basic Behavior

The rest of Step 1 implements AutoClik’s basic behavior, which consists of displaying text at mouse clicks and accepting changed text by using a dialog box.

To complete this topic, you need to create a dialog box and add a menu item. For tutorial information, see Scribble Lesson 7, Adding a Dialog Box, and Add the Clear All Command to Scribble's Edit Menu in Scribble Lesson 5.

To add the member variables to AutoClik’s document class

  1. Declare the following member variables in the public Attributes section of AutoClickDoc.h:
    CPoint m_pt;
    CString m_str;
    
  2. Use ClassView to jump to the CAutoClickDoc constructor and add the following lines (after the EnableAutomation call):
    m_pt = CPoint(10,10);
    m_str = _T("Automation!");
    
  3. Serialize the member variables in the document class. Use ClassView to jump to the Serialize member function and implement it with the following code.
    ar << m_pt << m_str;
    
    ar >> m_pt >> m_str;
    

To implement AutoClik’s drawing code

The implementations of OnLButtonDown and OnEditChangeText make use of the helper function, Refresh.

To implement the Refresh helper function

  1. In ClassView, right-click the class CAutoClickDoc.

  2. On the pop-up menu that appears, click Add Member Function.

  3. In the Add Member Function dialog box:

    ClassWizard adds the declaration to the Public section of the header file and creates a starter definition in the implementation file.

  4. From ClassView, jump to the newly-created Refresh and implement it with the following code.
    UpdateAllViews(NULL);
    SetModifiedFlag();
    

To implement the mouse click handler

  1. Using WizardBar, select CAutoClickView from the Class list.

  2. Click the action arrow located on the right end of WizardBar.

  3. Click Add Windows Message Handler.

    The New Windows Message and Event Handlers dialog box appears.

  4. From the New Windows messages/events list box, select WM_LBUTTONDOWN.

  5. Click Add and Edit.

  6. Replace the highlighted //TODO comment with the following code:
    CAutoClickDoc* pDoc = GetDocument();
    pDoc->m_pt = point;
    pDoc->Refresh();
    

To implement the Change Text dialog box

  1. On the Insert menu, click Resource.

    The Insert Resource dialog box appears.

  2. Select Dialog and click OK.

  3. Open and pin down the dialog’s properties page. (To open the properties page, right click on the dialog box and click Properties.)

  4. In the Dialog Properties page, type the following information:
  5. Add two controls to the dialog box:
  6. From the View menu, click ClassWizard.

    The Adding a Class dialog box appears, with a message that IDD_CHANGE_TEXT is a new resource, and with the Create a new class option selected by default.

    ClassWizard knows that a class hasn’t been defined yet for your dialog resource, so it displays this dialog box to enable you to define one.

    Note   If you had created the dialog class before creating the dialog resource, you could specify the Select an existing class option in this dialog box to connect the dialog box to the existing class.

  7. Click OK to create the dialog class.

    The Create New Class dialog box appears.

  8. Under Class Information, in the Name box, type CChangeText.

    Note the following default selections:

  9. Click OK.

    ClassWizard creates the class and returns you to the MFC ClassWizard dialog box. Click OK to close the dialog box.

  10. Switch to the IDD_CHANGE_TEXT dialog box in ResourceView.

  11. Hold down the CTRL key and double-click IDC_EDIT1. Type m_str in the Member Variable Name box to add the member variable for the edit control.

  12. Click OK.

To add the Change Text command to AutoClik’s Edit menu

  1. From ResourceView, expand the Menu folder and double-click IDR_ACLICKTYPE.

    The menu editor opens.

  2. Click AutoClik’s Edit menu.

  3. Add a separator below the Paste menu item.

  4. Add the following menu item text below the separator:

    Change &Text...

  5. Press ENTER.

    The menu editor automatically names the command ID_EDIT_CHANGETEXT. You can view this by selecting the menu item again.

  6. Type a prompt string such as:
    Change text displayed in the view.
    

Close the resource editors before proceeding to the next step.

To implement the handler for the Change Text command

  1. Using WizardBar, select CAutoClickDoc from the Class list.

  2. From the Filter list, select ID_EDIT_CHANGETEXT.

  3. Click the action arrow located on the right end of WizardBar.

  4. Click Add Windows Message Handler.

    The New Windows Message and Event Handlers dialog box appears. In the Class or object to handle list box, ID_EDIT_CHANGETEXT should be selected.

  5. Click COMMAND, and click Add and Edit. Accept the default name OnEditChangeText and click OK.

  6. Replace the highlighted //TODO comment with the following code:
    CChangeText dlg;
    dlg.m_str = m_str;
    if (dlg.DoModal())
    {
    m_str = dlg.m_str;
    Refresh();
    }
    
  7. Add the following #include statement to AutoClickDoc.cpp:
    #include "ChangeText.h"