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

2024-02-04 14:08

本文主要是介绍Q143432: HOWTO: Gain Access to an ActiveX Control from its Property Page,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近一直在解决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 pageLPDISPATCH FAR *m_lpDispatch = GetObjectArray(&Ulong);// Get the CCmdTarget object associated to any one of the above// array elementspMyCtrl = (CMyCtrl*) CCmdTarget::FromIDispatch(m_lpDispatch[0]);// Cleanupreturn 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

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

这篇关于Q143432: HOWTO: Gain Access to an ActiveX Control from its Property Page的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/677755

相关文章

vue 父组件调用子组件的方法报错,“TypeError: Cannot read property ‘subDialogRef‘ of undefined“

vue 父组件调用子组件的方法报错,“TypeError: Cannot read property ‘subDialogRef’ of undefined” 最近用vue做的一个界面,引入了一个子组件,在父组件中调用子组件的方法时,报错提示: [Vue warn]: Error in v-on handler: “TypeError: Cannot read property ‘methods

MFC中Spin Control控件使用,同时数据在Edit Control中显示

实现mfc spin control 上下滚动,只需捕捉spin control 的 UDN_DELTAPOD 消息,如下:  OnDeltaposSpin1(NMHDR *pNMHDR, LRESULT *pResult) {  LPNMUPDOWN pNMUpDown = reinterpret_cast(pNMHDR);  // TODO: 在此添加控件通知处理程序代码    if

Cannot read property ‘length‘ of null while opening vscode terminal

同一问题地址:Cannot read property ‘length’ of null while opening vscode terminal 问题描述 One day, 我在ubuntu 18.04下用vscode打开一个项目,并想和往常一样在vscode使用终端,发现报错Cannot read property 'length' of null。 解决 打开setting.jso

2024年 Biomedical Signal Processing and Control 期刊投稿经验最新分享

期刊介绍 《Biomedical Signal Processing and Control 》期刊旨在为临床医学和生物科学中信号和图像的测量和分析研究提供一个跨学科的国际论坛。重点放在处理在临床诊断,患者监测和管理中使用的方法和设备的实际,应用为主导的研究的贡献。 生物医学信号处理和控制反映了这些方法在工程和临床科学的界面上被使用和发展的主要领域。期刊的范围包括相关的评论论文(review p

vue中路由管理(vue-router,page)使用总结

现在的项目都以模块化的方式去开发,所以在这样的开发模式下,如何更好的去管理路由是开发中所需要考虑的重点,幸运的是当前的开发中已经有了成熟的中间件去管理,我们只需要用就可以了 下面是我在学习vue-router的时候在原来基础上修改出来的demo,也是为了有助于对vue-router的理解 首先理解下vue官网的一个示例demo https://jsfiddle.net/yyx990803/x

Android 属性动画(Property Animation)

本文是学习以下三位大神之后,整理的学习笔记,彩蛋在编号6          http://blog.csdn.net/lmj623565791/article/details/38067475          http://www.cnblogs.com/angeldevil/archive/2011/12/02/2271096.html          http://www.tu

C#通过ACE OLEDB驱动程序访问 Access和 Excel

ACE 代表 Access Connectivity Engine。它是 Microsoft 提供的一组组件,用于访问和操作 Microsoft Access 数据库以及其他类似的文件格式,如 Excel 工作簿。ACE 主要包括以下几部分: ACE OLEDB 驱动程序:用于通过 OLE DB 提供程序访问 Access 数据库和 Excel 文件。例如,Microsoft.ACE.OLED

【0324】Postgres内核 Shared Buffer Access Rules (共享缓冲区访问规则)说明

0. 章节内容 1. 共享磁盘缓冲区访问机制 (shared disk buffers) 共享磁盘缓冲区有两套独立的访问控制机制:引用计数(a/k/a pin 计数)和缓冲区内容锁。(实际上,还有第三级访问控制:在访问任何属于某个关系表的页面之前,必须持有该关系表的适当类型的锁。这里不讨论关系级锁。) Pins 在对缓冲区做任何操作之前,必须“对缓冲区pin”(即增加其引用计数, re

基于ASP+ACCESS的教师信息管理系统

摘要 随着我国社会主义市场经济的发展和改革开放的不断深入,计算机的应用已遍及国民经济的各个领域,计算机来到我们的工作和生活中,改变着我们和周围的一切。在以前,学校用手工处理教师档案以及工资发放等繁多的工作和数据时,人手总会出现些不必要的错误和问题,特别是在查找,统计和保存方面。与此相适应,用计算机代替手工管理,是信息社会发展的必然趋势,也是我们计算机工作者的责任。教师信息管理系统开发主要包括数据

unable to access android sdk add-on list解决办法

mac环境,由于不小心删掉了sdk文件夹的内容,拷贝别人的文件内容过来后,发现sdkmanager不见了。 慌乱中重装了Android Studio。 打开app后发现如下提示:unable to access android sdk add-on list 解决办法: 在 Android Studio 安装目录 bin/idea.properties 文件最后追加一句 disabl