/// <include file='doc\PreviewPrintController.uex' path='docs/doc[@for="PreviewPrintController.OnStartPage"]/*' />
        /// <devdoc>
        ///    <para>
        ///       Implements StartEnd for generating print preview information.
        ///    </para>
        /// </devdoc>
        public override Graphics OnStartPage(PrintDocument document, PrintPageEventArgs e)
        {
            Debug.Assert(dc != IntPtr.Zero && graphics == null, "PrintController methods called in the wrong order?");

            // For security purposes, don't assume our public methods methods are called in any particular order
            CheckSecurity();

            base.OnStartPage(document, e);

            IntSecurity.AllPrintingAndUnmanagedCode.Assert();

            e.PageSettings.CopyToHdevmode(modeHandle);
            Size size = e.PageBounds.Size;

            // Metafile framing rectangles apparently use hundredths of mm as their unit of measurement,
            // instead of the GDI+ standard hundredth of an inch.
            Size     metafileSize = PrinterUnitConvert.Convert(size, PrinterUnit.Display, PrinterUnit.HundredthsOfAMillimeter);
            Metafile metafile     = new Metafile(dc, new Rectangle(0, 0, metafileSize.Width, metafileSize.Height));

            PreviewPageInfo info = new PreviewPageInfo(metafile, size);

            list.Add(info);
            graphics = Graphics.FromImage(metafile);

            if (antiAlias)
            {
                graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
                graphics.SmoothingMode     = SmoothingMode.AntiAlias;
            }

            return(graphics);
        }
Beispiel #2
0
        /// <summary>
        /// Implements StartEnd for generating print preview information.
        /// </summary>
        public override Graphics OnStartPage(PrintDocument document, PrintPageEventArgs e)
        {
            base.OnStartPage(document, e);

            if (e.CopySettingsToDevMode)
            {
                e.PageSettings.CopyToHdevmode(_modeHandle);
            }

            Size size = e.PageBounds.Size;

            // Metafile framing rectangles apparently use hundredths of mm as their unit of measurement,
            // instead of the GDI+ standard hundredth of an inch.
            Size metafileSize = PrinterUnitConvert.Convert(size, PrinterUnit.Display, PrinterUnit.HundredthsOfAMillimeter);

            // Create a Metafile which accepts only GDI+ commands since we are the ones creating
            // and using this ...
            // Framework creates a dual-mode EMF for each page in the preview.
            // When these images are displayed in preview,
            // they are added to the dual-mode EMF. However,
            // GDI+ breaks during this process if the image
            // is sufficiently large and has more than 254 colors.
            // This code path can easily be avoided by requesting
            // an EmfPlusOnly EMF..
            Metafile metafile = new Metafile(_dc.Hdc, new Rectangle(0, 0, metafileSize.Width, metafileSize.Height), MetafileFrameUnit.GdiCompatible, EmfType.EmfPlusOnly);

            PreviewPageInfo info = new PreviewPageInfo(metafile, size);

            _list.Add(info);
            PrintPreviewGraphics printGraphics = new PrintPreviewGraphics(document, e);

            _graphics = Graphics.FromImage(metafile);

            if (_graphics != null && document.OriginAtMargins)
            {
                // Adjust the origin of the graphics object to be at the
                // user-specified margin location
                int   dpiX           = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(_dc, _dc.Hdc), SafeNativeMethods.LOGPIXELSX);
                int   dpiY           = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(_dc, _dc.Hdc), SafeNativeMethods.LOGPIXELSY);
                int   hardMarginX_DU = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(_dc, _dc.Hdc), SafeNativeMethods.PHYSICALOFFSETX);
                int   hardMarginY_DU = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(_dc, _dc.Hdc), SafeNativeMethods.PHYSICALOFFSETY);
                float hardMarginX    = hardMarginX_DU * 100 / dpiX;
                float hardMarginY    = hardMarginY_DU * 100 / dpiY;

                _graphics.TranslateTransform(-hardMarginX, -hardMarginY);
                _graphics.TranslateTransform(document.DefaultPageSettings.Margins.Left, document.DefaultPageSettings.Margins.Top);
            }

            _graphics.PrintingHelper = printGraphics;

            if (UseAntiAlias)
            {
                _graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
                _graphics.SmoothingMode     = SmoothingMode.AntiAlias;
            }
            return(_graphics);
        }
