客户端如何实现对MFC ActiveX的Event 的调用介绍

来源:互联网 发布:unix socket编程 编辑:程序博客网 时间:2024/04/30 11:41

请参看codeproject上的文章:

http://www.codeproject.com/KB/COM/ActiveXMfcState.aspx

 

 

Building the MFC client

    Our MFC client, is a plain old Appwizard generated Dialog-based application with additional Automation support. As usual, open VC++ 6.0 and create a new MFC Appwizard EXE project called MFCClient. Hit Build to build the project, and take a break from all that hard work!

The OLE/COM Object Viewer is a nifty little tool that is shipped along with Visual C++ 6.0. It will help us generate the IDL file for the DLL component. Go to Tools->OLE/COM Object Viewer and open this tool. Next, in OLE/COM Object Viewer, click on File->View Typelib and navigate to the VBEvent.dll file that we have previously built. Ready for some magic? Click on Open and open up ITypeLib Viewer Can you can view the IDL file? Whoa! Save the file through File->Save As, as VBEvents.IDL and close the tool. We have no need for it at present.

Next in our VC++ project, add this IDL file to the project. In FileView, right click on the IDL file and choose Settings. In the MIDL tab, set the Output header file name to VBEvents.h and the UUID filename to VBEvents_i.c. Also deselect the MkTyplib compatible option.

Save everything and in FileView, right-click on the VBEvents.IDL file and choose Compile. This will build the typelibrary and generate the necessary files.

Examine the MIDL generated VBEvents_i.c file. It contains all the UUID definitions that the client can use to build a sink object. In VBEvents.h, notice the dual interface _clsEventSrc. The component's dispinterface __clsEventSrc is identified by DIID___clsEvent. This is the event source for out custom event.

The next step is to add an sink object to connect to the source event. Fortunately, for us, MFC makes the task of building an event sink as easy as 1-2-3 (and well,4-5-6) With a couple of MFC macros, you can delegate much of the intricacy involved behind building a sink to MFC. First, add a new CCmdTarget derived class to the project called MFCSink. In the ClassWizard choose to select the Automation option. This is our sink object with Automation support.

Then import the server's typelib in the client with #import. If you haven't read my previous article, read it here.

 

 

 

同时关注微软的ActiveX例子:Circ

 

 

 

 

原创粉丝点击