Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / WinForms / Managed / System / WinForms / Printing / PageSetupDialog.cs / 1305376 / PageSetupDialog.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Windows.Forms { using Microsoft.Win32; using System; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Drawing.Printing; using System.Runtime.InteropServices; using System.Security; using System.Text; using System.Globalization; ////// /// [DefaultProperty("Document")] [SRDescription(SR.DescriptionPageSetupDialog)] // The only event this dialog has is HelpRequested, which isn't very useful public sealed class PageSetupDialog : CommonDialog { // If PrintDocument != null, pageSettings == printDocument.PageSettings private PrintDocument printDocument = null; private PageSettings pageSettings = null; private PrinterSettings printerSettings = null; private bool allowMargins; private bool allowOrientation; private bool allowPaper; private bool allowPrinter; private Margins minMargins; private bool showHelp; private bool showNetwork; private bool enableMetric; ///Represents /// a dialog box that allows users to manipulate page settings, including margins and paper orientation. ////// /// public PageSetupDialog() { Reset(); } ///Initializes a new instance of the ///class. /// /// [ SRCategory(SR.CatBehavior), DefaultValue(true), SRDescription(SR.PSDallowMarginsDescr) ] public bool AllowMargins { get { return allowMargins; } set { allowMargins = value; } } ////// Gets or sets a value indicating whether the margins section of the dialog box is enabled. /// /// ////// /// [ SRCategory(SR.CatBehavior), DefaultValue(true), SRDescription(SR.PSDallowOrientationDescr) ] public bool AllowOrientation { get { return allowOrientation;} set { allowOrientation = value;} } ///Gets or sets a value indicating whether the orientation section of the dialog box (landscape vs. portrait) /// is enabled. /// ////// /// [ SRCategory(SR.CatBehavior), DefaultValue(true), SRDescription(SR.PSDallowPaperDescr) ] public bool AllowPaper { get { return allowPaper;} set { allowPaper = value;} } ////// Gets or sets a value indicating whether the paper section of the dialog box (paper size and paper source) /// is enabled. /// /// ////// /// [ SRCategory(SR.CatBehavior), DefaultValue(true), SRDescription(SR.PSDallowPrinterDescr) ] public bool AllowPrinter { get { return allowPrinter;} set { allowPrinter = value;} } ////// Gets or sets a value indicating whether the Printer button is enabled. /// /// ////// /// [ SRCategory(SR.CatData), DefaultValue(null), SRDescription(SR.PDdocumentDescr) ] public PrintDocument Document { get { return printDocument;} set { printDocument = value; if (printDocument != null) { pageSettings = printDocument.DefaultPageSettings; printerSettings = printDocument.PrinterSettings; } } } ///Gets or sets a value indicating the ////// to get page settings from. /// /// /// This allows the user to override the current behavior where the Metric is converted to ThousandOfInch even for METRIC MEASUREMENTSYSTEM /// which returns a HUNDREDSOFMILLIMETER value. /// [ DefaultValue(false), SRDescription(SR.PSDenableMetricDescr), Browsable(true), EditorBrowsable(EditorBrowsableState.Always) ] public bool EnableMetric { get { return enableMetric;} set { enableMetric = value;} } ////// /// [ SRCategory(SR.CatData), SRDescription(SR.PSDminMarginsDescr) ] public Margins MinMargins { get { return minMargins;} set { if (value == null) value = new Margins(0, 0, 0, 0); minMargins = value; } } ////// Gets or sets a value indicating the minimum margins the /// user is allowed to select, in hundredths of an inch. /// /// ////// /// [ SRCategory(SR.CatData), DefaultValue(null), Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), SRDescription(SR.PSDpageSettingsDescr) ] public PageSettings PageSettings { get { return pageSettings;} set { pageSettings = value; printDocument = null; } } ////// Gets /// or sets /// a value indicating /// the page settings modified by the dialog box. /// /// ////// /// [ SRCategory(SR.CatData), DefaultValue(null), Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), SRDescription(SR.PSDprinterSettingsDescr) ] public PrinterSettings PrinterSettings { get { return printerSettings;} set { printerSettings = value; printDocument = null; } } ////// Gets /// or sets the printer /// settings the dialog box will modify if the user clicks the Printer button. /// ////// /// [ SRCategory(SR.CatBehavior), DefaultValue(false), SRDescription(SR.PSDshowHelpDescr) ] public bool ShowHelp { get { return showHelp;} set { showHelp = value;} } ////// Gets or sets a value indicating whether the Help button is visible. /// ////// /// [ SRCategory(SR.CatBehavior), DefaultValue(true), SRDescription(SR.PSDshowNetworkDescr) ] public bool ShowNetwork { get { return showNetwork;} set { showNetwork = value;} } private int GetFlags() { int flags = 0; flags |= NativeMethods.PSD_ENABLEPAGESETUPHOOK; if (!allowMargins) flags |= NativeMethods.PSD_DISABLEMARGINS; if (!allowOrientation) flags |= NativeMethods.PSD_DISABLEORIENTATION; if (!allowPaper) flags |= NativeMethods.PSD_DISABLEPAPER; if (!allowPrinter || printerSettings == null) flags |= NativeMethods.PSD_DISABLEPRINTER; if (showHelp) flags |= NativeMethods.PSD_SHOWHELP; if (!showNetwork) flags |= NativeMethods.PSD_NONETWORKBUTTON; if (minMargins != null) flags |= NativeMethods.PSD_MINMARGINS; if (pageSettings.Margins != null) flags |= NativeMethods.PSD_MARGINS; // return flags; } ////// Gets or sets a value indicating whether the Network button is visible. /// ////// /// public override void Reset() { allowMargins = true; allowOrientation = true; allowPaper = true; allowPrinter = true; MinMargins = null; // turns into Margin with all zeros pageSettings = null; printDocument = null; printerSettings = null; showHelp = false; showNetwork = true; } private void ResetMinMargins() { MinMargins = null; } ////// Resets all options to their default values. /// ////// /// private bool ShouldSerializeMinMargins() { return minMargins.Left != 0 || minMargins.Right != 0 || minMargins.Top != 0 || minMargins.Bottom != 0; } private static void UpdateSettings(NativeMethods.PAGESETUPDLG data, PageSettings pageSettings, PrinterSettings printerSettings) { // SetHDevMode demands AllPrintingAndUnmanagedCode Permission : Since we are calling that function we should Assert the permision, IntSecurity.AllPrintingAndUnmanagedCode.Assert(); try { pageSettings.SetHdevmode(data.hDevMode); if (printerSettings != null) { printerSettings.SetHdevmode(data.hDevMode); printerSettings.SetHdevnames(data.hDevNames); } } finally { CodeAccessPermission.RevertAssert(); } Margins newMargins = new Margins(); newMargins.Left = data.marginLeft; newMargins.Top = data.marginTop; newMargins.Right = data.marginRight; newMargins.Bottom = data.marginBottom; PrinterUnit fromUnit = ((data.Flags & NativeMethods.PSD_INHUNDREDTHSOFMILLIMETERS) != 0) ? PrinterUnit.HundredthsOfAMillimeter : PrinterUnit.ThousandthsOfAnInch; pageSettings.Margins = PrinterUnitConvert.Convert(newMargins, fromUnit, PrinterUnit.Display); } ////// Indicates whether the ////// property should be /// persisted. /// /// /// ///protected override bool RunDialog(IntPtr hwndOwner) { IntSecurity.SafePrinting.Demand(); NativeMethods.WndProc hookProcPtr = new NativeMethods.WndProc(this.HookProc); if (pageSettings == null) throw new ArgumentException(SR.GetString(SR.PSDcantShowWithoutPage)); NativeMethods.PAGESETUPDLG data = new NativeMethods.PAGESETUPDLG(); data.lStructSize = Marshal.SizeOf(data); data.Flags = GetFlags(); data.hwndOwner = hwndOwner; data.lpfnPageSetupHook = hookProcPtr; PrinterUnit toUnit = PrinterUnit.ThousandthsOfAnInch; // Refer VSWhidbey: 331160. Below was a breaking change from RTM and EVERETT even though this was a correct FIX. // EnableMetric is a new Whidbey property which we allow the users to choose between the AutoConversion or not. if (EnableMetric) { //take the Units of Measurement while determining the PrinterUnits... //bug (121347)... StringBuilder sb = new StringBuilder(2); int result = UnsafeNativeMethods.GetLocaleInfo(NativeMethods.LOCALE_USER_DEFAULT,NativeMethods.LOCALE_IMEASURE, sb,sb.Capacity); if (result > 0 && Int32.Parse(sb.ToString(), CultureInfo.InvariantCulture) == 0) { toUnit = PrinterUnit.HundredthsOfAMillimeter; } } if (MinMargins != null) { Margins margins = PrinterUnitConvert.Convert(MinMargins, PrinterUnit.Display, toUnit); data.minMarginLeft = margins.Left; data.minMarginTop = margins.Top; data.minMarginRight = margins.Right; data.minMarginBottom = margins.Bottom; } if (pageSettings.Margins != null) { Margins margins = PrinterUnitConvert.Convert(pageSettings.Margins, PrinterUnit.Display, toUnit); data.marginLeft = margins.Left; data.marginTop = margins.Top; data.marginRight = margins.Right; data.marginBottom = margins.Bottom; } // Ensure that the margins are >= minMargins. // This is a requirement of the PAGESETUPDLG structure. // data.marginLeft = Math.Max(data.marginLeft, data.minMarginLeft); data.marginTop = Math.Max(data.marginTop, data.minMarginTop); data.marginRight = Math.Max(data.marginRight, data.minMarginRight); data.marginBottom = Math.Max(data.marginBottom, data.minMarginBottom); PrinterSettings printer = (printerSettings == null) ? pageSettings.PrinterSettings : printerSettings; // GetHDevmode demands AllPrintingAndUnmanagedCode Permission : Since we are calling that function we should Assert the permision, IntSecurity.AllPrintingAndUnmanagedCode.Assert(); try { data.hDevMode = printer.GetHdevmode(pageSettings); data.hDevNames = printer.GetHdevnames(); } finally { CodeAccessPermission.RevertAssert(); } try { bool status = UnsafeNativeMethods.PageSetupDlg(data); if (!status) { // Debug.WriteLine(Windows.CommonDialogErrorToString(Windows.CommDlgExtendedError())); return false; } UpdateSettings(data, pageSettings, printerSettings); // yes, printerSettings, not printer return true; } finally { UnsafeNativeMethods.GlobalFree(new HandleRef(data, data.hDevMode)); UnsafeNativeMethods.GlobalFree(new HandleRef(data, data.hDevNames)); } } } } // 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
- Parameter.cs
- HttpCapabilitiesEvaluator.cs
- WebEvents.cs
- DispatcherExceptionFilterEventArgs.cs
- IPEndPoint.cs
- TdsParserStateObject.cs
- Trace.cs
- VarInfo.cs
- TextElementEnumerator.cs
- MinimizableAttributeTypeConverter.cs
- EntityClassGenerator.cs
- WebBrowsableAttribute.cs
- FileSecurity.cs
- SemaphoreSlim.cs
- ButtonFieldBase.cs
- ValueTypeFixupInfo.cs
- iisPickupDirectory.cs
- WebPartConnectionsConnectVerb.cs
- KnownIds.cs
- Cell.cs
- WebScriptEnablingElement.cs
- Transform.cs
- TypeExtensionSerializer.cs
- MarkupExtensionParser.cs
- SmiContextFactory.cs
- PathFigureCollectionConverter.cs
- QuadraticBezierSegment.cs
- COM2ExtendedBrowsingHandler.cs
- WmlLabelAdapter.cs
- ProcessingInstructionAction.cs
- NodeFunctions.cs
- SoapElementAttribute.cs
- XPathNodePointer.cs
- _SslSessionsCache.cs
- ParentQuery.cs
- DbDataAdapter.cs
- AffineTransform3D.cs
- TextModifierScope.cs
- PageCodeDomTreeGenerator.cs
- GatewayDefinition.cs
- XPathExpr.cs
- ConnectionManagementElementCollection.cs
- PeerToPeerException.cs
- SafeUserTokenHandle.cs
- SortExpressionBuilder.cs
- AllMembershipCondition.cs
- AutomationPropertyInfo.cs
- SchemaMerger.cs
- SpecularMaterial.cs
- DbParameterCollectionHelper.cs
- ConnectionInterfaceCollection.cs
- ReaderContextStackData.cs
- StringFormat.cs
- XmlQueryStaticData.cs
- LassoSelectionBehavior.cs
- ReferencedType.cs
- BinaryObjectWriter.cs
- CodeDirectionExpression.cs
- Decoder.cs
- LockedHandleGlyph.cs
- AssemblyAssociatedContentFileAttribute.cs
- XmlCountingReader.cs
- HtmlTableRow.cs
- ResourcePermissionBase.cs
- DiscreteKeyFrames.cs
- ObjectAnimationBase.cs
- AsyncResult.cs
- InputScopeConverter.cs
- SessionState.cs
- Int32CAMarshaler.cs
- CodeTypeReferenceCollection.cs
- StateMachineHelpers.cs
- UnionExpr.cs
- RSAPKCS1SignatureFormatter.cs
- MLangCodePageEncoding.cs
- HttpApplicationFactory.cs
- XmlAnyElementAttribute.cs
- XPathSelfQuery.cs
- Variant.cs
- AbandonedMutexException.cs
- URIFormatException.cs
- SmiEventSink_Default.cs
- SqlHelper.cs
- SqlDataSourceStatusEventArgs.cs
- AppSettingsSection.cs
- MenuItemStyleCollectionEditor.cs
- PointLight.cs
- DependencyPropertyAttribute.cs
- MsmqHostedTransportManager.cs
- DataReceivedEventArgs.cs
- ResourceDefaultValueAttribute.cs
- _Semaphore.cs
- TableDesigner.cs
- StringConverter.cs
- XmlCharCheckingReader.cs
- PowerStatus.cs
- DataGridViewRowConverter.cs
- ResourceWriter.cs
- GlyphShapingProperties.cs
- WebBrowsableAttribute.cs