Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / whidbey / NetFXspW7 / ndp / fx / src / WinForms / Managed / System / WinForms / Printing / PrintControllerWithStatusDialog.cs / 1 / PrintControllerWithStatusDialog.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Windows.Forms { using System.Diagnostics; using System; using System.Threading; using System.Drawing; using Microsoft.Win32; using System.ComponentModel; using System.Drawing.Printing; using System.Security; using System.Security.Permissions; ////// /// public class PrintControllerWithStatusDialog : PrintController { private PrintController underlyingController; private PrintDocument document; private BackgroundThread backgroundThread; private int pageNumber; private string dialogTitle; ///[To be supplied.] ////// /// public PrintControllerWithStatusDialog(PrintController underlyingController) : this(underlyingController, SR.GetString(SR.PrintControllerWithStatusDialog_DialogTitlePrint)) { } ///[To be supplied.] ////// /// public PrintControllerWithStatusDialog(PrintController underlyingController, string dialogTitle) { this.underlyingController = underlyingController; this.dialogTitle = dialogTitle; } ///[To be supplied.] ////// /// public override bool IsPreview { get { if (underlyingController != null) { return underlyingController.IsPreview; } return false; } } ////// This is new public property which notifies if this controller is used for PrintPreview.. so get the underlying Controller /// and return its IsPreview Property. /// ////// /// /// public override void OnStartPrint(PrintDocument document, PrintEventArgs e) { base.OnStartPrint(document, e); this.document = document; pageNumber = 1; if (SystemInformation.UserInteractive) { backgroundThread = new BackgroundThread(this); // starts running & shows dialog automatically } // OnStartPrint does the security check... lots of // extra setup to make sure that we tear down // correctly... // try { underlyingController.OnStartPrint(document, e); } catch { if (backgroundThread != null) { backgroundThread.Stop(); } throw; } finally { if (backgroundThread != null && backgroundThread.canceled) { e.Cancel = true; } } } ////// Implements StartPrint by delegating to the underlying controller. /// ////// /// /// public override Graphics OnStartPage(PrintDocument document, PrintPageEventArgs e) { base.OnStartPage(document, e); if (backgroundThread != null) { backgroundThread.UpdateLabel(); } Graphics result = underlyingController.OnStartPage(document, e); if (backgroundThread != null && backgroundThread.canceled){ e.Cancel = true; } return result; } ////// Implements StartPage by delegating to the underlying controller. /// ////// /// /// public override void OnEndPage(PrintDocument document, PrintPageEventArgs e) { underlyingController.OnEndPage(document, e); if (backgroundThread != null && backgroundThread.canceled) { e.Cancel = true; } pageNumber++; base.OnEndPage(document, e); } ////// Implements EndPage by delegating to the underlying controller. /// ////// /// /// public override void OnEndPrint(PrintDocument document, PrintEventArgs e) { underlyingController.OnEndPrint(document, e); if (backgroundThread != null && backgroundThread.canceled) { e.Cancel = true; } if (backgroundThread != null) { backgroundThread.Stop(); } base.OnEndPrint(document, e); } private class BackgroundThread { private PrintControllerWithStatusDialog parent; private StatusDialog dialog; private Thread thread; internal bool canceled = false; private bool alreadyStopped = false; // Called from any thread internal BackgroundThread(PrintControllerWithStatusDialog parent) { this.parent = parent; // Calling Application.DoEvents() from within a paint event causes all sorts of problems, // so we need to put the dialog on its own thread. thread = new Thread(new ThreadStart(Run)); thread.SetApartmentState(ApartmentState.STA); thread.Start(); } // on correct thread [ UIPermission(SecurityAction.Assert, Window=UIPermissionWindow.AllWindows), SecurityPermission(SecurityAction.Assert, Flags=SecurityPermissionFlag.UnmanagedCode), ] private void Run() { // SECREVIEW : need all permissions to make the window not get adorned... // try { lock (this) { if (alreadyStopped) { return; } dialog = new StatusDialog(this, parent.dialogTitle); ThreadUnsafeUpdateLabel(); dialog.Visible = true; } if (!alreadyStopped) { Application.Run(dialog); } } finally { lock (this) { if (dialog != null) { dialog.Dispose(); dialog = null; } } } } // Called from any thread internal void Stop() { lock (this) { if (dialog != null && dialog.IsHandleCreated) { dialog.BeginInvoke(new MethodInvoker(dialog.Close)); return; } alreadyStopped = true; } } // on correct thread private void ThreadUnsafeUpdateLabel() { // "page {0} of {1}" dialog.label1.Text = SR.GetString(SR.PrintControllerWithStatusDialog_NowPrinting, parent.pageNumber, parent.document.DocumentName); } // Called from any thread internal void UpdateLabel() { if (dialog != null && dialog.IsHandleCreated) { dialog.BeginInvoke(new MethodInvoker(ThreadUnsafeUpdateLabel)); // Don't wait for a response } } } private class StatusDialog : Form { internal Label label1; private Button button1; private BackgroundThread backgroundThread; internal StatusDialog(BackgroundThread backgroundThread, string dialogTitle) { InitializeComponent(); this.backgroundThread = backgroundThread; this.Text = dialogTitle; this.MinimumSize = Size; } ////// Implements EndPrint by delegating to the underlying controller. /// ////// Tells whether the current resources for this dll have been /// localized for a RTL language. /// private static bool IsRTLResources { get { return SR.GetString(SR.RTL) != "RTL_False"; } } private void InitializeComponent() { if (IsRTLResources) { this.RightToLeft = RightToLeft.Yes; } this.label1 = new Label(); this.button1 = new Button(); label1.Location = new Point(8, 16); label1.TextAlign = ContentAlignment.MiddleCenter; label1.Size = new Size(240, 64); label1.TabIndex = 1; label1.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; button1.Size = new Size(75, 23); button1.TabIndex = 0; button1.Text = SR.GetString(SR.PrintControllerWithStatusDialog_Cancel); button1.Location = new Point(88, 88); button1.Anchor = AnchorStyles.Bottom; button1.Click += new EventHandler(button1_Click); this.AutoScaleDimensions = new Size(6, 13); this.AutoScaleMode = AutoScaleMode.Font; this.MaximizeBox = false; this.ControlBox = false; this.MinimizeBox = false; this.ClientSize = new Size(256, 122); this.CancelButton = button1; this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide; this.Controls.Add(label1); this.Controls.Add(button1); } private void button1_Click(object sender, System.EventArgs e) { button1.Enabled = false; label1.Text = SR.GetString(SR.PrintControllerWithStatusDialog_Canceling); backgroundThread.canceled = true; } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Windows.Forms { using System.Diagnostics; using System; using System.Threading; using System.Drawing; using Microsoft.Win32; using System.ComponentModel; using System.Drawing.Printing; using System.Security; using System.Security.Permissions; ////// /// public class PrintControllerWithStatusDialog : PrintController { private PrintController underlyingController; private PrintDocument document; private BackgroundThread backgroundThread; private int pageNumber; private string dialogTitle; ///[To be supplied.] ////// /// public PrintControllerWithStatusDialog(PrintController underlyingController) : this(underlyingController, SR.GetString(SR.PrintControllerWithStatusDialog_DialogTitlePrint)) { } ///[To be supplied.] ////// /// public PrintControllerWithStatusDialog(PrintController underlyingController, string dialogTitle) { this.underlyingController = underlyingController; this.dialogTitle = dialogTitle; } ///[To be supplied.] ////// /// public override bool IsPreview { get { if (underlyingController != null) { return underlyingController.IsPreview; } return false; } } ////// This is new public property which notifies if this controller is used for PrintPreview.. so get the underlying Controller /// and return its IsPreview Property. /// ////// /// /// public override void OnStartPrint(PrintDocument document, PrintEventArgs e) { base.OnStartPrint(document, e); this.document = document; pageNumber = 1; if (SystemInformation.UserInteractive) { backgroundThread = new BackgroundThread(this); // starts running & shows dialog automatically } // OnStartPrint does the security check... lots of // extra setup to make sure that we tear down // correctly... // try { underlyingController.OnStartPrint(document, e); } catch { if (backgroundThread != null) { backgroundThread.Stop(); } throw; } finally { if (backgroundThread != null && backgroundThread.canceled) { e.Cancel = true; } } } ////// Implements StartPrint by delegating to the underlying controller. /// ////// /// /// public override Graphics OnStartPage(PrintDocument document, PrintPageEventArgs e) { base.OnStartPage(document, e); if (backgroundThread != null) { backgroundThread.UpdateLabel(); } Graphics result = underlyingController.OnStartPage(document, e); if (backgroundThread != null && backgroundThread.canceled){ e.Cancel = true; } return result; } ////// Implements StartPage by delegating to the underlying controller. /// ////// /// /// public override void OnEndPage(PrintDocument document, PrintPageEventArgs e) { underlyingController.OnEndPage(document, e); if (backgroundThread != null && backgroundThread.canceled) { e.Cancel = true; } pageNumber++; base.OnEndPage(document, e); } ////// Implements EndPage by delegating to the underlying controller. /// ////// /// /// public override void OnEndPrint(PrintDocument document, PrintEventArgs e) { underlyingController.OnEndPrint(document, e); if (backgroundThread != null && backgroundThread.canceled) { e.Cancel = true; } if (backgroundThread != null) { backgroundThread.Stop(); } base.OnEndPrint(document, e); } private class BackgroundThread { private PrintControllerWithStatusDialog parent; private StatusDialog dialog; private Thread thread; internal bool canceled = false; private bool alreadyStopped = false; // Called from any thread internal BackgroundThread(PrintControllerWithStatusDialog parent) { this.parent = parent; // Calling Application.DoEvents() from within a paint event causes all sorts of problems, // so we need to put the dialog on its own thread. thread = new Thread(new ThreadStart(Run)); thread.SetApartmentState(ApartmentState.STA); thread.Start(); } // on correct thread [ UIPermission(SecurityAction.Assert, Window=UIPermissionWindow.AllWindows), SecurityPermission(SecurityAction.Assert, Flags=SecurityPermissionFlag.UnmanagedCode), ] private void Run() { // SECREVIEW : need all permissions to make the window not get adorned... // try { lock (this) { if (alreadyStopped) { return; } dialog = new StatusDialog(this, parent.dialogTitle); ThreadUnsafeUpdateLabel(); dialog.Visible = true; } if (!alreadyStopped) { Application.Run(dialog); } } finally { lock (this) { if (dialog != null) { dialog.Dispose(); dialog = null; } } } } // Called from any thread internal void Stop() { lock (this) { if (dialog != null && dialog.IsHandleCreated) { dialog.BeginInvoke(new MethodInvoker(dialog.Close)); return; } alreadyStopped = true; } } // on correct thread private void ThreadUnsafeUpdateLabel() { // "page {0} of {1}" dialog.label1.Text = SR.GetString(SR.PrintControllerWithStatusDialog_NowPrinting, parent.pageNumber, parent.document.DocumentName); } // Called from any thread internal void UpdateLabel() { if (dialog != null && dialog.IsHandleCreated) { dialog.BeginInvoke(new MethodInvoker(ThreadUnsafeUpdateLabel)); // Don't wait for a response } } } private class StatusDialog : Form { internal Label label1; private Button button1; private BackgroundThread backgroundThread; internal StatusDialog(BackgroundThread backgroundThread, string dialogTitle) { InitializeComponent(); this.backgroundThread = backgroundThread; this.Text = dialogTitle; this.MinimumSize = Size; } ////// Implements EndPrint by delegating to the underlying controller. /// ////// Tells whether the current resources for this dll have been /// localized for a RTL language. /// private static bool IsRTLResources { get { return SR.GetString(SR.RTL) != "RTL_False"; } } private void InitializeComponent() { if (IsRTLResources) { this.RightToLeft = RightToLeft.Yes; } this.label1 = new Label(); this.button1 = new Button(); label1.Location = new Point(8, 16); label1.TextAlign = ContentAlignment.MiddleCenter; label1.Size = new Size(240, 64); label1.TabIndex = 1; label1.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; button1.Size = new Size(75, 23); button1.TabIndex = 0; button1.Text = SR.GetString(SR.PrintControllerWithStatusDialog_Cancel); button1.Location = new Point(88, 88); button1.Anchor = AnchorStyles.Bottom; button1.Click += new EventHandler(button1_Click); this.AutoScaleDimensions = new Size(6, 13); this.AutoScaleMode = AutoScaleMode.Font; this.MaximizeBox = false; this.ControlBox = false; this.MinimizeBox = false; this.ClientSize = new Size(256, 122); this.CancelButton = button1; this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide; this.Controls.Add(label1); this.Controls.Add(button1); } private void button1_Click(object sender, System.EventArgs e) { button1.Enabled = false; label1.Text = SR.GetString(SR.PrintControllerWithStatusDialog_Canceling); backgroundThread.canceled = true; } } } } // 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
- DataServiceRequest.cs
- WindowsImpersonationContext.cs
- Keyboard.cs
- MultiPropertyDescriptorGridEntry.cs
- CustomUserNameSecurityTokenAuthenticator.cs
- UTF7Encoding.cs
- ExtensibleClassFactory.cs
- ItemDragEvent.cs
- Automation.cs
- InvalidateEvent.cs
- RepeatBehavior.cs
- PtsCache.cs
- PropertyChangedEventArgs.cs
- QilXmlWriter.cs
- UserMapPath.cs
- RSAPKCS1SignatureDeformatter.cs
- PerformanceCountersElement.cs
- Preprocessor.cs
- CTreeGenerator.cs
- StreamUpdate.cs
- SqlDependency.cs
- BoundColumn.cs
- PerformanceCounterCategory.cs
- tooltip.cs
- TextChange.cs
- Int32EqualityComparer.cs
- DataGridSortCommandEventArgs.cs
- DataServiceSaveChangesEventArgs.cs
- EntityProviderFactory.cs
- SqlBooleanMismatchVisitor.cs
- DesignerSerializationVisibilityAttribute.cs
- UserNamePasswordValidator.cs
- SingleStorage.cs
- DefinitionUpdate.cs
- AQNBuilder.cs
- LayoutDump.cs
- BuildProvidersCompiler.cs
- CalendarAutoFormatDialog.cs
- HMACSHA256.cs
- SessionEndedEventArgs.cs
- InvalidCardException.cs
- DSGeneratorProblem.cs
- FormsAuthenticationConfiguration.cs
- PEFileEvidenceFactory.cs
- Match.cs
- ValidationErrorInfo.cs
- DetailsViewInsertEventArgs.cs
- ButtonChrome.cs
- MailAddressParser.cs
- TextEditorTables.cs
- ListViewItemMouseHoverEvent.cs
- WebBrowser.cs
- HttpContext.cs
- ActiveXSite.cs
- ScriptReferenceEventArgs.cs
- DataPagerFieldCollection.cs
- XmlQualifiedNameTest.cs
- AdornerLayer.cs
- ObjectQuery.cs
- ConfigXmlText.cs
- QilInvokeLateBound.cs
- DirectionalLight.cs
- ClientSponsor.cs
- DataSourceCacheDurationConverter.cs
- PolicyStatement.cs
- TextServicesPropertyRanges.cs
- SettingsSection.cs
- HMACSHA512.cs
- TableCell.cs
- UnsafeNetInfoNativeMethods.cs
- ThemeableAttribute.cs
- Control.cs
- PropertyGeneratedEventArgs.cs
- NotImplementedException.cs
- CodeGotoStatement.cs
- TableCellCollection.cs
- ExtensibleClassFactory.cs
- SafeViewOfFileHandle.cs
- KeyPressEvent.cs
- DataServiceClientException.cs
- CodeNamespaceImportCollection.cs
- DrawingContextDrawingContextWalker.cs
- HttpCookieCollection.cs
- configsystem.cs
- Exceptions.cs
- WebBrowser.cs
- BmpBitmapDecoder.cs
- DesignerRegionMouseEventArgs.cs
- FormsAuthenticationConfiguration.cs
- StrokeNodeData.cs
- HiddenFieldPageStatePersister.cs
- CurrencyManager.cs
- FontFamilyValueSerializer.cs
- ManagementObject.cs
- UdpUtility.cs
- Duration.cs
- SafePointer.cs
- TraceSection.cs
- KeyFrames.cs
- SrgsGrammarCompiler.cs