Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / ndp / fx / src / CommonUI / System / Drawing / Printing / PrintPreviewGraphics.cs / 1 / 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;
///
///
/// Retrives the printer graphics during preview.
///
internal class PrintPreviewGraphics {
private PrintPageEventArgs printPageEventArgs;
private PrintDocument printDocument;
public PrintPreviewGraphics(PrintDocument document, PrintPageEventArgs e) {
printPageEventArgs = e;
printDocument = document;
}
///
///
/// Gets the Visible bounds of this graphics object. Used during print preview.
///
public RectangleF VisibleClipBounds {
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;
///
///
/// Retrives the printer graphics during preview.
///
internal class PrintPreviewGraphics {
private PrintPageEventArgs printPageEventArgs;
private PrintDocument printDocument;
public PrintPreviewGraphics(PrintDocument document, PrintPageEventArgs e) {
printPageEventArgs = e;
printDocument = document;
}
///
///
/// Gets the Visible bounds of this graphics object. Used during print preview.
///
public RectangleF VisibleClipBounds {
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
- MemoryMappedFile.cs
- PasswordTextContainer.cs
- TextCompositionManager.cs
- StateBag.cs
- metrodevice.cs
- DropDownList.cs
- GZipStream.cs
- ProjectionPathBuilder.cs
- TextServicesProperty.cs
- HuffmanTree.cs
- SerializationAttributes.cs
- ContactManager.cs
- MetadataPropertyAttribute.cs
- KeySplineConverter.cs
- AuthenticationServiceManager.cs
- RawUIStateInputReport.cs
- WpfXamlLoader.cs
- ComAdminWrapper.cs
- PresentationAppDomainManager.cs
- ServiceRouteHandler.cs
- StylusPointPropertyId.cs
- LZCodec.cs
- DataGridHyperlinkColumn.cs
- CodeEventReferenceExpression.cs
- SQLGuidStorage.cs
- XmlSchemaSimpleTypeRestriction.cs
- XmlQualifiedName.cs
- SamlAssertion.cs
- XmlSchemaSimpleContent.cs
- XPathNavigator.cs
- XappLauncher.cs
- NativeActivityTransactionContext.cs
- SocketAddress.cs
- ReadOnlyTernaryTree.cs
- CacheChildrenQuery.cs
- ApplicationBuildProvider.cs
- OracleInfoMessageEventArgs.cs
- ScriptMethodAttribute.cs
- DbProviderSpecificTypePropertyAttribute.cs
- _ListenerResponseStream.cs
- DbBuffer.cs
- NetStream.cs
- IdlingCommunicationPool.cs
- ValueType.cs
- ComponentRenameEvent.cs
- RecordsAffectedEventArgs.cs
- SignatureDescription.cs
- ToolBarButton.cs
- DocumentApplicationState.cs
- ReceiveMessageRecord.cs
- ToolboxComponentsCreatingEventArgs.cs
- ModelFunction.cs
- DiscriminatorMap.cs
- TextEditorDragDrop.cs
- BulletedListEventArgs.cs
- ButtonBaseAdapter.cs
- ServerProtocol.cs
- DataObjectFieldAttribute.cs
- CodeParameterDeclarationExpressionCollection.cs
- EntityDataSourceDataSelection.cs
- WS2007FederationHttpBindingCollectionElement.cs
- EventLevel.cs
- SmtpDateTime.cs
- WebBrowserEvent.cs
- Validator.cs
- AttributeTableBuilder.cs
- XmlFormatExtensionPointAttribute.cs
- SingleAnimation.cs
- StickyNoteContentControl.cs
- RelationshipSet.cs
- complextypematerializer.cs
- UrlPropertyAttribute.cs
- Transactions.cs
- Int16.cs
- DPCustomTypeDescriptor.cs
- X509ThumbprintKeyIdentifierClause.cs
- Descriptor.cs
- CreateUserErrorEventArgs.cs
- ProxyWebPartConnectionCollection.cs
- ellipse.cs
- _ListenerResponseStream.cs
- MimePart.cs
- SettingsAttributes.cs
- DataGridViewButtonCell.cs
- JsonWriterDelegator.cs
- KnowledgeBase.cs
- AsnEncodedData.cs
- TryCatchDesigner.xaml.cs
- SimpleParser.cs
- SequentialUshortCollection.cs
- LinqDataSourceSelectEventArgs.cs
- ItemsChangedEventArgs.cs
- SchemaNamespaceManager.cs
- ThreadInterruptedException.cs
- ColumnTypeConverter.cs
- EntityContainerEntitySet.cs
- Brush.cs
- DoubleAnimationBase.cs
- Substitution.cs
- DocumentViewerConstants.cs