PrintDocument.cs 4.5 KB

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