码迷,mamicode.com
首页 > 编程语言 > 详细

C++ 开发OCX 的方法和注意事项

时间:2017-07-29 11:33:54      阅读:372      评论:0      收藏:0      [点我收藏+]

标签:blog   multi   one   lin   返回值   ror   不同   when   active   

C++ 开发OCX 的方法和注意事项
前言 

  ActiveX控件是一种实现了一系列特定接口而使其在使用和外观上更象一个控件的COM组件。ActiveX控件这种技术涉及到了几乎所有的COM和OLE的技术精华,如可链接对象、统一数据传输、OLE文档、属性页、永久存储以及OLE自动化等。

   ActiveX控件作为基本的界面单元,必须拥有自己的属性和方法以适合不同特点的程序和向包容器程序提供功能服务,其属性和方法均由自动化服务的 IDispatch接口来支持。除了属性和方法外,ActiveX控件还具有区别于自动化服务的一种特性--事件。事件指的是从控件发送给其包容程序的一 种通知。与窗口控件通过发送消息通知其拥有者类似,ActiveX控件是通过触发事件来通知其包容器的。事件的触发通常是通过控件包容器提供的 IDispatch接口来调用自动化对象的方法来实现的。在设计ActiveX控件时就应当考虑控件可能会发生哪些事件以及包容器程序将会对其中的哪些事 件感兴趣并将这些事件包含进来。与自动化服务不同,ActiveX控件的方法、属性和事件均有自定义(custom)和库存(stock)两种不同的类 型。自定义的方法和属性也就是是普通的自动化方法和属性,自定义事件则是自己选取名字和Dispatch ID的事件。而所谓的库存方法、属性和事件则是使用了ActiveX控件规定了名字和Dispatch ID的"标准"方法、属性和事件。

   ActiveX控件可以使COM组件从外观和使用上能与普通的窗口控件一样,而且还提供了类似于设置Windows标准控件属性的属性页,使其能够在包 容器程序的设计阶段对ActiveX控件的属性进行可视化设置。ActiveX控件提供的这些功能使得对其的使用将是非常方便的。本文下面即以MFC为工 具对ActiveX控件的开发进行介绍。
(上面的简介纯属百度,看看了解了解就行不重要,但是我们需要知道的是,这种控件能做什么,这才是我们关心的。)
OCX 控件通过注册,可以像普通控件一样被拖拉使用,相当于我们自定义了一个控件。但是不同的是,该控件C++ 可以调用,C#可以调用,甚至可以用于WEB页面。后面我们一个一个编写测试用例。

开发OCX 控件
我们使用vs2010 开发OCX控件,新建一个 MFC ACTIVEX CONTROL 解决方案。然
后下一步,下一步走就好。
技术分享
这时候我们尝试编译一下该工程,可能会有错误
错误:
error MSB8011: Failed to register output. Please try enabling Per-user Redirection or register the component from a command prompt with elevated permissions. C:\Program Files\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppCommon.targets 744

解决方法:
This error happens when Visual Studio (2010) can‘t find the dependent dll files that are required by the program for creating the OCX. The OCX is created in the Debug directory of the project (for Debug builds), and specifying the "Working directory" (in project settings) as the folder where the dependent DLL‘s are, won‘t help Visual Studio in locating the DLL‘s.
Now the catch is (this is what makes it a headache to solve the problem) that sometimes, without Visual Studio being able to create the OCX, you won‘t be able to run Dependency Walker on the OCX. So you‘ll never figure out that the missing DLL‘s are the problem.
If you know which DLL‘s need to be placed in the Debug folder, just place them there, and the error will disappear. If you don‘t know, and if your OCX didn‘t get created, then go to project settings > Linker > General > Register Output and set the value to "No". This will create your OCX for you, but won‘t go through the process of registering it, which is when it needs the dependent DLL‘s.
Once you double-click the OCX and Dependency Walker shows you the missing DLL‘s with yellow circle icons, just place those DLL‘s in the same folder as the OCX, and your program will work fine. It‘s that simple.
(参考 https://stackoverflow.com/questions/10477117/solving-error-msb8011-failed-to-register-output)
错误:
Please use the /MD switch for _AFXDLL builds
解决方法:
工程->设置->C/C++,在分类中选中“Code Generation”,然后在Use run-time library下选中Multithreaded DLL或Debug Multithreaded DLL
(参考http://blog.sina.com.cn/s/blog_7d1dc9de01012yge.html)


然后我们给控件添加一个方法叫Hello1

 技术分享


返回值设置字符串BSTR 一个参数 类型 BSTR 参数名 str

技术分享
然后 编码Hello1方法的实现,该方法在mfcActiveCtrl.cpp中。该方法原样返回参数值。

第二步:我们给控件添加一个事件DbClick
技术分享

技术分享

好了,这个时候,我们的这个OCX 控件已经有了一个Hello1的方法,和一个DbClick事件。
其他的就和MFC 的操作差不多了。这里只是入门方法。

C++ 开发OCX 的方法和注意事项

标签:blog   multi   one   lin   返回值   ror   不同   when   active   

原文地址:http://www.cnblogs.com/techdreaming/p/7253857.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!