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
- GridViewCancelEditEventArgs.cs
- XmlAttribute.cs
- sqlnorm.cs
- RetrieveVirtualItemEventArgs.cs
- HttpModuleCollection.cs
- OpenFileDialog.cs
- SelectionPatternIdentifiers.cs
- SqlConnectionHelper.cs
- TextEffect.cs
- PagedControl.cs
- OracleException.cs
- RotateTransform3D.cs
- EmptyReadOnlyDictionaryInternal.cs
- SerialReceived.cs
- errorpatternmatcher.cs
- WebResourceUtil.cs
- DocumentPageView.cs
- CounterCreationData.cs
- PersonalizationProviderHelper.cs
- LayoutUtils.cs
- DesignerProperties.cs
- SpellerError.cs
- MenuDesigner.cs
- ApplicationActivator.cs
- RecipientIdentity.cs
- XamlFilter.cs
- WebBrowserDesigner.cs
- EpmContentDeSerializer.cs
- Clock.cs
- SamlAuthenticationClaimResource.cs
- BuildProvider.cs
- FormsAuthenticationEventArgs.cs
- AuthorizationSection.cs
- TreeBuilder.cs
- LineBreakRecord.cs
- ViewGenerator.cs
- ProxyManager.cs
- DelegateBodyWriter.cs
- FtpCachePolicyElement.cs
- pingexception.cs
- StringToken.cs
- CDSCollectionETWBCLProvider.cs
- _AutoWebProxyScriptEngine.cs
- DataGridCellsPresenter.cs
- PipeStream.cs
- PointAnimationUsingPath.cs
- TouchEventArgs.cs
- IPEndPoint.cs
- CodeTypeDeclaration.cs
- LabelLiteral.cs
- TdsRecordBufferSetter.cs
- X509Certificate2Collection.cs
- HwndStylusInputProvider.cs
- TableSectionStyle.cs
- CommentEmitter.cs
- PersonalizationAdministration.cs
- Events.cs
- CounterCreationDataCollection.cs
- ConfigXmlText.cs
- PackWebResponse.cs
- ContextMenu.cs
- SiteMapSection.cs
- PersonalizationDictionary.cs
- InvalidOperationException.cs
- StickyNoteHelper.cs
- DataGridViewSelectedCellsAccessibleObject.cs
- ArrayList.cs
- DocumentApplicationJournalEntry.cs
- SmtpTransport.cs
- SSmlParser.cs
- SystemWebCachingSectionGroup.cs
- GradientBrush.cs
- EdgeProfileValidation.cs
- RelatedView.cs
- MonitorWrapper.cs
- FixedTextView.cs
- Add.cs
- DataColumnSelectionConverter.cs
- SmiMetaDataProperty.cs
- GeneralTransform.cs
- SymbolType.cs
- ModifiableIteratorCollection.cs
- UIElementHelper.cs
- TcpConnectionPoolSettings.cs
- WsdlBuildProvider.cs
- HttpHandlerActionCollection.cs
- WindowsContainer.cs
- HttpInputStream.cs
- HandlerWithFactory.cs
- IgnoreSectionHandler.cs
- PenThreadPool.cs
- baseaxisquery.cs
- SchemaSetCompiler.cs
- UpdatePanelTriggerCollection.cs
- PrintDialog.cs
- SqlReferenceCollection.cs
- XmlnsDefinitionAttribute.cs
- X509KeyIdentifierClauseType.cs
- ConsoleTraceListener.cs
- ValidationSummary.cs