/// <summary> /// Implements EndPrint for generating print preview information. /// </summary> public override void OnEndPrint(PrintDocument document, PrintEventArgs e) { if (_dc != null) { _dc.Dispose(); _dc = null; } base.OnEndPrint(document, e); }
/// <summary> /// Implements StartPrint for generating print preview information. /// </summary> public override void OnStartPrint(PrintDocument document, PrintEventArgs e) { base.OnStartPrint(document, e); if (!document.PrinterSettings.IsValid) { throw new InvalidPrinterException(document.PrinterSettings); } // 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 !); }
public override bool Equals(object?obj) { DeviceContext?other = obj as DeviceContext; if (other == this) { return(true); } if (other == null) { return(false); } // Note: Use property instead of field so the HDC is initialized. Also, this avoid serialization issues (the obj could be a proxy that does not have access to private fields). return(other.Hdc == Hdc); }
/// <summary> /// Implements StartPrint for printing to a physical printer. /// </summary> public override void OnStartPrint(PrintDocument document, PrintEventArgs e) { Debug.Assert(_dc == null && _graphics == null, "PrintController methods called in the wrong order?"); Debug.Assert(_modeHandle != null); 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); Interop.Gdi32.DOCINFO info = new Interop.Gdi32.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 = Interop.Gdi32.StartDoc(new HandleRef(_dc, _dc.Hdc), info); if (result <= 0) { int error = Marshal.GetLastPInvokeError(); if (error == SafeNativeMethods.ERROR_CANCELLED) { e.Cancel = true; } else { throw new Win32Exception(error); } } }
/// <summary> /// Implements EndPrint for printing to a physical printer. /// </summary> public override void OnEndPrint(PrintDocument document, PrintEventArgs e) { Debug.Assert(_dc != null && _graphics == null, "PrintController methods called in the wrong order?"); if (_dc != null) { try { int result = (e.Cancel) ? Interop.Gdi32.AbortDoc(new HandleRef(_dc, _dc.Hdc)) : Interop.Gdi32.EndDoc(new HandleRef(_dc, _dc.Hdc)); if (result <= 0) { throw new Win32Exception(); } } finally { _dc.Dispose(); _dc = null; } } base.OnEndPrint(document, e); }