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
- EventsTab.cs
- AdvancedBindingEditor.cs
- BitStream.cs
- ListViewItemSelectionChangedEvent.cs
- Encoding.cs
- ConfigUtil.cs
- BasicCellRelation.cs
- MenuItemBinding.cs
- EmptyEnumerator.cs
- PrimitiveDataContract.cs
- SmtpNetworkElement.cs
- FormsAuthenticationModule.cs
- ParseElementCollection.cs
- InternalEnumValidator.cs
- SqlMultiplexer.cs
- SqlCrossApplyToCrossJoin.cs
- GAC.cs
- FixedLineResult.cs
- UInt64Converter.cs
- SiteMapHierarchicalDataSourceView.cs
- PassportAuthenticationModule.cs
- FtpRequestCacheValidator.cs
- OpCopier.cs
- HostedHttpTransportManager.cs
- MimeWriter.cs
- MetadataPropertyAttribute.cs
- DataGridViewCellCancelEventArgs.cs
- ActivityCodeDomSerializer.cs
- Point3DCollectionConverter.cs
- CdpEqualityComparer.cs
- MediaPlayerState.cs
- DataControlImageButton.cs
- TailPinnedEventArgs.cs
- UIElementParaClient.cs
- AvTraceDetails.cs
- ListViewDeletedEventArgs.cs
- ContainerActivationHelper.cs
- PenContexts.cs
- Asn1IntegerConverter.cs
- SendingRequestEventArgs.cs
- FrameAutomationPeer.cs
- LinqDataSource.cs
- ActivityIdHeader.cs
- UrlMapping.cs
- BindingEditor.xaml.cs
- PtsContext.cs
- objectresult_tresulttype.cs
- ErrorFormatterPage.cs
- TextMarkerSource.cs
- DesignerGeometryHelper.cs
- CodeMethodReturnStatement.cs
- DocumentPage.cs
- XmlConvert.cs
- XmlChoiceIdentifierAttribute.cs
- PropertyEmitter.cs
- Helper.cs
- SizeAnimationClockResource.cs
- DesignerSerializerAttribute.cs
- HtmlInputCheckBox.cs
- DataObjectAttribute.cs
- glyphs.cs
- ViewgenGatekeeper.cs
- CellCreator.cs
- PerspectiveCamera.cs
- FontFamilyIdentifier.cs
- FixedTextContainer.cs
- CodeConditionStatement.cs
- activationcontext.cs
- DropShadowBitmapEffect.cs
- NavigationHelper.cs
- HotSpot.cs
- activationcontext.cs
- TemplateInstanceAttribute.cs
- CommonDialog.cs
- ResourcePermissionBaseEntry.cs
- FormCollection.cs
- sqlmetadatafactory.cs
- TrackingParameters.cs
- XmlUTF8TextWriter.cs
- Baml2006Reader.cs
- Accessors.cs
- HttpConfigurationContext.cs
- DefaultSection.cs
- _NetworkingPerfCounters.cs
- RotateTransform.cs
- HttpProfileBase.cs
- IteratorDescriptor.cs
- TextLineResult.cs
- HostedElements.cs
- FileLevelControlBuilderAttribute.cs
- ValidationRuleCollection.cs
- DesignRelationCollection.cs
- UTF8Encoding.cs
- ServiceReference.cs
- XmlSerializerVersionAttribute.cs
- Publisher.cs
- DragDeltaEventArgs.cs
- RenderData.cs
- Point4DValueSerializer.cs
- WriteStateInfoBase.cs