PrintDocument.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. using System;
  12. using System.ComponentModel;
  13. namespace System.Drawing.Printing
  14. {
  15. [DefaultEvent ("PrintPage"), DefaultProperty ("DocumentName")]
  16. [ToolboxItemFilter ("System.Drawing.Printing", ToolboxItemFilterType.Allow)]
  17. [DesignerCategory ("Component")]
  18. public class PrintDocument : System.ComponentModel.Component
  19. {
  20. private PageSettings defaultpagesettings;
  21. private PrinterSettings printersettings;
  22. private PrintController printcontroller;
  23. private string documentname;
  24. #if !(NET_1_0)
  25. private bool originAtMargins = false; // .NET V1.1 Beta
  26. #endif
  27. public PrintDocument() {
  28. documentname = "document"; //offical default.
  29. defaultpagesettings = new PageSettings(); // use default values of default printer
  30. printersettings = new PrinterSettings(); // use default values
  31. printcontroller = new StandardPrintController();
  32. }
  33. // properties
  34. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  35. [Browsable (false)]
  36. [SRDescription ("The settings for the current page.")]
  37. public PageSettings DefaultPageSettings{
  38. get{
  39. return defaultpagesettings;
  40. }
  41. set{
  42. defaultpagesettings = value;
  43. }
  44. }
  45. // Name of the document, not the file!
  46. [DefaultValue ("document")]
  47. [SRDescription ("The name of the document.")]
  48. public string DocumentName{
  49. get{
  50. return documentname;
  51. }
  52. set{
  53. documentname = value;
  54. }
  55. }
  56. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  57. [Browsable (false)]
  58. [SRDescription ("The print controller object.")]
  59. public PrintController PrintController{
  60. get{
  61. return printcontroller;
  62. }
  63. set{
  64. printcontroller = value;
  65. }
  66. }
  67. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  68. [Browsable (false)]
  69. [SRDescription ("The current settings for the active printer.")]
  70. public PrinterSettings PrinterSettings{
  71. get{
  72. return printersettings;
  73. }
  74. set{
  75. printersettings = value;
  76. }
  77. }
  78. #if !(NET_1_0)
  79. [DefaultValue (false)]
  80. [SRDescription ("Determines if the origin is set at the specified margins.")]
  81. public bool OriginAtMargins{
  82. get{
  83. return originAtMargins;
  84. }
  85. set{
  86. originAtMargins = value;
  87. }
  88. }
  89. #endif
  90. // methods
  91. public void Print(){
  92. PrintEventArgs printArgs = new PrintEventArgs();
  93. this.OnBeginPrint(printArgs);
  94. if (printArgs.Cancel)
  95. return;
  96. PrintController.OnStartPrint(this, printArgs);
  97. if (printArgs.Cancel)
  98. return;
  99. // while there is more pages
  100. PrintPageEventArgs printPageArgs;
  101. do
  102. {
  103. PageSettings pageSettings = DefaultPageSettings.Clone() as PageSettings;
  104. this.OnQueryPageSettings(new QueryPageSettingsEventArgs(pageSettings));
  105. printPageArgs = new PrintPageEventArgs(
  106. null,
  107. pageSettings.Bounds,
  108. new Rectangle(0, 0, pageSettings.PaperSize.Width, pageSettings.PaperSize.Height),
  109. pageSettings);
  110. Graphics g = PrintController.OnStartPage(this, printPageArgs);
  111. // assign Graphics in printPageArgs
  112. printPageArgs.SetGraphics(g);
  113. if (!printPageArgs.Cancel)
  114. this.OnPrintPage(printPageArgs);
  115. PrintController.OnEndPage(this, printPageArgs);
  116. if (printPageArgs.Cancel)
  117. break;
  118. } while (printPageArgs.HasMorePages);
  119. this.OnEndPrint(printArgs);
  120. PrintController.OnEndPrint(this, printArgs);
  121. }
  122. public override string ToString(){
  123. return "[PrintDocument " + this.DocumentName + "]";
  124. }
  125. // events
  126. protected virtual void OnBeginPrint(PrintEventArgs e){
  127. //fire the event
  128. if (BeginPrint != null)
  129. BeginPrint(this, e);
  130. }
  131. protected virtual void OnEndPrint(PrintEventArgs e){
  132. //fire the event
  133. if (EndPrint != null)
  134. EndPrint(this, e);
  135. }
  136. protected virtual void OnPrintPage(PrintPageEventArgs e){
  137. //fire the event
  138. if (PrintPage != null)
  139. PrintPage(this, e);
  140. }
  141. protected virtual void OnQueryPageSettings(QueryPageSettingsEventArgs e){
  142. //fire the event
  143. if (QueryPageSettings != null)
  144. QueryPageSettings(this, e);
  145. }
  146. [SRDescription ("Raised when printing begins")]
  147. public event PrintEventHandler BeginPrint;
  148. [SRDescription ("Raised when printing ends")]
  149. public event PrintEventHandler EndPrint;
  150. [SRDescription ("Raised when printing of a new page begins")]
  151. public event PrintPageEventHandler PrintPage;
  152. [SRDescription ("Raised before printing of a new page begins")]
  153. public event QueryPageSettingsEventHandler QueryPageSettings;
  154. }
  155. }