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
- StyleCollection.cs
- SupportedAddressingMode.cs
- ThemeDirectoryCompiler.cs
- MemoryResponseElement.cs
- ClientScriptManager.cs
- StatusBar.cs
- MenuAutomationPeer.cs
- SqlTrackingService.cs
- IsolatedStorageFile.cs
- RuleInfoComparer.cs
- SmiMetaData.cs
- MultipartIdentifier.cs
- DescendantQuery.cs
- RenderOptions.cs
- DataDesignUtil.cs
- StopStoryboard.cs
- MSAAWinEventWrap.cs
- PeerNameRecordCollection.cs
- DataGridViewEditingControlShowingEventArgs.cs
- DummyDataSource.cs
- SessionState.cs
- RegistrationServices.cs
- SizeAnimationClockResource.cs
- CompiledIdentityConstraint.cs
- FontSource.cs
- TreeChangeInfo.cs
- ActivityInterfaces.cs
- CollectionView.cs
- ColumnWidthChangedEvent.cs
- NavigatingCancelEventArgs.cs
- UserPersonalizationStateInfo.cs
- AspNetHostingPermission.cs
- WindowsRichEditRange.cs
- RecordsAffectedEventArgs.cs
- StorageEndPropertyMapping.cs
- MapPathBasedVirtualPathProvider.cs
- EventMap.cs
- PrivateFontCollection.cs
- SectionVisual.cs
- BaseCollection.cs
- ControlIdConverter.cs
- ExpressionPrinter.cs
- NavigatorOutput.cs
- DependencyProperty.cs
- RelationshipFixer.cs
- UpdatePanelTriggerCollection.cs
- FixUp.cs
- ServerValidateEventArgs.cs
- SuppressMergeCheckAttribute.cs
- ExpressionBuilderContext.cs
- DoubleCollection.cs
- CfgParser.cs
- TemplateField.cs
- HuffmanTree.cs
- ClientSideQueueItem.cs
- ThreadExceptionDialog.cs
- Stack.cs
- OdbcException.cs
- GiveFeedbackEvent.cs
- RevocationPoint.cs
- OracleException.cs
- SQLDoubleStorage.cs
- SQLCharsStorage.cs
- XmlSerializerVersionAttribute.cs
- ExtendedProtectionPolicy.cs
- TextTreeTextElementNode.cs
- EmbossBitmapEffect.cs
- ServiceMetadataBehavior.cs
- DictionaryBase.cs
- ViewStateModeByIdAttribute.cs
- MenuItem.cs
- ScrollBarRenderer.cs
- Span.cs
- SqlDataSourceCommandParser.cs
- LogSwitch.cs
- SimpleMailWebEventProvider.cs
- WebEventCodes.cs
- URLMembershipCondition.cs
- DispatcherObject.cs
- ProxyHwnd.cs
- FlowDocumentReaderAutomationPeer.cs
- IndexerNameAttribute.cs
- StylusButtonEventArgs.cs
- ActivityBindForm.Designer.cs
- SystemResourceHost.cs
- XsdCachingReader.cs
- DataRelation.cs
- SiteMapDataSourceView.cs
- StylusPointPropertyUnit.cs
- DiagnosticsConfigurationHandler.cs
- Ref.cs
- RC2CryptoServiceProvider.cs
- TableLayout.cs
- _HeaderInfo.cs
- EmbeddedMailObject.cs
- ExpandSegment.cs
- ConnectionManagementElement.cs
- dsa.cs
- CounterCreationData.cs
- Types.cs