PrintDocument.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // System.Drawing.PrintDocument.cs
  3. //
  4. // Authors:
  5. // Dennis Hayes ([email protected])
  6. // Herve Poussineau ([email protected])
  7. // Andreas Nahr ([email protected])
  8. //
  9. // (C) 2002 Ximian, Inc
  10. //
  11. //
  12. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System;
  34. using System.ComponentModel;
  35. namespace System.Drawing.Printing
  36. {
  37. [DefaultEvent ("PrintPage"), DefaultProperty ("DocumentName")]
  38. [ToolboxItemFilter ("System.Drawing.Printing", ToolboxItemFilterType.Allow)]
  39. public class PrintDocument : System.ComponentModel.Component
  40. {
  41. private PageSettings defaultpagesettings;
  42. private PrinterSettings printersettings;
  43. private PrintController printcontroller;
  44. private string documentname;
  45. #if !(NET_1_0)
  46. private bool originAtMargins = false; // .NET V1.1 Beta
  47. #endif
  48. public PrintDocument() {
  49. documentname = "document"; //offical default.
  50. defaultpagesettings = new PageSettings(); // use default values of default printer
  51. printersettings = new PrinterSettings(); // use default values
  52. printcontroller = new StandardPrintController();
  53. }
  54. // properties
  55. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  56. [Browsable (false)]
  57. [SRDescription ("The settings for the current page.")]
  58. public PageSettings DefaultPageSettings{
  59. get{
  60. return defaultpagesettings;
  61. }
  62. set{
  63. defaultpagesettings = value;
  64. }
  65. }
  66. // Name of the document, not the file!
  67. [DefaultValue ("document")]
  68. [SRDescription ("The name of the document.")]
  69. public string DocumentName{
  70. get{
  71. return documentname;
  72. }
  73. set{
  74. documentname = value;
  75. }
  76. }
  77. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  78. [Browsable (false)]
  79. [SRDescription ("The print controller object.")]
  80. public PrintController PrintController{
  81. get{
  82. return printcontroller;
  83. }
  84. set{
  85. printcontroller = value;
  86. }
  87. }
  88. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  89. [Browsable (false)]
  90. [SRDescription ("The current settings for the active printer.")]
  91. public PrinterSettings PrinterSettings{
  92. get{
  93. return printersettings;
  94. }
  95. set{
  96. printersettings = value;
  97. }
  98. }
  99. #if !(NET_1_0)
  100. [DefaultValue (false)]
  101. [SRDescription ("Determines if the origin is set at the specified margins.")]
  102. public bool OriginAtMargins{
  103. get{
  104. return originAtMargins;
  105. }
  106. set{
  107. originAtMargins = value;
  108. }
  109. }
  110. #endif
  111. // methods
  112. public void Print(){
  113. PrintEventArgs printArgs = new PrintEventArgs();
  114. this.OnBeginPrint(printArgs);
  115. if (printArgs.Cancel)
  116. return;
  117. PrintController.OnStartPrint(this, printArgs);
  118. if (printArgs.Cancel)
  119. return;
  120. Graphics g = null;
  121. // while there are more pages
  122. PrintPageEventArgs printPageArgs;
  123. do
  124. {
  125. PageSettings pageSettings = DefaultPageSettings.Clone() as PageSettings;
  126. this.OnQueryPageSettings(new QueryPageSettingsEventArgs(pageSettings));
  127. printPageArgs = new PrintPageEventArgs(
  128. null,
  129. pageSettings.Bounds,
  130. new Rectangle(0, 0, pageSettings.PaperSize.Width, pageSettings.PaperSize.Height),
  131. pageSettings);
  132. if (g == null) {
  133. g = Graphics.FromHdc (printArgs.GraphicsContext.Hdc);
  134. printArgs.GraphicsContext.Graphics = g;
  135. }
  136. printPageArgs.GraphicsContext = printArgs.GraphicsContext;
  137. PrintController.OnStartPage(this, printPageArgs);
  138. // assign Graphics in printPageArgs
  139. printPageArgs.SetGraphics(g);
  140. if (!printPageArgs.Cancel)
  141. this.OnPrintPage(printPageArgs);
  142. PrintController.OnEndPage(this, printPageArgs);
  143. if (printPageArgs.Cancel)
  144. break;
  145. } while (printPageArgs.HasMorePages);
  146. this.OnEndPrint(printArgs);
  147. PrintController.OnEndPrint(this, printArgs);
  148. }
  149. public override string ToString(){
  150. return "[PrintDocument " + this.DocumentName + "]";
  151. }
  152. // events
  153. protected virtual void OnBeginPrint(PrintEventArgs e){
  154. //fire the event
  155. if (BeginPrint != null)
  156. BeginPrint(this, e);
  157. }
  158. protected virtual void OnEndPrint(PrintEventArgs e){
  159. //fire the event
  160. if (EndPrint != null)
  161. EndPrint(this, e);
  162. }
  163. protected virtual void OnPrintPage(PrintPageEventArgs e){
  164. //fire the event
  165. if (PrintPage != null)
  166. PrintPage(this, e);
  167. }
  168. protected virtual void OnQueryPageSettings(QueryPageSettingsEventArgs e){
  169. //fire the event
  170. if (QueryPageSettings != null)
  171. QueryPageSettings(this, e);
  172. }
  173. [SRDescription ("Raised when printing begins")]
  174. public event PrintEventHandler BeginPrint;
  175. [SRDescription ("Raised when printing ends")]
  176. public event PrintEventHandler EndPrint;
  177. [SRDescription ("Raised when printing of a new page begins")]
  178. public event PrintPageEventHandler PrintPage;
  179. [SRDescription ("Raised before printing of a new page begins")]
  180. public event QueryPageSettingsEventHandler QueryPageSettings;
  181. }
  182. }