Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / CommonUI / System / Drawing / Printing / PrintPreviewGraphics.cs / 1305376 / PrintPreviewGraphics.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Drawing { using System.Runtime.InteropServices; using System.ComponentModel; using System.Diagnostics; using System; using Microsoft.Win32; using System.Security; using System.Security.Permissions; using System.Drawing.Internal; using System.Drawing.Imaging; using System.Drawing.Text; using System.Drawing.Drawing2D; using System.Drawing.Printing; using System.Runtime.Versioning; ////// /// internal class PrintPreviewGraphics { private PrintPageEventArgs printPageEventArgs; private PrintDocument printDocument; public PrintPreviewGraphics(PrintDocument document, PrintPageEventArgs e) { printPageEventArgs = e; printDocument = document; } ///Retrives the printer graphics during preview. ////// /// Gets the Visible bounds of this graphics object. Used during print preview. /// public RectangleF VisibleClipBounds { [ResourceExposure(ResourceScope.None)] [ResourceConsumption(ResourceScope.Process, ResourceScope.Process)] get { IntPtr hdevMode = printPageEventArgs.PageSettings.PrinterSettings.GetHdevmodeInternal(); using( DeviceContext dc = printPageEventArgs.PageSettings.PrinterSettings.CreateDeviceContext(hdevMode)) { using( Graphics graphics = Graphics.FromHdcInternal(dc.Hdc) ) { if (printDocument.OriginAtMargins) { // Adjust the origin of the graphics object to be at the user-specified margin location // Note: Graphics.FromHdc internally calls SaveDC(hdc), we can still use the saved hdc to get the resolution. 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(printDocument.DefaultPageSettings.Margins.Left, printDocument.DefaultPageSettings.Margins.Top); } return graphics.VisibleClipBounds; } } } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Drawing { using System.Runtime.InteropServices; using System.ComponentModel; using System.Diagnostics; using System; using Microsoft.Win32; using System.Security; using System.Security.Permissions; using System.Drawing.Internal; using System.Drawing.Imaging; using System.Drawing.Text; using System.Drawing.Drawing2D; using System.Drawing.Printing; using System.Runtime.Versioning; ////// /// internal class PrintPreviewGraphics { private PrintPageEventArgs printPageEventArgs; private PrintDocument printDocument; public PrintPreviewGraphics(PrintDocument document, PrintPageEventArgs e) { printPageEventArgs = e; printDocument = document; } ///Retrives the printer graphics during preview. ////// /// Gets the Visible bounds of this graphics object. Used during print preview. /// public RectangleF VisibleClipBounds { [ResourceExposure(ResourceScope.None)] [ResourceConsumption(ResourceScope.Process, ResourceScope.Process)] get { IntPtr hdevMode = printPageEventArgs.PageSettings.PrinterSettings.GetHdevmodeInternal(); using( DeviceContext dc = printPageEventArgs.PageSettings.PrinterSettings.CreateDeviceContext(hdevMode)) { using( Graphics graphics = Graphics.FromHdcInternal(dc.Hdc) ) { if (printDocument.OriginAtMargins) { // Adjust the origin of the graphics object to be at the user-specified margin location // Note: Graphics.FromHdc internally calls SaveDC(hdc), we can still use the saved hdc to get the resolution. 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(printDocument.DefaultPageSettings.Margins.Left, printDocument.DefaultPageSettings.Margins.Top); } return graphics.VisibleClipBounds; } } } } } } // 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
- LicenseManager.cs
- FieldAccessException.cs
- ResourceManager.cs
- EventMappingSettingsCollection.cs
- ValidationHelper.cs
- Span.cs
- TokenizerHelper.cs
- ValidateNames.cs
- Container.cs
- ExpressionTextBox.xaml.cs
- CodeBlockBuilder.cs
- OleAutBinder.cs
- RuntimeConfig.cs
- PointValueSerializer.cs
- FaultBookmark.cs
- NotifyInputEventArgs.cs
- UTF7Encoding.cs
- EntityConnectionStringBuilder.cs
- LambdaCompiler.Statements.cs
- RangeValuePattern.cs
- BindingWorker.cs
- RootBrowserWindow.cs
- ButtonFlatAdapter.cs
- FilteredDataSetHelper.cs
- ColumnBinding.cs
- BaseServiceProvider.cs
- SignatureToken.cs
- NativeMethods.cs
- OletxResourceManager.cs
- XmlSchemaCollection.cs
- RayMeshGeometry3DHitTestResult.cs
- Permission.cs
- CultureSpecificStringDictionary.cs
- PropertyMetadata.cs
- _LocalDataStore.cs
- ItemCollection.cs
- DrawingState.cs
- CodeDirectoryCompiler.cs
- DataGridViewSelectedRowCollection.cs
- WebColorConverter.cs
- EntityContainerEntitySet.cs
- SecurityContext.cs
- Point3DCollectionValueSerializer.cs
- ListViewSelectEventArgs.cs
- UIElement3D.cs
- MouseWheelEventArgs.cs
- webbrowsersite.cs
- Quaternion.cs
- SqlUserDefinedAggregateAttribute.cs
- ByeOperation11AsyncResult.cs
- SHA384.cs
- BufferModesCollection.cs
- BitmapDecoder.cs
- WebUtility.cs
- CellQuery.cs
- SqlClientWrapperSmiStream.cs
- WebPartExportVerb.cs
- DataGridColumnStyleMappingNameEditor.cs
- NativeMethods.cs
- HtmlInputSubmit.cs
- RuleSetDialog.cs
- DataGridViewColumnCollectionDialog.cs
- FrameworkElement.cs
- AttachedPropertyBrowsableAttribute.cs
- PrintDialogException.cs
- SoapExtensionTypeElementCollection.cs
- TimelineClockCollection.cs
- EntityProxyFactory.cs
- TypeDescriptor.cs
- WebPermission.cs
- SecurityUtils.cs
- dtdvalidator.cs
- TraceHandlerErrorFormatter.cs
- RunInstallerAttribute.cs
- XmlDataDocument.cs
- PermissionToken.cs
- SpecularMaterial.cs
- ProfilePropertySettingsCollection.cs
- EventSetter.cs
- CellConstantDomain.cs
- DataGridHelper.cs
- GraphicsContainer.cs
- PagedDataSource.cs
- SQLInt64.cs
- TagPrefixAttribute.cs
- ObjectDataSourceSelectingEventArgs.cs
- __Filters.cs
- smtpconnection.cs
- QuaternionAnimationUsingKeyFrames.cs
- PathSegmentCollection.cs
- HelpKeywordAttribute.cs
- DefaultBinder.cs
- SpecularMaterial.cs
- BlockUIContainer.cs
- BindingContext.cs
- XPathNodeHelper.cs
- Calendar.cs
- CompositeFontParser.cs
- CustomSignedXml.cs
- DesignerActionMethodItem.cs