Beispiel #1
0
        //public static void SetPrinterDevMode(string pPrinterName)
        //{

        //}

        public static void ChangePrinterPaper(string pPrinterName, string pPaperName)
        {
            PrinterDefaults pDefault = null;

            pDefault = new PrinterDefaults()
            {
                DesiredAccess = (int)PrintAccessRights.PrinterGenericExecute
            };

            var hPrinter = IntPtr.Zero;

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

                int    level;
                object info;
                var    pDevMode = IntPtr.Zero;

                if (pDevMode == IntPtr.Zero)
                {
                    /* Per-User DevMode:
                     *
                     * 本地打印设备:
                     * HKEY_CURRENT_USER\Printers\DevModes2
                     * "{PrinterName}"
                     *
                     * 远程打印设备:
                     * HKEY_CURRENT_USER\Printers\DevModes2\
                     * "\\{MachineName}\{PrinterName}"
                     *
                     * HKEY_CURRENT_USER\Printers\Connections\",,{MachineName},{PrinterName}"\
                     * "DevMode"
                     */
                    level = 9; // Per-User DEVMODE
                    info  = Native.ThunkGetPrinter(hPrinter, level);
                    if (info != null)
                    {
                        // 此处只有修改过 "打印首选项" 才会有值;
                        pDevMode = ((PrinterInfo9)info).pDevMode;
                    }
                }

                if (pDevMode == IntPtr.Zero)
                {
                    /* Global DevMode:
                     *
                     * 本地打印设备:
                     * HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers\{PrinterName}
                     * Default DevMode
                     *
                     * 远程打印设备:
                     * HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Providers\Client Side Rendering Print Provider\Servers\{MachineName}\Printers\{PrinterGuid}
                     * Default DevMode
                     */
                    level = 8; // Global DEVMODE
                    info  = Native.ThunkGetPrinter(hPrinter, level);
                    if (info != null)
                    {
                        pDevMode = ((PrinterInfo8)info).pDevMode;
                    }
                }

                if (pDevMode != IntPtr.Zero)
                {
                    var dm = (DevMode)Marshal.PtrToStructure(pDevMode, typeof(DevMode));

                    // var size = Marshal.SizeOf(typeof(DevMode));

                    //foreach (var f in typeof(DevMode).GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public))
                    //{
                    //    Debug.WriteLine("{0}: {1}", f.Name, f.GetValue(dm));
                    //}
                    //Debug.WriteLine(string.Empty);
                    //Debug.WriteLine(string.Empty);

                    var papers = GetPrinterPapers(pPrinterName);
                    var paper  = papers.Where(p => p.PaperName == pPaperName).FirstOrDefault();
                    if (paper != null)
                    {
                        dm.dmPaperSize = (short)paper.RawKind;
                        //dm.dmPaperLength = (short)PrintSystemUnitConvert.Convert(paper.Height, PrintSystemUnit.HundredthsOfInch, PrintSystemUnit.TenthsOfMillimeter);
                        //dm.dmPaperWidth = (short)PrintSystemUnitConvert.Convert(paper.Width, PrintSystemUnit.HundredthsOfInch, PrintSystemUnit.TenthsOfMillimeter);

                        var info9 = new PrinterInfo9();
                        info9.pDevMode = pDevMode;

                        Marshal.StructureToPtr(dm, pDevMode, true);

                        Native.ThunkSetPrinter(hPrinter, info9);
                    }
                }
            }
            finally
            {
                if (hPrinter != IntPtr.Zero)
                {
                    Native.ClosePrinter(hPrinter);
                    hPrinter = IntPtr.Zero;
                }
            }
        }
Beispiel #2
0
 internal static extern bool OpenPrinter2([MarshalAs(UnmanagedType.LPTStr)] string pPrinterName, out IntPtr phPrinter, PrinterDefaults pDefault, PrinterOptions pOptions);
Beispiel #3
0
        public static void GetPrinterDevMode(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 };

            var hPrinter = IntPtr.Zero;

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

                int    level;
                object info;
                var    pDevMode = IntPtr.Zero;

                if (pDevMode == IntPtr.Zero)
                {
                    /* Per-User DevMode:
                     *
                     * 本地打印设备:
                     * HKEY_CURRENT_USER\Printers\DevModes2
                     * "{PrinterName}"
                     *
                     * 远程打印设备:
                     * HKEY_CURRENT_USER\Printers\DevModes2\
                     * "\\{MachineName}\{PrinterName}"
                     *
                     * HKEY_CURRENT_USER\Printers\Connections\",,{MachineName},{PrinterName}"\
                     * "DevMode"
                     */
                    level = 9; // Per-User DEVMODE
                    info  = Native.ThunkGetPrinter(hPrinter, level);
                    if (info != null)
                    {
                        // 此处只有修改过 "打印首选项" 才会有值;
                        pDevMode = ((PrinterInfo9)info).pDevMode;
                    }
                }

                if (pDevMode == IntPtr.Zero)
                {
                    /* Global DevMode:
                     *
                     * 本地打印设备:
                     * HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers\{PrinterName}
                     * Default DevMode
                     *
                     * 远程打印设备:
                     * HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Providers\Client Side Rendering Print Provider\Servers\{MachineName}\Printers\{PrinterGuid}
                     * Default DevMode
                     */
                    level = 8; // Global DEVMODE
                    info  = Native.ThunkGetPrinter(hPrinter, level);
                    if (info != null)
                    {
                        pDevMode = ((PrinterInfo8)info).pDevMode;
                    }
                }

                if (pDevMode != IntPtr.Zero)
                {
                    var dm = (DevMode)Marshal.PtrToStructure(pDevMode, typeof(DevMode));

                    // var size = Marshal.SizeOf(typeof(DevMode));

                    foreach (var f in typeof(DevMode).GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public))
                    {
                        Debug.WriteLine("{0}: {1}", f.Name, f.GetValue(dm));
                    }
                    Debug.WriteLine(string.Empty);
                    Debug.WriteLine(string.Empty);
                }
            }
            finally
            {
                if (hPrinter != IntPtr.Zero)
                {
                    Native.ClosePrinter(hPrinter);
                    hPrinter = IntPtr.Zero;
                }
            }
        }