本文介绍了如何创建简单的ActiveX控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在尝试创建真正的简单 ActiveX 控件。 我真的不在乎它现在做了什么。 我只想调用其中一种方法。 我得到"对象不支持属性或方法'PrintDocument'。" 我错过了什么? 请参阅以下代码... 项目(C#类库):BrowserPrintDocument 注意:程序集是ComVisible,已签名并分配了GUID。 还有一个安装项目(BrowserPrintDocumentSetup) IBrowserPrinter.cs(界面) 使用System; 使用System.Runtime.InteropServices; 名称空间BrowserPrintDocument { ///< summary> ///此界面将方法公开给javascript ///< / summary> [Guid(" 68BD4E0D-D7BC-4CF6-BEB7-CAB950161E79")] [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] public interface IBrowserPrinter { / /将DispIdAttribute添加到源接口中的任何成员以指定COM DispId。 [DispId(0x60020001)] void PrintDocument(); } } BrowserPrinter.cs(codebase) 使用System;使用System.Drawing ; 使用System.Drawing.Printing; 使用System.Runtime.InteropServices; 名称空间BrowserPrintDocument { [ProgId(" BrowserPrinter")] [Guid(" 23F51170-D125-4665-9909-701B6F233AF2")] [ComVisible(true)] [ClassInterface(ClassInterfaceType.None)] public class BrowserPrinter:IBrowserPrinter { [ComVisible(true)] public void PrintDocument() { var printDocument = new PrintDocument(); printDocument.PrintPage + =(sender,args)=> { var image = Image.FromFile(@"C:\ Users \ Juulio.Bello \Pictures\untitled.png"); args.Graphics.DrawImage(image,0,0,image.Width,image.Height); }; printDocument.Print(); } /////< summary> /////将类注册为控件并设置其CodeBase条目 /////< / summary> /////< param name =" key">控件的注册表项< / param> // [ComRegisterFunction()] // public static void RegisterClass(string key) // { //} /// //<总结> /////被叫取消注册控件 /////< / summary> /////< param name =" key">注册表项< / param> // [ComUnregisterFunction()] // public static void UnregisterClass(string key) // { //} } } 项目(VB WebForm应用程序):测试 BrowserPrinter BrowserPrinter.aspx <%@ Page Language =" VB" AutoEventWireup = QUOT假QUOT;的CodeFile = QUOT; BrowserPrinter.aspx.vb"继承= QUOT; BrowserPrinter" %GT; <!DOCTYPE html> < html xmlns =" http://www.w3.org/1999/xhtml"> < head runat =" server"> < title>< / title> < / head> < body> < form id =" form1" RUNAT = QUOT;服务器"> < div> < object id =" BrowserPrinter"名称= QUOT; BrowserPrinter"的classid = QUOT; CLSID:23F51170-D125-4665-9909-701B6F233AF2"> < / div> < / form> < script type =" text / javascript"> 函数OpenActiveX(){ try { document.BrowserPrinter.PrintDocument(); } catch(Err){ alert(Err.description); } } OpenActiveX(); < / script> < / body> < / html> BrowserPrinter.aspx.vb Partial Class BrowserPrinter Inherits System.Web.UI.Page End Class 什么我错过了吗? 解决方案 您的C#代码实现了COM对象,但您的HTML代码需要ActiveX控件。一个合适的ActiveX控件需要实现其他几个接口。 我认为你不需要ActiveX控件。只需将您的HTML代码更改为: var BrowserPrinter = new ActiveXObject(" BrowserPrinter"); // ProgID是参数 BrowserPrinter.PrintDocument(); 并省略< div> ...< / div> I am trying to create a really simple ActiveX control. I don't REALLY care what it does for now. I just want to be able to invoke one of its methods. I get "Object doesn't support property or method 'PrintDocument'." What am I missing? Please see code below...Project (C# Class Library): BrowserPrintDocumentNote: The assembly is ComVisible, signed and is assigned a GUID. There is also a Setup project (BrowserPrintDocumentSetup)IBrowserPrinter.cs (interface)using System;using System.Runtime.InteropServices;namespace BrowserPrintDocument{ /// <summary> /// This interface exposes methods to javascript /// </summary> [Guid("68BD4E0D-D7BC-4CF6-BEB7-CAB950161E79")] [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] public interface IBrowserPrinter { //Add a DispIdAttribute to any members in the source interface to specify the COM DispId. [DispId(0x60020001)] void PrintDocument(); }}BrowserPrinter.cs (codebase)using System;using System.Drawing;using System.Drawing.Printing;using System.Runtime.InteropServices;namespace BrowserPrintDocument{ [ProgId("BrowserPrinter")] [Guid("23F51170-D125-4665-9909-701B6F233AF2")] [ComVisible(true)] [ClassInterface(ClassInterfaceType.None)] public class BrowserPrinter : IBrowserPrinter { [ComVisible(true)] public void PrintDocument() { var printDocument = new PrintDocument(); printDocument.PrintPage += (sender, args) => { var image = Image.FromFile(@"C:\Users\Julio.Bello\Pictures\untitled.png"); args.Graphics.DrawImage(image, 0, 0, image.Width, image.Height); }; printDocument.Print(); } /////<summary> /////Register the class as a control and set its CodeBase entry /////</summary> /////<param name="key">The registry key of the control</param> //[ComRegisterFunction()] //public static void RegisterClass(string key) //{ //} /////<summary> /////Called to unregister the control /////</summary> /////<param name="key">The registry key</param> //[ComUnregisterFunction()] //public static void UnregisterClass(string key) //{ //} }}Project (VB WebForm Application): TestBrowserPrinterBrowserPrinter.aspx<%@ Page Language="VB" AutoEventWireup="false" CodeFile="BrowserPrinter.aspx.vb" Inherits="BrowserPrinter" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title></head><body> <form id="form1" runat="server"> <div> <object id="BrowserPrinter" name="BrowserPrinter" classid="clsid:23F51170-D125-4665-9909-701B6F233AF2"> </div> </form> <script type="text/javascript"> function OpenActiveX() { try { document.BrowserPrinter.PrintDocument(); } catch (Err) { alert(Err.description); } } OpenActiveX(); </script></body></html>BrowserPrinter.aspx.vbPartial Class BrowserPrinter Inherits System.Web.UI.PageEnd ClassWhat else am I missing? 解决方案 Your C# code implements a COM object but your HTML code is requiring an ActiveX control. A proper ActiveX control require the implementation of several other interfaces.I don't think you require an ActiveX control. Simply change your HTML code to something like:var BrowserPrinter = new ActiveXObject("BrowserPrinter"); //ProgID is the parameterBrowserPrinter.PrintDocument();and leave out the <div>...</div> 这篇关于如何创建简单的ActiveX控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-12 00:06