Beispiel #3
0
 private System.Drawing.Printing.PaperSize PaperSizeFromMode(SafeNativeMethods.DEVMODE mode)
 {
     System.Drawing.Printing.PaperSize[] sizeArray = this.printerSettings.Get_PaperSizes();
     if ((mode.dmFields & 2) == 2)
     {
         for (int i = 0; i < sizeArray.Length; i++)
         {
             if (sizeArray[i].RawKind == mode.dmPaperSize)
             {
                 return(sizeArray[i]);
             }
         }
     }
     return(new System.Drawing.Printing.PaperSize(PaperKind.Custom, "custom", PrinterUnitConvert.Convert((int)mode.dmPaperWidth, PrinterUnit.TenthsOfAMillimeter, PrinterUnit.Display), PrinterUnitConvert.Convert((int)mode.dmPaperLength, PrinterUnit.TenthsOfAMillimeter, PrinterUnit.Display)));
 }
 private PaperSize PaperSizeFromMode(SafeNativeMethods.DEVMODE mode)
 {
     PaperSize[] sizes = printerSettings.Get_PaperSizes();
     if ((mode.dmFields & SafeNativeMethods.DM_PAPERSIZE) == SafeNativeMethods.DM_PAPERSIZE)
     {
         for (int i = 0; i < sizes.Length; i++)
         {
             if ((int)sizes[i].RawKind == mode.dmPaperSize)
             {
                 return(sizes[i]);
             }
         }
     }
     return(new PaperSize(PaperKind.Custom, "custom",
                          //mode.dmPaperWidth, mode.dmPaperLength);
                          PrinterUnitConvert.Convert(mode.dmPaperWidth, PrinterUnit.TenthsOfAMillimeter, PrinterUnit.Display),
                          PrinterUnitConvert.Convert(mode.dmPaperLength, PrinterUnit.TenthsOfAMillimeter, PrinterUnit.Display)));
 }
 public override Graphics OnStartPage(PrintDocument document, PrintPageEventArgs e)
 {
     this.CheckSecurity();
     base.OnStartPage(document, e);
     try
     {
         IntSecurity.AllPrintingAndUnmanagedCode.Assert();
         e.PageSettings.CopyToHdevmode((IntPtr)base.modeHandle);
         Size            size  = e.PageBounds.Size;
         Size            size2 = PrinterUnitConvert.Convert(size, PrinterUnit.Display, PrinterUnit.HundredthsOfAMillimeter);
         Metafile        image = new Metafile(this.dc.Hdc, new Rectangle(0, 0, size2.Width, size2.Height), MetafileFrameUnit.GdiCompatible, EmfType.EmfPlusOnly);
         PreviewPageInfo info  = new PreviewPageInfo(image, size);
         this.list.Add(info);
         PrintPreviewGraphics graphics = new PrintPreviewGraphics(document, e);
         this.graphics = Graphics.FromImage(image);
         if ((this.graphics != null) && document.OriginAtMargins)
         {
             int   deviceCaps = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(this.dc, this.dc.Hdc), 0x58);
             int   num2       = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(this.dc, this.dc.Hdc), 90);
             int   num3       = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(this.dc, this.dc.Hdc), 0x70);
             int   num4       = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(this.dc, this.dc.Hdc), 0x71);
             float num5       = (num3 * 100) / deviceCaps;
             float num6       = (num4 * 100) / num2;
             this.graphics.TranslateTransform(-num5, -num6);
             this.graphics.TranslateTransform((float)document.DefaultPageSettings.Margins.Left, (float)document.DefaultPageSettings.Margins.Top);
         }
         this.graphics.PrintingHelper = graphics;
         if (this.antiAlias)
         {
             this.graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
             this.graphics.SmoothingMode     = SmoothingMode.AntiAlias;
         }
     }
     finally
     {
         CodeAccessPermission.RevertAssert();
     }
     return(this.graphics);
 }
        /// <summary>
        /// Copies the relevant information out of the PageSettings and into the handle.
        /// </summary>
        public void CopyToHdevmode(IntPtr hdevmode)
        {
            IntPtr modePointer = Interop.Kernel32.GlobalLock(hdevmode);

            SafeNativeMethods.DEVMODE mode = (SafeNativeMethods.DEVMODE)Marshal.PtrToStructure(modePointer, typeof(SafeNativeMethods.DEVMODE));

            if (_color.IsNotDefault && ((mode.dmFields & SafeNativeMethods.DM_COLOR) == SafeNativeMethods.DM_COLOR))
            {
                mode.dmColor = unchecked ((short)(((bool)_color) ? SafeNativeMethods.DMCOLOR_COLOR : SafeNativeMethods.DMCOLOR_MONOCHROME));
            }
            if (_landscape.IsNotDefault && ((mode.dmFields & SafeNativeMethods.DM_ORIENTATION) == SafeNativeMethods.DM_ORIENTATION))
            {
                mode.dmOrientation = unchecked ((short)(((bool)_landscape) ? SafeNativeMethods.DMORIENT_LANDSCAPE : SafeNativeMethods.DMORIENT_PORTRAIT));
            }

            if (_paperSize != null)
            {
                if ((mode.dmFields & SafeNativeMethods.DM_PAPERSIZE) == SafeNativeMethods.DM_PAPERSIZE)
                {
                    mode.dmPaperSize = unchecked ((short)_paperSize.RawKind);
                }

                bool setWidth  = false;
                bool setLength = false;

                if ((mode.dmFields & SafeNativeMethods.DM_PAPERLENGTH) == SafeNativeMethods.DM_PAPERLENGTH)
                {
                    // dmPaperLength is always in tenths of millimeter but paperSizes are in hundredth of inch ..
                    // so we need to convert :: use PrinterUnitConvert.Convert(value, PrinterUnit.TenthsOfAMillimeter /*fromUnit*/, PrinterUnit.Display /*ToUnit*/)
                    int length = PrinterUnitConvert.Convert(_paperSize.Height, PrinterUnit.Display, PrinterUnit.TenthsOfAMillimeter);
                    mode.dmPaperLength = unchecked ((short)length);
                    setLength          = true;
                }
                if ((mode.dmFields & SafeNativeMethods.DM_PAPERWIDTH) == SafeNativeMethods.DM_PAPERWIDTH)
                {
                    int width = PrinterUnitConvert.Convert(_paperSize.Width, PrinterUnit.Display, PrinterUnit.TenthsOfAMillimeter);
                    mode.dmPaperWidth = unchecked ((short)width);
                    setWidth          = true;
                }

                if (_paperSize.Kind == PaperKind.Custom)
                {
                    if (!setLength)
                    {
                        mode.dmFields |= SafeNativeMethods.DM_PAPERLENGTH;
                        int length = PrinterUnitConvert.Convert(_paperSize.Height, PrinterUnit.Display, PrinterUnit.TenthsOfAMillimeter);
                        mode.dmPaperLength = unchecked ((short)length);
                    }
                    if (!setWidth)
                    {
                        mode.dmFields |= SafeNativeMethods.DM_PAPERWIDTH;
                        int width = PrinterUnitConvert.Convert(_paperSize.Width, PrinterUnit.Display, PrinterUnit.TenthsOfAMillimeter);
                        mode.dmPaperWidth = unchecked ((short)width);
                    }
                }
            }

            if (_paperSource != null && ((mode.dmFields & SafeNativeMethods.DM_DEFAULTSOURCE) == SafeNativeMethods.DM_DEFAULTSOURCE))
            {
                mode.dmDefaultSource = unchecked ((short)_paperSource.RawKind);
            }

            if (_printerResolution != null)
            {
                if (_printerResolution.Kind == PrinterResolutionKind.Custom)
                {
                    if ((mode.dmFields & SafeNativeMethods.DM_PRINTQUALITY) == SafeNativeMethods.DM_PRINTQUALITY)
                    {
                        mode.dmPrintQuality = unchecked ((short)_printerResolution.X);
                    }
                    if ((mode.dmFields & SafeNativeMethods.DM_YRESOLUTION) == SafeNativeMethods.DM_YRESOLUTION)
                    {
                        mode.dmYResolution = unchecked ((short)_printerResolution.Y);
                    }
                }
                else
                {
                    if ((mode.dmFields & SafeNativeMethods.DM_PRINTQUALITY) == SafeNativeMethods.DM_PRINTQUALITY)
                    {
                        mode.dmPrintQuality = unchecked ((short)_printerResolution.Kind);
                    }
                }
            }

            Marshal.StructureToPtr(mode, modePointer, false);

            // It's possible this page has a DEVMODE for a different printer than the DEVMODE passed in here
            // (Ex: occurs when Doc.DefaultPageSettings.PrinterSettings.PrinterName != Doc.PrinterSettings.PrinterName)
            //
            // if the passed in devmode has fewer bytes than our buffer for the extrainfo, we want to skip the merge as it will cause
            // a buffer overrun
            if (mode.dmDriverExtra >= ExtraBytes)
            {
                int retCode = SafeNativeMethods.DocumentProperties(NativeMethods.NullHandleRef, NativeMethods.NullHandleRef, printerSettings.PrinterName, modePointer, modePointer, SafeNativeMethods.DM_IN_BUFFER | SafeNativeMethods.DM_OUT_BUFFER);
                if (retCode < 0)
                {
                    Interop.Kernel32.GlobalFree(modePointer);
                }
            }

            Interop.Kernel32.GlobalUnlock(hdevmode);
        }
        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.PaperSizes = 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);
                }
            }
        }
