Step 4: Adding a Custom Event to the ActiveX Control
This step demonstrates how to add a custom event (PointerPosition) to the ActiveX control. Each time the user moves the mouse pointer, the member function handleEvent of the view interactor will fire the event and send it to the container. This event has three parameters:
A
BSTR parameter, which represents the name of the object underneath the mouse, if any.
OLE_XPOS_PIXELS and
OLE_YPOS_PIXELS parameters, which represent the position of the mouse relative to the
IlvManagerView.
You are going to:
Define the new custom event.
1. In the ClassView tab of the Project Workspace window click the DemoMFCCtrl interface with the right mouse button.
2. Choose Add > Add Event from the menu that appears.
3. In the dialog box, enter the following values:
PointerPosition in
Event Name,
FirePointerPosition in
Internal Name,
BSTR name,
OLE_XPOS_PIXELS x,
OLE_YPOS_PIXELS y as the parameters.
4. Rebuild your project.
5. In DemoMFCCtrl.h, check that the FirePointerPosition method is declared as public and that its first parameter is typed as BSTR.
Modify the constructor of the class DemoCtrl.
Modify this constructor so that it accepts a reference to the class CDemoMFCCtrl as its second parameter. Add a forward declaration for the class CDemoMFCCtrl to the file DemoCtrl.h and include the files stdafx.h, DemoMFC.h, and DemoMFCCtl.h into the file DemoCtrl.cpp.
Note: You must include these files before the Rogue Wave Views header files. |
Modify the member function CDemoMFCCtrl::onCreate.
In the member function CDemoMFCCtrl::OnCreate (file DemoMFCCtl.cpp), add *this as the second parameter to the call to the DemoCtrl constructor.
Define the class TrackInteractor.
In the file DemoCtrl.cpp, define a new class TrackInteractor deriving from IlvViewManagerInteractor:
class TrackInteractor : public IlvManagerViewInteractor
{
public:
TrackInteractor(IlvManager* m, IlvView* v, CDemoMFCCtrl& MFCCtrl)
: IlvManagerViewInteractor(m, v), m_MFCCtrl(MFCCtrl)
{
}
~TrackInteractor() {}
void handleEvent(IlvEvent& event)
{
if (event.getType() == IlvPointerMoved) {
IlvGraphic* obj =
getManager()->lastContains(IlvPoint(event.x(), event.y()),
getView());
CString mfcName;
if (obj) {
const char* name = getManager()->getObjectName(obj);
mfcName = name;
}
BSTR bstrName = mfcName.AllocSysString();
m_MFCCtrl.FirePointerPosition(&bstrName,
OLE_XPOS_PIXELS(event.x()),
OLE_XPOS_PIXELS(event.y()));
::SysFreeString(bstrName);
}
if (!getManager()->dispatchToObjects(event, getView()))
getManager()->shortCut(event, getView());
}
private:
CDemoMFCCtrl& m_MFCCtrl;
};
Add the private m_Inter variable to the DemoCtrl class.
To add this variable you can use the wizard as explained in
Step 3: Adding Custom Properties to the ActiveX Control. Do not forget to include a forward declaration for the
TrackInteractor class in the
DemoCtrl.h file.
Declare the class TrackInteractor as a friend of the class CDemoMFCCtrl (in the file DemoMFCCtl.h).
Initialize the m_Inter variable in the DemoCtrl constructor and attach it to the view through the manager.
DemoCtrl::DemoCtrl(HWND hWnd, CDemoMFCCtrl& MFCCtrl)
: Ctrl(_Display, reinterpret_cast<IlvSystemView>(hWnd)), m_Inter(0)
{
m_Inter = new TrackInteractor(getManager(), getManagerView(), MFCCtrl);
getManager()->setInteractor(m_Inter, getManagerView());
}
Modify the destructor to delete the interactor stored in m_Inter.
DemoCtrl::~DemoCtrl()
{
if (m_Inter) {
getManager()->setInteractor(0, getManagerView());
delete m_Inter;
m_Inter = 0;
}
}
Test the properties.
The function DemoMFCCtrl_PointerPosition is called each time a PointerPosition event is fired. It displays the coordinates of the object which the mouse is over, if any, and its name, if defined:
<HTML>
<HEAD>
<TITLE>Test page for object DemoMFCCtl</TITLE>
</HEAD>
<SCRIPT LANGUAGE = "javascript">
<!--
function load(value)
{
DemoMFCCtrl.FileName = value;
}
-->
</SCRIPT>
<SCRIPT LANGUAGE = "VBScript">
<!--
Sub DemoMFCCtrl_PointerPosition(name, x, y)
text1.innerText = name & " (" & x & ", " & y & ")"
End Sub
-->
</SCRIPT>
<BODY>
<P>
<OBJECT WIDTH=400 HEIGHT=400 ID="DemoMFCCtrl" CLASSID="CLSID:3D31E7B8-400B-
11D3-B74F-00C04F68A89D"></OBJECT>
<P>
<INPUT id=text1 name=text1>
</P>
</BODY>
</HTML>
Note: Since the class id is generated, the class id in your file may be different from the one in this example. |
See the project VB/testMFC.vbp for an example of how to run a test via Visual Basic. The function DemoMFCCtrl1_PointerPosition is called each time a PointerPosition event is fired.
Version 5.7
Copyright © 2013, Rogue Wave Software, Inc. All Rights Reserved.