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
CPoint m_pt;
CString m_str;
CAutoClickDoc
constructor and add the following lines (after the EnableAutomation
call):m_pt = CPoint(10,10);
m_str = _T("Automation!");
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
OnDraw
member function of CAutoClickView
and add the following line, just after the ASSERT_VALID
call (you can replace the //TODO comment):pDC->TextOut(pDoc->m_pt.x, pDoc->m_pt.y,
pDoc->m_str);
The implementations of OnLButtonDown
and OnEditChangeText
make use of the helper function, Refresh
.
To implement the Refresh helper function
CAutoClickDoc
.void
.Refresh()
.ClassWizard adds the declaration to the Public section of the header file and creates a starter definition in the implementation file.
Refresh
and implement it with the following code.UpdateAllViews(NULL);
SetModifiedFlag();
To implement the mouse click handler
The New Windows Message and Event Handlers dialog box appears.
CAutoClickDoc* pDoc = GetDocument();
pDoc->m_pt = point;
pDoc->Refresh();
To implement the Change Text dialog box
The Insert Resource dialog box appears.
IDD_CHANGE_TEXT
.Change Text
.
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.
The Create New Class dialog box appears.
CChangeText
.Note the following default selections:
ClassWizard creates the class and returns you to the MFC ClassWizard dialog box. Click OK to close the dialog box.
IDD_CHANGE_TEXT
dialog box in ResourceView.m_str
in the Member Variable Name box to add the member variable for the edit control.To add the Change Text command to AutoClik’s Edit menu
The menu editor opens.
Change &Text...
The menu editor automatically names the command ID_EDIT_CHANGETEXT
. You can view this by selecting the menu item again.
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
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.
OnEditChangeText
and click OK.CChangeText dlg;
dlg.m_str = m_str;
if (dlg.DoModal())
{
m_str = dlg.m_str;
Refresh();
}
#include "ChangeText.h"