How to obtain access to an ActiveX control from its property page 如何从一个控件的属性页中获取对该控件的访问?

How to obtain access to an ActiveX control
from its property page
如何从一个控件的属性页中获取对该控件的访问?

This article was previously published under Q205670
这篇文章的发布编号为 Q205670

Note Microsoft Visual C++ .NET (2002) and Microsoft Visual C++ .NET (2003) support both the managed code model that is provided by the .NET Framework and the unmanaged native Windows code model. The information in this article applies to unmanaged Visual C++ code only.
注: Microsoft Visual C++.NET (2002) 和 Microsoft Visual C++ .NET (2003) 支持由 .NET Framework 提供的托管和非托管Windows 代码模型。 这篇文章中的信息仅仅适用于非托管 Visual C++ 代码。

SUMMARY简介/概要
When you use an ActiveX control, you may have to call member functions or gain access to member variables of the control class from its associated property pages. This article explains to do this and provides a code sample as illustration.
当你使用ActiveX控件时,你可能会通过控件的属性页访问控件的成员变量或调用控件的成员函数。这篇文章就是来说明这个问题,并给出一个代码示例作为参考。

MORE INFORMATION更多信息
A property sheet in an ActiveX control allows a user to manipulate the properties of a control by displaying one or more property pages. The properties can belong to one control or to a collection of ActiveX controls of the same type. The code sample demonstrates how to access the properties of a control or a group of selected controls from the property page of a control. It also demonstrates how to get direct access to the control instance.
 一个ActiveX控件的属性页允许通过显示一个或多个属性页来操纵控件的属性。这些属性可以依附于一个控件或具有相同类型的控件集。该代码示例演示了如何通过一个控件的属性页访问控件的属性或一组被指定的属性。另外,它还演示了直接获取控件实例的方法。
1、Generate a new MFC ActiveX control and specify the project name as MyTest. Accept all of the defaults for the control when prompted.
2、Add a new member function to the control. Name the function SayHello, with return type void. Add the following code to it:
 1、创建一个新的MFC ActiveX工程,命名为 MyTest。向导生成时,接受所有的默认选项。
 2、添加Ctrl类的成员函数SayHello,返回值为void,实现代码如下:

void CMyTestCtrl::SayHello()
{
    AfxMessageBox("Hello"); //Add this statement.
}
3、Create a property: 创建一个新的属性
In Visual C++ .NET
Open the Class View window and then open MyTestLib (this is the node that represents the IDL file for the project).
Right-click the subnode for the default dispinterface (_DMyTest), Add, and then click Add Property.
In the Add Property Wizard, set the property type to LONG and the property name to ControlPointer. For Implementation, click to select Get/Set methods.
Click Finish to generate the property.
In earlier versions of Visual C++
Click the Automation tab in the MFC ClassWizard dialog box, and then add a new property for the control of type LONG. 点击MFC类向导对话框的自动化标签,添加一个LONG类型的控件属性
Set the External name field to ControlPointer, select Get/Set methods for Implementation, and then accept the rest of the settings.设置属性的外部名字为ControlPointer,选择Get/Set方法,其它设置选择默认。
4、Use the following code for the generated ControlPointer property methods:
ControlPointer属性的实现代码如下:
long CMyTestCtrl::GetControlPointer()
{
    return reinterpret_cast<long>(this);
}
void CMyTestCtrl::SetControlPointer(long /*nNewValue*/)
{
    SetNotSupported();
}
5、Build the project to make the type library before proceeding. 编译控件之前首先重建工程的类型库
6、Generate automation wrappers for the control: 创建自动化控件
In Visual C++ .NET
On the Project menu, click Add Class Project.
In the Add Class dialog box, click to select MFC Class from Typelib, and then click OK.
In the Add Class From Typelib Wizard, locate Add class from, and then click to select File.
Find the .tlb file that was generated for the control. The file is located in the Debug directory and is named MyTest.tlb. Use the selection arrows to move only the _DMyTest interface into the Generated classes pane. Note You must now change the Generated Classes class from the default CDMyTest to _DMyTest. This also changes the generated class names for the wrappers. The wizard prepends a 'C' to the generated wrapper classes, which may cause problems when you use some type libraries. Changing names is necessary for this type library to make the code match between Visual C++ .NET and earlier versions.
Click Finish to generate the wrapper header files.
In earlier versions of Visual C++
①、Click the Automation tab in the Class Wizard, click Add class, and then click From a type library. 选择类向导的自动化标签,点击Add Class…,然后选择From a type library
②、Find the .tlb file that was generated for the control. It is located in the Debug directory and is named MyTest.tlb. 找到编译控件后产生的.tlb文件,它位于Debug目录,名字为MyTest.tlb
③、Select only the _DMyTest interface and click OK. The event interface is not needed. 仅仅选择 _DmyTest接口,点击OK即可。事件接口不需要选择。
7、Using the resource editor, add a button to the property page. Change the caption to Access all selected controls. 在属性对话框中,添加一个 Button按钮。改变它的内容为 存取所有被选中的控件
8、Add the control's header file after the other includes in the property page source file: 在属性页的.cpp文件中包含头文件
#include "MyTestCtl.h"
9、Add a button click handler for the button by using the Class Wizard.使用类向导为刚才创建的Button按钮,添加实现代码
10、Add the following code in the button click handler function: 在Button的处理函数中添加如下代码
  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();
        }
}
11、Add the following global code method before the button click handler function: 在Button函数的处理之前添加一个全局函数 GetControl
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;
    }
12、Build the project. 重建工程
13、Test the project in Visual C++ ActiveX Test Container. You see the message box when you click the Access all selected controls button on the property page.使用测试控件工程。当你点击属性页的Button时,你会看到一个消息框。

For more information, click the following article number to view the article in the Microsoft Knowledge Base: 对于从属性页中访问控件的方法或成员,你可以在MSKB中查看编号为 143432的文章。
143432  (http://support.microsoft.com/kb/143432/ ) How to gain access to an ActiveX control from its property page 如何从一个属性页中获取ActiveX控件的访问


 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值