HOWTO: Get Access to an ActiveX Control from its Property Page
ID: Q205670
|
The information in this article applies to:
-
The Microsoft Foundation Classes (MFC), included with:
-
Microsoft Visual C++, 32-bit Editions, versions 5.0, 6.0
SUMMARY
When using an ActiveX control, you may find situations where there is a need to call member functions or gain access to member variables of the control class from its associated property pages. This article explains in detail how this can be implemented and provides a code sample as illustration.
MORE INFORMATION
A property sheet in an ActiveX control allows a user to manipulate the control's properties by displaying one or more property pages. The properties could belong to either one control or to a collection of ActiveX controls of the same type. The code sample provided shows how to access the properties of a control or a group of selected controls from a control's property page, and get direct access to the control instance.
The following steps illustrate what needs to be done:
- Generate a new MFC ActiveX control and specify the project name as MyTest. Accept all of the defaults for the control when prompted.
- Add a new member function to the control. Name the function SayHello, with return type void. Add the following code to it:
void CMyTestCtrl::SayHello()
{
AfxMessageBox("Hello"); //Add this statement.
}
- On the View menu, click ClassWizard.
- Click the Automation tab in the MFC ClassWizard dialog box, and then add a new property for the control of Type long. Set the External name field to ControlPointer, select Get/Set methods for the Implementation, and then accept the rest of the settings.
- Use the following code for the generated ControlPointer property methods:
long CMyTestCtrl::GetControlPointer()
{
return reinterpret_cast<long>(this);
}
void CMyTestCtrl::SetControlPointer(long /*nNewValue*/)
{
SetNotSupported();
}
- Build the project to make the type library before proceeding.
- Generate automation wrappers for the control. Click the Automation tab in Class Wizard, click Add class, and then click From a type library. Find the .tlb file that was generated for the control. It should be in the Debug directory and named "MyTest.tlb". Select only the "_DMyTest" interface and click OK. The event interface is not needed.
- Using the resource editor, add a button to the property page.
Change the caption to "Access all selected controls".
- Add the control's header file after the other includes in the property page source file.
#include "MyTestCtl.h"
- Add a button click handler for the button by using ClassWizard.
- Add the following code in the button click handler function:
void CMyTestPropPage::OnButton1()
{
// Add this code:
ULONG uNumControls;
// Get the array of IDispatchs stored in the property page.
LPDISPATCH *lpDispatchControls = GetObjectArray(&uNumControls);
for (ULONG i = 0; i < uNumControls; i++)
{
CMyTestCtrl *pMyCtrl = GetControl(lpDispatchControls, i);
if (pMyCtrl)
pMyCtrl->SayHello();
}
}
- Add the following code before the button click handler function:
CMyTestCtrl* GetControl(LPDISPATCH *lpDispatchControls, ULONG iControlIndex)
{
CMyTestCtrl *pMyCtrl = NULL;
// Get the CCmdTarget object associated.
pMyCtrl = (CMyTestCtrl*) CCmdTarget::FromIDispatch(lpDispatchControls[iControlIndex]);
if (!pMyCtrl) // Above failed. Container must have aggregated the control.
{
long ControlPointer;
_DMyTest control(lpDispatchControls[iControlIndex]);
// GetObjectArray() docs state must not release pointer.
control.m_bAutoRelease = FALSE;
ControlPointer = control.GetControlPointer();
pMyCtrl = reinterpret_cast<CMyTestCtrl*>(ControlPointer);
}
return pMyCtrl;
}
- Build the Project.
- Test in Visual C++ 6.0 ActiveX Test Container.
You should see the message box when clicking the Access all selected controls button on the property page.
REFERENCES
For additional information, click the article number below
to view the article in the Microsoft Knowledge Base:
Q143432 HOWTO: Gain Access to an ActiveX Control from its Property Page
Additional query words:
property
Keywords : kbActiveX kbCOMt kbContainer kbCtrlCreate kbMFC kbPropSheet kbVC500 kbVC600 kbDSupport kbGrpMFCATL
Version : winnt:5.0,6.0
Platform : winnt
Issue type : kbhowto