Example #1
0
        public override void OnStartPrint(PrintDocument document, PrintEventArgs e) {
            Debug.Assert(dc == null && 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.OnStartPrint(document, e);

            
            try {
                
                if (!document.PrinterSettings.IsValid)
                throw new InvalidPrinterException(document.PrinterSettings);

                IntSecurity.AllPrintingAndUnmanagedCode.Assert();
                
                // We need a DC as a reference; we don't actually draw on it.
                // We make sure to reuse the same one to improve performance.
                dc = document.PrinterSettings.CreateInformationContext(modeHandle);
            }
            finally {
                CodeAccessPermission.RevertAssert();
            }
            
        }
 internal static void RemoveDeviceContext(DeviceContext dc)
 {
     if (activeDeviceContexts != null)
     {
         activeDeviceContexts.RemoveByHashCode(dc);
     }
 }
Example #3
0
        public override void OnStartPrint(PrintDocument document, PrintEventArgs e) {
            Debug.Assert(dc == null && 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(document);

            base.OnStartPrint(document, e);
            // the win32 methods below SuppressUnmanagedCodeAttributes so assertin on UnmanagedCodePermission is redundant
            if (!document.PrinterSettings.IsValid)
                throw new InvalidPrinterException(document.PrinterSettings);

            dc = document.PrinterSettings.CreateDeviceContext(modeHandle);
            SafeNativeMethods.DOCINFO info = new SafeNativeMethods.DOCINFO();
            info.lpszDocName = document.DocumentName;
            if (document.PrinterSettings.PrintToFile)
                info.lpszOutput = document.PrinterSettings.OutputPort; //This will be "FILE:"
            else
                info.lpszOutput = null;
            info.lpszDatatype = null;
            info.fwType = 0;

            int result = SafeNativeMethods.StartDoc(new HandleRef(this.dc, dc.Hdc), info);
            if (result <= 0) {
                int error = Marshal.GetLastWin32Error();
                if (error == SafeNativeMethods.ERROR_CANCELLED) {
                    e.Cancel = true;
                }
                else {
                    throw new Win32Exception(error);
                }
            }
        }
 public override void OnEndPrint(PrintDocument document, PrintEventArgs e)
 {
     this.CheckSecurity(document);
     System.Drawing.IntSecurity.UnmanagedCode.Assert();
     try
     {
         if (this.dc != null)
         {
             try
             {
                 int num = e.Cancel ? SafeNativeMethods.AbortDoc(new HandleRef(this.dc, this.dc.Hdc)) : SafeNativeMethods.EndDoc(new HandleRef(this.dc, this.dc.Hdc));
                 if (num <= 0)
                 {
                     throw new Win32Exception();
                 }
             }
             finally
             {
                 this.dc.Dispose();
                 this.dc = null;
             }
         }
     }
     finally
     {
         CodeAccessPermission.RevertAssert();
     }
     base.OnEndPrint(document, e);
 }
 public override void OnEndPrint(PrintDocument document, PrintEventArgs e)
 {
     this.CheckSecurity();
     this.dc.Dispose();
     this.dc = null;
     base.OnEndPrint(document, e);
 }
 internal static void AddDeviceContext(DeviceContext dc)
 {
     if (activeDeviceContexts == null)
     {
         activeDeviceContexts = new System.Drawing.ClientUtils.WeakRefCollection();
         activeDeviceContexts.RefCheckThreshold = 20;
     }
     if (!activeDeviceContexts.Contains(dc))
     {
         dc.Disposing += new EventHandler(DeviceContexts.OnDcDisposing);
         activeDeviceContexts.Add(dc);
     }
 }
Example #7
0
        /// <include file='doc\PreviewPrintController.uex' path='docs/doc[@for="PreviewPrintController.OnEndPrint"]/*' />
        /// <devdoc>
        ///    <para>
        ///       Implements EndPrint for generating print preview information.
        ///    </para>
        /// </devdoc>
        public override void OnEndPrint(PrintDocument document, PrintEventArgs e) {
            Debug.Assert(dc != null && graphics == null, "PrintController methods called in the wrong order?");

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

            dc.Dispose();
            dc = null;

            base.OnEndPrint(document, e);
        }
Example #8
0
        public override void OnEndPrint(PrintDocument document, PrintEventArgs e) {
            Debug.Assert(dc != null && 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(document);
            IntSecurity.UnmanagedCode.Assert();

            try 
            {
                if (dc != null) {
                    try {
                        int result = (e.Cancel) ? SafeNativeMethods.AbortDoc(new HandleRef(dc, dc.Hdc)) : SafeNativeMethods.EndDoc(new HandleRef(dc, dc.Hdc));
                        if (result <= 0)
                            throw new Win32Exception();
                    }
                    finally {
                        dc.Dispose();
                        dc = null;
                    }
                }
            }
            finally {
                CodeAccessPermission.RevertAssert();
            }
            base.OnEndPrint(document, e);
        }
 public override void OnStartPrint(PrintDocument document, PrintEventArgs e)
 {
     this.CheckSecurity(document);
     base.OnStartPrint(document, e);
     if (!document.PrinterSettings.IsValid)
     {
         throw new InvalidPrinterException(document.PrinterSettings);
     }
     this.dc = document.PrinterSettings.CreateDeviceContext((IntPtr) base.modeHandle);
     SafeNativeMethods.DOCINFO lpDocInfo = new SafeNativeMethods.DOCINFO {
         lpszDocName = document.DocumentName
     };
     if (document.PrinterSettings.PrintToFile)
     {
         lpDocInfo.lpszOutput = document.PrinterSettings.OutputPort;
     }
     else
     {
         lpDocInfo.lpszOutput = null;
     }
     lpDocInfo.lpszDatatype = null;
     lpDocInfo.fwType = 0;
     if (SafeNativeMethods.StartDoc(new HandleRef(this.dc, this.dc.Hdc), lpDocInfo) <= 0)
     {
         int error = Marshal.GetLastWin32Error();
         if (error != 0x4c7)
         {
             throw new Win32Exception(error);
         }
         e.Cancel = true;
     }
 }
 public override void OnStartPrint(PrintDocument document, PrintEventArgs e)
 {
     this.CheckSecurity();
     base.OnStartPrint(document, e);
     try
     {
         if (!document.PrinterSettings.IsValid)
         {
             throw new InvalidPrinterException(document.PrinterSettings);
         }
         IntSecurity.AllPrintingAndUnmanagedCode.Assert();
         this.dc = document.PrinterSettings.CreateInformationContext((IntPtr) base.modeHandle);
     }
     finally
     {
         CodeAccessPermission.RevertAssert();
     }
 }