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
- CodeIdentifiers.cs
- MenuItem.cs
- SurrogateSelector.cs
- MemoryFailPoint.cs
- WindowsAuthenticationModule.cs
- ACE.cs
- ByteStorage.cs
- ProcessInfo.cs
- CssClassPropertyAttribute.cs
- Compiler.cs
- UnsafeNativeMethodsCLR.cs
- PKCS1MaskGenerationMethod.cs
- GridView.cs
- PerfCounters.cs
- Positioning.cs
- PowerModeChangedEventArgs.cs
- ConfigXmlText.cs
- UrlPath.cs
- SqlGenerator.cs
- SizeConverter.cs
- safelink.cs
- CommonRemoteMemoryBlock.cs
- IListConverters.cs
- ScriptResourceAttribute.cs
- TextBox.cs
- SchemaNotation.cs
- Decimal.cs
- RegexBoyerMoore.cs
- FirstMatchCodeGroup.cs
- SoapBinding.cs
- wmiprovider.cs
- GridViewRow.cs
- Overlapped.cs
- GroupPartitionExpr.cs
- UIElementParagraph.cs
- MsmqIntegrationBindingElement.cs
- WindowsScrollBarBits.cs
- DbSetClause.cs
- MachineKeySection.cs
- StringValidatorAttribute.cs
- GlyphInfoList.cs
- SendingRequestEventArgs.cs
- TextAdaptor.cs
- StopStoryboard.cs
- ExpressionEditorAttribute.cs
- DesignTimeDataBinding.cs
- ListSurrogate.cs
- CodeDirectoryCompiler.cs
- ConfigurationPropertyCollection.cs
- CacheDependency.cs
- HttpDebugHandler.cs
- SqlParameterizer.cs
- SharedPersonalizationStateInfo.cs
- FormViewUpdateEventArgs.cs
- RecipientInfo.cs
- RedistVersionInfo.cs
- ScrollChrome.cs
- KeysConverter.cs
- _CookieModule.cs
- XmlCDATASection.cs
- CatalogZone.cs
- MailWebEventProvider.cs
- DiagnosticStrings.cs
- RoutingExtensionElement.cs
- UnsignedPublishLicense.cs
- ListView.cs
- WmfPlaceableFileHeader.cs
- Mappings.cs
- DataFieldConverter.cs
- XPathParser.cs
- SqlReorderer.cs
- DataGridViewLinkCell.cs
- ListViewItemSelectionChangedEvent.cs
- WebPartEditorCancelVerb.cs
- XmlDataProvider.cs
- ArraySet.cs
- GridSplitterAutomationPeer.cs
- ProfileSettingsCollection.cs
- CodeNamespaceCollection.cs
- Byte.cs
- TreeNodeStyleCollection.cs
- UnsafeNativeMethodsMilCoreApi.cs
- AuthenticatingEventArgs.cs
- AbstractSvcMapFileLoader.cs
- DataGridToolTip.cs
- ToolStripGripRenderEventArgs.cs
- TreeNodeStyleCollection.cs
- PageCodeDomTreeGenerator.cs
- ScriptControl.cs
- WebPageTraceListener.cs
- ZoneButton.cs
- WebZone.cs
- ClassGenerator.cs
- GenericRootAutomationPeer.cs
- InkPresenter.cs
- RotateTransform.cs
- DbSourceCommand.cs
- TreeNodeEventArgs.cs
- XmlSchemaSimpleType.cs
- _CommandStream.cs