Q143432: HOWTO: Gain Access to an ActiveX Control from its Property Page

本文介绍如何在Visual C++中使用ActiveX控件的属性页访问控件派生类的成员函数和变量。通过利用属性页持有的IDispatch指针数组,可以实现对控件的直接操作。文章提供了详细的实现方法和代码示例。

最近一直在解决OCX在Visual Studio 2010环境下的Visual C++中,从PropertyPage中不能访问本体控件的问题。以前的Microsoft Knowledge Base都消失了,找到了Archive。转过来留存。
原文地址:Q143432: HOWTO: Gain Access to an ActiveX Control from its Property Page

Article: Q143432
Product(s): Microsoft C Compiler
Version(s): 1.0,1.5,1.51,1.52,2.0,2.1,2.2,4.0,5.0,6.0
Operating System(s):
Keyword(s): kbActiveX kbCOMt kbCtrl kbMFC kbVC100 kbVC150 kbVC200 kbVC500 kbVC600 kbGrpDSMFCATL kbA
Last Modified: 26-JUL-2002


The information in this article applies to:

  • The Microsoft Foundation Classes (MFC), used with:
    • Microsoft Visual C++ for Windows, 16-bit edition, versions 1.0, 1.5, 1.51, 1.52
    • Microsoft Visual C++, 32-bit Editions, versions 2.0, 2.1, 2.2, 4.0
    • Microsoft Visual C++, 32-bit Enterprise Edition, versions 5.0, 6.0
    • Microsoft Visual C++, 32-bit Professional Edition, versions 5.0, 6.0
    • Microsoft Visual C++, 32-bit Learning Edition, version 6.0
    • Microsoft Visual C++.NET (2002)

SUMMARY

When using an ActiveX control, you find situations where there is a need to call
member functions or gain access to member variables of the control derived class
from its associated property page. This can be achieved by making use of the
array of IDispatch pointers (held by each property page) that represent the
objects being affected due to the manipulations done through the property page.
This article explains in detail how this can implemented and gives a code sample
to illustrate it.

MORE INFORMATION

Property sheets, in an ActiveX control, allow an end user to directly manipulate
the control’s properties by displaying one or more property pages that display a
collection of properties. These properties could belong either to one particular
control or to a collection of ActiveX controls.

Each ActiveX control property page is an in-proc object with its own CLSID that
implements the interface IPropertyPage. The IPropertyPage::SetObjects member
function is used to provide a property page with pointers to the objects
(IUnknowns) manipulated by this particular page. Please refer to the OLE
Programmer’s Reference, Vol. 1, for more information about the SetObjects
function.

The MFC implementation for the IPropertyPage interface stores the object pointers
as an array of IDispatchs representing the controls that are affected by a
particular property page. This array can be accessed by using
COlePropertyPage::GetObjectArray(). The property pages in MFC make use of this
IDispatch array to apply the changes directly to those objects (that is, the
controls) by creating a COleDispatchDriver-derived class, attaching the
IDispatch to this class, and invoking the SetProperty/GetProperty of
COleDispatchDriver to convey the change to the control-derived class.

An ActiveX Control generated using the ControlWizard creates a property page that
can be used to manipulate the properties of one particular ActiveX control
rather than manipulating a collection of controls. Hence, the control associated
to a property page can be accessed by obtaining the previously mentioned
IDispatch array in the COlePropertyPage and calling the static function
CCmdTarget::FromIDispatch to return a pointer to the CCmdTarget object
associated with any one of the IDispatchs. The sample code section of this
article illustraties this method.

Note that calling CCmdTarget::FromIDispatch(), for an IDispatch pointer belonging
to an ActiveX Control, will always return NULL in versions before MFC 4.x. For
more information about this problem, please see the following article in the
Microsoft Knowledge Base:

Q138414 PRB: FromIDispatch Returns NULL for OLE Control

This is no longer a problem in versions MFC 4.x.

