1.为什么要用ActiveX?
网页本身的功能是有限的,要想实现一些网页本身不支持的功能,比如:网页上的p2p视频播放,就得靠ActiveX这种古老的技术。
2.c#能开发ActiveX吗?
严格意义上讲,c#是不能生成纯正ocx控件的,我们在vs.net中新建项目时,也找不到专门的"ActiveX项目"新建项,最多也只就能新建"类库"得到一个dll而非ocx(因此我们也无法用传统的regsvr32来注册该dll),但是c#能开发com组件,activeX控件本质上讲跟com是一类技术,所以用c#开发"能够让网页调用的com类库"还是可行的。
3.开发步骤:
(1)新建一个类库 (2)修改项目的"属性",在“生成”选项中把“输出”中的“为com互操作注册”勾中,然后再到“应用程序”选项中找到“程序集信息”按钮,点击它,在弹出的界面中勾中“使程序集COM可见(M)”
(3)修改AssemblyInfo.cs,增加[assembly: AllowPartiallyTrustedCallers()],完整内容类似下面这样:
代码
1 using System.Reflection; 2 using System.Runtime.CompilerServices; 3 using System.Runtime.InteropServices; 4 using System.Security; 5 6 // General Information about an assembly is controlled through the following 7 // set of attributes. Change these attribute values to modify the information 8 // associated with an assembly. 9 [assembly: AssemblyTitle("ActiveXDemo")] 10 [assembly: AssemblyDescription("")] 11 [assembly: AssemblyConfiguration("")] 12 [assembly: AssemblyCompany("Microsoft")] 13 [assembly: AssemblyProduct("ActiveXDemo")] 14 [assembly: AssemblyCopyright("Copyright ? Microsoft 2009")] 15 [assembly: AssemblyTrademark("")] 16 [assembly: AssemblyCulture("")] 17 [assembly: AllowPartiallyTrustedCallers()] 18 19 // Setting ComVisible to false makes the types in this assembly not visible 20 // to COM components. If you need to access a type in this assembly from 21 // COM, set the ComVisible attribute to true on that type. 22 [assembly: ComVisible(true)] 23 24 // The following GUID is for the ID of the typelib if this project is exposed to COM 25 [assembly: Guid("bd585d12-7f22-4b3f-959f-18efbfc53f94")] 26 27 // Version information for an assembly consists of the following four values: 28 // 29 // Major Version 30 // Minor Version 31 // Build Number 32 // Revision 33 // 34 // You can specify all the values or you can default the Build and Revision Numbers 35 // by using the ‘*‘ as shown below: 36 // [assembly: AssemblyVersion("1.0.*")] 37 [assembly: AssemblyVersion("1.0.0.0")] 38 [assembly: AssemblyFileVersion("1.0.0.0")]
(4)新建一个IObjectSafety接口文件IObjectSafety.cs,内容如下: