C++ 開發OCX 的方法和注意事項


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 的操作差不多了。這里只是入門方法。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM