Exemplo n.º 1
0
        /* 经过测试:
         *
         * a). 同个打印服务器内的所有打印机, 结果全都一样;
         * 例如:
         * GetForms("安装的本地打印机") == GetForms(NULL);
         * GetForms("链接的共享打印机") 结果来自共享的计算机;
         *
         * b). 调用 AddForm 添加表单, 即使指定打印机名, 本机(不含链接)所有支持该尺寸的打印机都会加上;
         *
         * c). 通过系统操作:
         *     安装打印机后, 打开打印机首选项 => 高级 => 纸张规格: 已包含支持的本机自定义的纸张;
         *
         * 结论:
         * 添加纸张针对的是: "打印服务器", 该服务器下的所有打印机, 只要支持该尺寸都会显示在可选的纸张列表里;
         */
        internal static void ThunkAddForm(IntPtr hPrinter, FormInfo1 pForm)
        {
            var level = 1;
            var type  = typeof(FormInfo1);

            var pBuffer = IntPtr.Zero;

            try
            {
                pBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(type));
                Marshal.StructureToPtr(pForm, pBuffer, false /*仅在指针位置存在旧的同类数据时才设置为真*/);

                if (!AddForm(hPrinter, level, pBuffer))
                {
                    Error.ThrowLastError();
                }
            }
            finally
            {
                if (pBuffer != IntPtr.Zero)
                {
                    Marshal.DestroyStructure(pBuffer, type); // 用于销毁结构的引用字段占用的内存;
                    Marshal.FreeHGlobal(pBuffer);
                }
            }
        }
        static void SetForm(string pPrinterName)
        {
            PrinterDefaults pDefault = null;

            if (pPrinterName != null)
            {
                pDefault = new PrinterDefaults()
                {
                    DesiredAccess = (int)PrintAccessRights.PrinterGenericExecute
                }
            }
            ;
            else // String.Empty 将抛异常: 打印机名无效;
            {
                pDefault = new PrinterDefaults()
                {
                    DesiredAccess = (int)PrintAccessRights.ServerGenericRead
                }
            };

            IntPtr hPrinter;

            if (!Native.OpenPrinter(pPrinterName, out hPrinter, pDefault))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            var w2        = (int)(215.9f * 1000);
            var h2        = (int)(279.4f * 1000);
            var pFormName = "Test241/2";
            var formInfo1 = new FormInfo1()
            {
                pName = pFormName,
                Flags = FormInfoFlags.User,
                // Unit: 0.001mm.
                Size = new SIZE(w2, h2),
                // Unit: 0.001mm.
                ImageableArea = new RECT(0, 0, w2, h2),
            };

            Native.ThunkSetForm(hPrinter, pFormName, formInfo1);

            var ps32 = GetPrinterFormsWithLevel1(pPrinterName).Cast <FormInfo2>().Where(p => p.Flags == FormInfoFlags.User).ToArray();
        }
Exemplo n.º 3
0
        internal PrintForm(FormInfo1 form)
        {
            this._Flags = form.Flags;

            this._Name = form.pName;

            this._Unit = PrintSystemUnit.ThousandthsOfMillimeter;

            var size = form.Size;

            this._Width  = size.cx;
            this._Height = size.cy;

            var area = form.ImageableArea;

            this._LeftMargin   = area.left;
            this._RightMargin  = size.cx - area.right;
            this._TopMargin    = area.top;
            this._BottomMargin = size.cy - area.bottom;
        }
Exemplo n.º 4
0
        public static void AddFormOfServer(string pPrinterName, string pFormName, int width, int height)
        {
            PrinterDefaults pDefault = null;

            //if (pPrinterName != null)
            //    pDefault = new PrinterDefaults() { DesiredAccess = (int)PrintAccessRights.PrinterGenericExecute };
            //else // String.Empty 将抛异常: 打印机名无效;
            pDefault = new PrinterDefaults()
            {
                DesiredAccess = (int)PrintAccessRights.ServerGenericRead
            };

            var hPrinter = IntPtr.Zero;

            try
            {
                if (!Native.OpenPrinter(pPrinterName, out hPrinter, pDefault))
                {
                    Error.ThrowLastError();
                }

                var form = new FormInfo1()
                {
                    Flags         = FormInfoFlags.User,
                    pName         = pFormName,
                    Size          = new SIZE(width, height),
                    ImageableArea = new RECT(0, 0, width, height),
                };

                // Native.ThunkSetForm(hPrinter, "Werp214/3", form);

                // Add
                Native.ThunkAddForm(hPrinter, form);
            }
            finally
            {
                Native.ClosePrinter(hPrinter);
                hPrinter = IntPtr.Zero;
            }
        }
Exemplo n.º 5
0
        static void AddOrUpdateForm(IntPtr hPrinter, string pFormName, double width, double height, PrintSystemUnit unit)
        {
            var level = 1;
            var o     = Native.ThunkGetForm(hPrinter, pFormName, level);

            if (o != null)
            {
                var form = (FormInfo1)o;
                if (form.Flags == FormInfoFlags.Builtin)
                {
                    throw new ArgumentException("内建规格不可修改");
                }

                var w = (int)PrintSystemUnitConvert.Convert(width, unit, PrintSystemUnit.ThousandthsOfMillimeter);
                var h = (int)PrintSystemUnitConvert.Convert(height, unit, PrintSystemUnit.ThousandthsOfMillimeter);
                form.Size          = new SIZE(w, h);
                form.ImageableArea = new RECT(0, 0, w, h);

                // Update
                Native.ThunkSetForm(hPrinter, pFormName, form);
            }
            else
            {
                var w    = (int)PrintSystemUnitConvert.Convert(width, unit, PrintSystemUnit.ThousandthsOfMillimeter);
                var h    = (int)PrintSystemUnitConvert.Convert(height, unit, PrintSystemUnit.ThousandthsOfMillimeter);
                var form = new FormInfo1()
                {
                    Flags         = FormInfoFlags.User,
                    pName         = pFormName,
                    Size          = new SIZE(w, h),
                    ImageableArea = new RECT(0, 0, w, h),
                };

                // Add
                Native.ThunkAddForm(hPrinter, form);
            }
        }