Sample Code

     // The header file of the control-derived class must be included in
     // the same source file.
     #include "myctrl.h"

     CMyCtrl* CMyPropPage::GetControlClass()
     {
       CMyCtrl *pMyCtrl;
       ULONG Ulong;

       // Get the array of IDispatchs stored in the property page
       LPDISPATCH FAR *m_lpDispatch = GetObjectArray(&Ulong);

       // Get the CCmdTarget object associated to any one of the above
       // array elements
       pMyCtrl = (CMyCtrl*) CCmdTarget::FromIDispatch(m_lpDispatch[0]);

       // Cleanup
       return pMyCtrl;
     }

     // If your control has a public member variable, in this case
     // I am using m_direct_control, then that variable can be
     // manipulated as follows.

     void CMyPropPage::OnLButtonDown(UINT nFlags, CPoint point)
        {
          // Modify a member variable of Control directly.
          CMyCtrl *pMyCtrl = GetControlClass();
          if (pMyCtrl)
          {
             pMyCtrl->m_direct_control++;
   
             // Display the new value of the variable in a message box.
             char buf[100];
             AfxMessageBox (_itoa (pMyCtrl->m_direct_control, buf, 10));
          }
   
          COlePropertyPage::OnLButtonDown(nFlags, point);
        }

In this code, it is assumed that the array of IDispatchs returned from
GetObjectArray holds the same IDispatch pointer because in a default
ControlWizard-generated application, each property page manipulates a particular
ActiveX control.

IMPORTANT NOTE: You may get NULL returned from CCmdTarget::FromIDispatch() when
the control is created with aggregation support. This behavior is noticable in
containers such as Visual C++ 6.0 ActiveX Test Container, Excel 97, Excel 2000,
Frontpage 98, and perhaps others. Therefore, the proposed method above won’t
work in those containers. A possible workaround is to make the control
nonaggregatable by setting the following in the control constructor:

  CPropPageAccessCtrl::CPropPageAccessCtrl()
  {
      InitializeIIDs(&IID_DPropPageAccess, &IID_DPropPageAccessEvents);

  //new code
  //no aggregation please!  
      m_xInnerUnknown = 0; //Base class COleControl set this with call to EnableAggregation()
  //end of new code
  }

While this workaround will work for Visual C++ ActiveX Test Container 6.0, this
workaround is not an option for containers that only support such aggregatable
controls as Excel 97 and Excel 2000. Disabling aggregation would prevent end
users from adding the control to an Excel spreadsheet. One could add a long
property to the control and set it to the “this” pointer of the control. Then,
one could retrieve this property from the page, do a cast on the value to the
control’s type, and use it. For additional information about this method, please
click the article number below to view the article in the Microsoft Knowledge
Base:

Q205670 HOWTO: Get Access to an ActiveX Control from its Property Page

Additional query words: ocx visualc

======================================================================
Keywords : kbActiveX kbCOMt kbCtrl kbMFC kbVC100 kbVC150 kbVC200 kbVC500 kbVC600 kbGrpDSMFCATL kbArchitecture
Technology : kbAudDeveloper kbMFC
Version : :1.0,1.5,1.51,1.52,2.0,2.1,2.2,4.0,5.0,6.0
Issue type : kbhowto

=============================================================================

【评估多目标跟踪方法】9个高度敏捷目标在编队中的轨迹和测量研究(Matlab代码实现)内容概要:本文围绕“评估多目标跟踪方法”,重点研究9个高度敏捷目标在编队飞行中的轨迹生成与测量过程,并提供完整的Matlab代码实现。文中详细模拟了目标的动态行为、运动约束及编队结构,通过仿真获取目标的状态信息与观测数据,用于验证和比较不同多目标跟踪算法的性能。研究内容涵盖轨迹建模、噪声处理、传感器测量模拟以及数据可视化等关键技术环节,旨在为雷达、无人机编队、自动驾驶等领域的多目标跟踪系统提供可复现的测试基准。; 适合人群:具备一定Matlab编程基础,从事控制工程、自动化、航空航天、智能交通或人工智能等相关领域的研究生、科研人员及工程技术人员。; 使用场景及目标:①用于多目标跟踪算法(如卡尔曼滤波、粒子滤波、GM-CPHD等)的性能评估与对比实验;②作为无人机编队、空中交通监控等应用场景下的轨迹仿真与传感器数据分析的教学与研究平台;③支持对高度机动目标在复杂编队下的可观测性与跟踪精度进行深入分析。; 阅读建议:建议读者结合提供的Matlab代码进行实践操作,重点关注轨迹生成逻辑与测量模型构建部分,可通过修改目标数量、运动参数或噪声水平来拓展实验场景,进一步提升对多目标跟踪系统设计与评估的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值