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(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 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;
     }
 }