Beispiel #1
0
        internal override void LoadPrinterSettings(string printer, PrinterSettings settings)
        {
            int     ret;
            DEVMODE devmode;
            IntPtr  hPrn    = IntPtr.Zero;
            IntPtr  ptr_dev = IntPtr.Zero;

            settings.maximum_copies = Win32DeviceCapabilities(printer, null, DCCapabilities.DC_COPIES, IntPtr.Zero, IntPtr.Zero);

            ret = Win32DeviceCapabilities(printer, null, DCCapabilities.DC_DUPLEX, IntPtr.Zero, IntPtr.Zero);
            settings.can_duplex = (ret == 1) ? true : false;

            ret = Win32DeviceCapabilities(printer, null, DCCapabilities.DC_COLORDEVICE, IntPtr.Zero, IntPtr.Zero);
            settings.supports_color = (ret == 1) ? true : false;

            ret = Win32DeviceCapabilities(printer, null, DCCapabilities.DC_ORIENTATION, IntPtr.Zero, IntPtr.Zero);
            if (ret != -1)
            {
                settings.landscape_angle = ret;
            }

            IntPtr dc = IntPtr.Zero;

            dc  = Win32CreateIC(null, printer, null, IntPtr.Zero /* DEVMODE */);
            ret = Win32GetDeviceCaps(dc, (int)DevCapabilities.TECHNOLOGY);
            settings.is_plotter = ret == (int)PrinterType.DT_PLOTTER;
            Win32DeleteDC(dc);

            try {
                Win32OpenPrinter(printer, out hPrn, IntPtr.Zero);
                ret = Win32DocumentProperties(IntPtr.Zero, hPrn, null, IntPtr.Zero, IntPtr.Zero, 0);

                if (ret < 0)
                {
                    return;
                }

                ptr_dev = Marshal.AllocHGlobal(ret);
                ret     = Win32DocumentProperties(IntPtr.Zero, hPrn, null, ptr_dev, IntPtr.Zero, 2);

                devmode = (DEVMODE)Marshal.PtrToStructure(ptr_dev, typeof(DEVMODE));

                LoadPrinterPaperSizes(printer, settings);
                foreach (PaperSize paper_size in settings.PaperSizes)
                {
                    if ((int)paper_size.Kind == devmode.dmPaperSize)
                    {
                        settings.DefaultPageSettings.PaperSize = paper_size;
                        break;
                    }
                }

                LoadPrinterPaperSources(printer, settings);
                foreach (PaperSource paper_source in settings.PaperSources)
                {
                    if ((int)paper_source.Kind == devmode.dmDefaultSource)
                    {
                        settings.DefaultPageSettings.PaperSource = paper_source;
                        break;
                    }
                }
            }
            finally {
                Win32ClosePrinter(hPrn);

                if (ptr_dev != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(ptr_dev);
                }
            }
        }
Beispiel #2
0
        void LoadPrinterPaperSizes(string printer, PrinterSettings settings)
        {
            int    items, ret;
            IntPtr ptr_names, buff_names = IntPtr.Zero;
            IntPtr ptr_sizes, buff_sizes = IntPtr.Zero;
            IntPtr ptr_sizes_enum, buff_sizes_enum = IntPtr.Zero;
            string name;

            if (settings.PaperSizes == null)
            {
                settings.paper_sizes = new PrinterSettings.PaperSizeCollection(new PaperSize [0]);
            }
            else
            {
                settings.PaperSizes.Clear();
            }

            items = Win32DeviceCapabilities(printer, null, DCCapabilities.DC_PAPERSIZE, IntPtr.Zero, IntPtr.Zero);

            if (items == -1)
            {
                return;
            }

            try {
                ptr_sizes      = buff_sizes = Marshal.AllocHGlobal(items * 2 * 4);
                ptr_names      = buff_names = Marshal.AllocHGlobal(items * 64 * 2);
                ptr_sizes_enum = buff_sizes_enum = Marshal.AllocHGlobal(items * 2);
                ret            = Win32DeviceCapabilities(printer, null, DCCapabilities.DC_PAPERSIZE, buff_sizes, IntPtr.Zero);

                if (ret == -1)
                {
                    // the finally clause will free the unmanaged memory before returning
                    return;
                }

                ret = Win32DeviceCapabilities(printer, null, DCCapabilities.DC_PAPERS, buff_sizes_enum, IntPtr.Zero);
                ret = Win32DeviceCapabilities(printer, null, DCCapabilities.DC_PAPERNAMES, buff_names, IntPtr.Zero);

                int       x, y;
                PaperSize ps;
                PaperKind kind;
                for (int i = 0; i < ret; i++)
                {
                    x = Marshal.ReadInt32(ptr_sizes, i * 8);
                    y = Marshal.ReadInt32(ptr_sizes, (i * 8) + 4);

                    x = PrinterUnitConvert.Convert(x, PrinterUnit.TenthsOfAMillimeter,
                                                   PrinterUnit.Display);

                    y = PrinterUnitConvert.Convert(y, PrinterUnit.TenthsOfAMillimeter,
                                                   PrinterUnit.Display);

                    name      = Marshal.PtrToStringUni(ptr_names);
                    ptr_names = new IntPtr(ptr_names.ToInt64() + 64 * 2);

                    kind           = (PaperKind)Marshal.ReadInt16(ptr_sizes_enum);
                    ptr_sizes_enum = new IntPtr(ptr_sizes_enum.ToInt64() + 2);

                    ps = new PaperSize(name, x, y);
                    ps.SetKind(kind);
                    settings.PaperSizes.Add(ps);
                }
            }
            finally {
                if (buff_names != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(buff_names);
                }
                if (buff_sizes != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(buff_sizes);
                }
                if (buff_sizes_enum != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(buff_sizes_enum);
                }
            }
        }