使用C#编写一个用于客户端打印的ActiveX控件(1)

时间:2022-05-19 21:04:44

最近做的一个Web项目要求提供票据的套打,刚开始使用PrintDocument类库在本机上测试可以打印,满心喜欢的庆幸原来打印原来这么简单,最后部署到服务器上才发现,PrintDocument原来只适用于WinForm。无奈,google一下,发现可以用脚本调用ActiveX的方式来实现,恩,这次还是先别太高兴了,以前听说写ActiveX要用VBVC来写,有点麻烦,如果能用c#来写就好了。再次google,发现居然还真的可以哦!但是有个前提,客户端要装有.net FrameWork,这没问题,刚好这个项目就有这样的前提,那么下面,我就把这两天做的结果写写,由于第一次搞这东西,还颇费工夫的。

 

 

首先,解决如何写一个ActiveX的问题。先创建一个”Windows窗体控件库,例如名叫”Print”,类要实现一个接口IObjectSafety,其作用是让IE知道这是个安全的控件;最后的代码如下:代码如下:

using System.ComponentModel;

using System.Drawing;

using System.Data;

using System.Text;

using System.Windows.Forms;

using System.Runtime.InteropServices;

using System.Drawing.Printing;

using System.Xml;

namespace PrintControl

{

    [Guid("F74272E3-E996-4bb3-A6EB-9995029C4A95")]

    public partial class Print : UserControl, IObjectSafety

    {

        public Print()

        {

            InitializeComponent();

        }

        public string PrintBill(string xml)

        {

              return xml;

        }

     //****************************************************************************

        #region

        private const string _IID_IDispatch = "{00020400-0000-0000-C000-000000000046}";

        private const string _IID_IDispatchEx = "{a6ef9860-c720-11d0-9337-00a0c90dcaa9}";

        private const string _IID_IPersistStorage = "{0000010A-0000-0000-C000-000000000046}";

        private const string _IID_IPersistStream = "{00000109-0000-0000-C000-000000000046}";

        private const string _IID_IPersistPropertyBag = "{37D84F60-42CB-11CE-8135-00AA004BB851}";

        private const int INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001;

        private const int INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002;

        private const int S_OK = 0;

        private const int E_FAIL = unchecked((int)0x80004005);

        private const int E_NOINTERFACE = unchecked((int)0x80004002);

        private bool _fSafeForScripting = true;

        private bool _fSafeForInitializing = true;

//使ActiveX控件成为安全的控件

        public int GetInterfaceSafetyOptions(ref Guid riid, ref int pdwSupportedOptions, ref int pdwEnabledOptions)

        {

            int Rslt = E_FAIL;

            string strGUID = riid.ToString("B");

            pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA;

            switch (strGUID)

            {

                case _IID_IDispatch:

                case _IID_IDispatchEx:

                    Rslt = S_OK;

                    pdwEnabledOptions = 0;

                    if (_fSafeForScripting == true)

                        pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER;

                    break;

                case _IID_IPersistStorage:

                case _IID_IPersistStream:

                case _IID_IPersistPropertyBag:

                    Rslt = S_OK;

                    pdwEnabledOptions = 0;

                    if (_fSafeForInitializing == true)

                        pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA;

                    break;

                default:

                    Rslt = E_NOINTERFACE;

                    break;

            }

            return Rslt;

        }

        public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions)

        {

            int Rslt = E_FAIL;

            string strGUID = riid.ToString("B");

            switch (strGUID)

            {

                case _IID_IDispatch:

                case _IID_IDispatchEx:

                    if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_CALLER) && (_fSafeForScripting == true))

                        Rslt = S_OK;

                    break;

                case _IID_IPersistStorage:

                case _IID_IPersistStream:

                case _IID_IPersistPropertyBag:

                    if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_DATA) && (_fSafeForInitializing == true))

                        Rslt = S_OK;

                    break;

                default:

                    Rslt = E_NOINTERFACE;

                    break;

            }

            return Rslt;

        }

        #endregion

}

    [ComImport, GuidAttribute("CB5BDC81-93C1-11CF-8F20-00805F2CD064")]

    [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]

    interface IObjectSafety

    {

        [PreserveSig]

        int GetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] ref int pdwSupportedOptions, [MarshalAs(UnmanagedType.U4)] ref int pdwEnabledOptions);

        [PreserveSig()]

        int SetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] int dwOptionSetMask, [MarshalAs(UnmanagedType.U4)] int dwEnabledOptions);

}

}

     //****************************************************************************

其实包含在//**符号内的代码是ActiveX的相关接口和接口实现的方法,从网上摘下来的,只管贴上去就行了。然后PrintBill这个方法就是到时真正要干活的东西了。写上打印的相关内容,这里就可以使用上PrintDocument类库来实现打印了。代码就不贴了,反正就是做你想做的事。 编译一下,应该可以通过。哦。别忘了,在项目的属性页里有两个地方要设置一下,1是在“生成”里将“为Com互操作性注册”打勾,2是“应用程序”里的“程序集信息”里勾上“使程序集COM可见”。最后再编译一下,OK

 

 

ActiveX控件已经写好了,接着该怎么用呢?编写一个Html,将<object id="Print" classid='clsid:F74272E3-E996-4bb3-A6EB-9995029C4A95'></object>贴上,至于贴在Head还是Body就随意了,这里有个地方要注意了,clsid后面的GUID就是刚才Print类上面的那个GUID,一定是对应的。将刚才编译好的Print.DLL放在跟Html的同一目录里,打开html,相信应该可以看到会有个未标识的ActiveX下载之类的提示。呵,如果看到这个东西,那至少可以证明上面的步骤已经成功了。接下来就是将ActiveX控件注册到客户端的问题了。

 

 

ActiveX注册到客户端,可是费了我好多工夫,按网上说的方法,但都注册不了,IE的安全性设到最低也无效,或许是我哪里没设置对吧,也不花时间去折腾了。直接打包成安装程序,让客户端自行下载去安装得了。效果是差不多的。

 

 

一切都准备好后,就差脚本里一句调用了,很简单,写法是这样:类名.方法(参数),就像调用一般的静态方法那样。这里如果有参数,那么只能是stringint等普通的类型,像泛型就不支持了。所以如果像要比较复杂的参数,那还是用xml字符串传进去,再去解析xml吧。

 

 

 

http://www.cnblogs.com/homer/archive/2005/01/04/86473.html