Beispiel #8
0
        public void CopyToHdevmode(IntPtr hdevmode)
        {
            IntSecurity.AllPrintingAndUnmanagedCode.Demand();
            IntPtr lparam = SafeNativeMethods.GlobalLock(new HandleRef(null, hdevmode));

            SafeNativeMethods.DEVMODE structure = (SafeNativeMethods.DEVMODE)UnsafeNativeMethods.PtrToStructure(lparam, typeof(SafeNativeMethods.DEVMODE));
            if (this.color.IsNotDefault && ((structure.dmFields & 0x800) == 0x800))
            {
                structure.dmColor = ((bool)this.color) ? ((short)2) : ((short)1);
            }
            if (this.landscape.IsNotDefault && ((structure.dmFields & 1) == 1))
            {
                structure.dmOrientation = ((bool)this.landscape) ? ((short)2) : ((short)1);
            }
            if (this.paperSize != null)
            {
                if ((structure.dmFields & 2) == 2)
                {
                    structure.dmPaperSize = (short)this.paperSize.RawKind;
                }
                bool flag  = false;
                bool flag2 = false;
                if ((structure.dmFields & 4) == 4)
                {
                    structure.dmPaperLength = (short)PrinterUnitConvert.Convert(this.paperSize.Height, PrinterUnit.Display, PrinterUnit.TenthsOfAMillimeter);
                    flag2 = true;
                }
                if ((structure.dmFields & 8) == 8)
                {
                    structure.dmPaperWidth = (short)PrinterUnitConvert.Convert(this.paperSize.Width, PrinterUnit.Display, PrinterUnit.TenthsOfAMillimeter);
                    flag = true;
                }
                if (this.paperSize.Kind == PaperKind.Custom)
                {
                    if (!flag2)
                    {
                        structure.dmFields     |= 4;
                        structure.dmPaperLength = (short)PrinterUnitConvert.Convert(this.paperSize.Height, PrinterUnit.Display, PrinterUnit.TenthsOfAMillimeter);
                    }
                    if (!flag)
                    {
                        structure.dmFields    |= 8;
                        structure.dmPaperWidth = (short)PrinterUnitConvert.Convert(this.paperSize.Width, PrinterUnit.Display, PrinterUnit.TenthsOfAMillimeter);
                    }
                }
            }
            if ((this.paperSource != null) && ((structure.dmFields & 0x200) == 0x200))
            {
                structure.dmDefaultSource = (short)this.paperSource.RawKind;
            }
            if (this.printerResolution != null)
            {
                if (this.printerResolution.Kind == PrinterResolutionKind.Custom)
                {
                    if ((structure.dmFields & 0x400) == 0x400)
                    {
                        structure.dmPrintQuality = (short)this.printerResolution.X;
                    }
                    if ((structure.dmFields & 0x2000) == 0x2000)
                    {
                        structure.dmYResolution = (short)this.printerResolution.Y;
                    }
                }
                else if ((structure.dmFields & 0x400) == 0x400)
                {
                    structure.dmPrintQuality = (short)this.printerResolution.Kind;
                }
            }
            Marshal.StructureToPtr(structure, lparam, false);
            if ((structure.dmDriverExtra >= this.ExtraBytes) && (SafeNativeMethods.DocumentProperties(System.Drawing.NativeMethods.NullHandleRef, System.Drawing.NativeMethods.NullHandleRef, this.printerSettings.PrinterName, lparam, lparam, 10) < 0))
            {
                SafeNativeMethods.GlobalFree(new HandleRef(null, lparam));
            }
            SafeNativeMethods.GlobalUnlock(new HandleRef(null, hdevmode));
        }