Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / ndp / fx / src / CommonUI / System / Drawing / Printing / PreviewPrintController.cs / 1 / PreviewPrintController.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Drawing.Printing {
using Microsoft.Win32;
using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Internal;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Runtime.InteropServices;
using CodeAccessPermission = System.Security.CodeAccessPermission;
///
///
/// A PrintController which "prints" to a series of images.
///
public class PreviewPrintController : PrintController {
private IList list = new ArrayList(); // list of PreviewPageInfo
private System.Drawing.Graphics graphics;
private DeviceContext dc;
private bool antiAlias;
private void CheckSecurity() {
IntSecurity.SafePrinting.Demand();
}
///
///
///
/// This is new public property which notifies if this controller is used for PrintPreview.
///
///
public override bool IsPreview {
get {
return true;
}
}
///
///
///
/// Implements StartPrint for generating print preview information.
///
///
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();
}
}
///
///
///
/// Implements StartEnd for generating print preview information.
///
///
public override Graphics OnStartPage(PrintDocument document, PrintPageEventArgs 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.OnStartPage(document, e);
try {
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);
// 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 (antiAlias) {
graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
graphics.SmoothingMode = SmoothingMode.AntiAlias;
}
}
finally {
CodeAccessPermission.RevertAssert();
}
return graphics;
}
///
///
///
/// Implements EndPage for generating print preview information.
///
///
public override void OnEndPage(PrintDocument document, PrintPageEventArgs 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();
graphics.Dispose();
graphics = null;
base.OnEndPage(document, e);
}
///
///
///
/// Implements EndPrint for generating print preview information.
///
///
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);
}
///
///
///
/// The "printout".
///
///
public PreviewPageInfo[] GetPreviewPageInfo() {
// For security purposes, don't assume our public methods methods are called in any particular order
CheckSecurity();
PreviewPageInfo[] temp = new PreviewPageInfo[list.Count];
list.CopyTo(temp, 0);
return temp;
}
///
public virtual bool UseAntiAlias {
get {
return antiAlias;
}
set {
antiAlias = value;
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Drawing.Printing {
using Microsoft.Win32;
using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Internal;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Runtime.InteropServices;
using CodeAccessPermission = System.Security.CodeAccessPermission;
///
///
/// A PrintController which "prints" to a series of images.
///
public class PreviewPrintController : PrintController {
private IList list = new ArrayList(); // list of PreviewPageInfo
private System.Drawing.Graphics graphics;
private DeviceContext dc;
private bool antiAlias;
private void CheckSecurity() {
IntSecurity.SafePrinting.Demand();
}
///
///
///
/// This is new public property which notifies if this controller is used for PrintPreview.
///
///
public override bool IsPreview {
get {
return true;
}
}
///
///
///
/// Implements StartPrint for generating print preview information.
///
///
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();
}
}
///
///
///
/// Implements StartEnd for generating print preview information.
///
///
public override Graphics OnStartPage(PrintDocument document, PrintPageEventArgs 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.OnStartPage(document, e);
try {
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);
// 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 (antiAlias) {
graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
graphics.SmoothingMode = SmoothingMode.AntiAlias;
}
}
finally {
CodeAccessPermission.RevertAssert();
}
return graphics;
}
///
///
///
/// Implements EndPage for generating print preview information.
///
///
public override void OnEndPage(PrintDocument document, PrintPageEventArgs 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();
graphics.Dispose();
graphics = null;
base.OnEndPage(document, e);
}
///
///
///
/// Implements EndPrint for generating print preview information.
///
///
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);
}
///
///
///
/// The "printout".
///
///
public PreviewPageInfo[] GetPreviewPageInfo() {
// For security purposes, don't assume our public methods methods are called in any particular order
CheckSecurity();
PreviewPageInfo[] temp = new PreviewPageInfo[list.Count];
list.CopyTo(temp, 0);
return temp;
}
///
public virtual bool UseAntiAlias {
get {
return antiAlias;
}
set {
antiAlias = value;
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- DataGridViewSortCompareEventArgs.cs
- SimpleHandlerBuildProvider.cs
- MetadataPropertyvalue.cs
- EntityCommand.cs
- DrawingVisualDrawingContext.cs
- StaticTextPointer.cs
- StringFunctions.cs
- webclient.cs
- FigureParaClient.cs
- DataSourceView.cs
- ServiceXNameTypeConverter.cs
- LinqDataSourceInsertEventArgs.cs
- SystemUdpStatistics.cs
- EntityContainerAssociationSet.cs
- LayoutTableCell.cs
- DescendantOverDescendantQuery.cs
- xmlglyphRunInfo.cs
- WsdlBuildProvider.cs
- GraphicsPathIterator.cs
- FieldTemplateFactory.cs
- Win32Native.cs
- Bold.cs
- httpserverutility.cs
- TypeUtils.cs
- XmlReflectionImporter.cs
- XmlDataCollection.cs
- SqlCacheDependencyDatabase.cs
- SHA256Managed.cs
- WebPartManager.cs
- DataGridViewColumnCollectionEditor.cs
- IgnoreDeviceFilterElement.cs
- NumericUpDown.cs
- DataRelation.cs
- CookieProtection.cs
- WebFormDesignerActionService.cs
- DataGridViewCellConverter.cs
- PointAnimationUsingPath.cs
- CheckBoxField.cs
- OdbcConnectionFactory.cs
- EntityConnection.cs
- RootDesignerSerializerAttribute.cs
- CodeConditionStatement.cs
- StaticFileHandler.cs
- AssemblyCache.cs
- DataGridViewAutoSizeColumnModeEventArgs.cs
- MenuAutomationPeer.cs
- ContainerTracking.cs
- ServiceDescriptionImporter.cs
- BamlStream.cs
- CounterSample.cs
- BidOverLoads.cs
- ProtocolElement.cs
- ProfessionalColorTable.cs
- ManipulationStartedEventArgs.cs
- NativeMethods.cs
- StateInitialization.cs
- ZipPackage.cs
- TreeNode.cs
- FaultCode.cs
- TextViewElement.cs
- AssertFilter.cs
- XappLauncher.cs
- Int32AnimationUsingKeyFrames.cs
- Mutex.cs
- GridViewItemAutomationPeer.cs
- MemberInfoSerializationHolder.cs
- FrameAutomationPeer.cs
- EntityDescriptor.cs
- SchemaDeclBase.cs
- NetPeerTcpBinding.cs
- DnsPermission.cs
- DataBoundControlHelper.cs
- PipelineModuleStepContainer.cs
- FixedSOMTableCell.cs
- FixedDocument.cs
- Matrix.cs
- WindowsToolbarAsMenu.cs
- AffineTransform3D.cs
- Soap11ServerProtocol.cs
- HebrewNumber.cs
- HierarchicalDataBoundControl.cs
- Type.cs
- EdmItemError.cs
- SynchronizedReadOnlyCollection.cs
- OptimalTextSource.cs
- BinaryFormatterWriter.cs
- FixedSOMSemanticBox.cs
- XmlNamespaceDeclarationsAttribute.cs
- BitFlagsGenerator.cs
- DataGridCheckBoxColumn.cs
- GotoExpression.cs
- GradientStop.cs
- CacheVirtualItemsEvent.cs
- RegexNode.cs
- HwndSourceKeyboardInputSite.cs
- OdbcStatementHandle.cs
- DataBindingValueUIHandler.cs
- LineInfo.cs
- _Rfc2616CacheValidators.cs
- WindowsTreeView.cs