C#打印设置实现源码详解

开发 后端
C#打印设置是如何在实际编程开发中体现的呢?这里向你讲述了C#打印设置需要注意什么以及C#打印设置常用属性是如何进行操作的等等内容。

C#打印设置是如何在实际编程开发中体现的呢?C#打印设置需要注意什么呢?C#打印设置常用属性是如何进行操作的呢?让我们在实例中解决这些问题吧:

C#打印设置实例代码:

  1. using System;  
  2. using System.Drawing;  
  3. using System.Collections;  
  4. using System.ComponentModel;  
  5. using System.Windows.Forms;  
  6. using System.Data;  
  7.  
  8. namespace WindowsApplication1  
  9. {  
  10.  /// <summary>  
  11.  /// C#打印设置之Form1 的摘要说明。  
  12.  /// </summary>  
  13.  public class Form1 : System.Windows.Forms.Form  
  14.  {  
  15. private System.Drawing.Printing.PrintDocument pd;  
  16. private PrintPreviewControl ppc;  
  17. private PrintPreviewDialog ppd;  
  18. private System.Windows.Forms.PrintDialog printDialog1;  
  19. private System.Windows.Forms.Button button1;  
  20. private System.Windows.Forms.Button button2;  
  21. private System.Windows.Forms.Button button3;  
  22. private System.Windows.Forms.TextBox textBox1;  
  23.  
  24. String text="";  
  25. /// <summary>  
  26. /// C#打印设置之必需的设计器变量。  
  27. /// </summary>  
  28. private System.ComponentModel.Container components = null;  
  29.  
  30. public Form1()  
  31. {  
  32.  //  
  33.  // C#打印设置之Windows 窗体设计器支持所必需的  
  34.  //  
  35.  InitializeComponent();  
  36.  
  37.  //  
  38.  // TODO: 在 InitializeComponent 调用后添加任何构造函数代码  
  39.  //  
  40. }  
  41.  
  42. /// <summary>  
  43. /// C#打印设置之清理所有正在使用的资源。  
  44. /// </summary>  
  45. protected override void Dispose( bool disposing )  
  46. {  
  47.  if( disposing )  
  48.  {  
  49. if (components != null)   
  50. {  
  51.  components.Dispose();  
  52. }  
  53.  }  
  54.  base.Dispose( disposing );  
  55. }  
  56.  
  57. #region Windows 窗体设计器生成的代码  
  58. /// <summary>  
  59. /// C#打印设置之设计器支持所需的方法 - 不要使用代码编辑器修改  
  60. /// 此方法的内容。  
  61. /// </summary>  
  62. private void InitializeComponent()  
  63. {  
  64. this.pd = new System.Drawing.Printing.PrintDocument();  
  65. this.button1 = new System.Windows.Forms.Button();  
  66. this.button2 = new System.Windows.Forms.Button();  
  67. this.button3 = new System.Windows.Forms.Button();  
  68. this.textBox1 = new System.Windows.Forms.TextBox();  
  69. this.printDialog1 = new System.Windows.Forms.PrintDialog();  
  70. this.SuspendLayout();  
  71. //   
  72. // button1  
  73. //   
  74. this.button1.Location = new System.Drawing.Point(32, 154);  
  75. this.button1.Name = "button1";  
  76. this.button1.Size = new System.Drawing.Size(75, 23);  
  77. this.button1.TabIndex = 1;  
  78. this.button1.Text = "开始打印";  
  79. this.button1.Click += new System.EventHandler(this.button1_Click);  
  80. //   
  81. // button2  
  82. //   
  83. this.button2.Location = new System.Drawing.Point(120, 154);  
  84. this.button2.Name = "button2";  
  85. this.button2.Size = new System.Drawing.Size(75, 23);  
  86. this.button2.TabIndex = 2;  
  87. this.button2.Text = "打印预览";  
  88. this.button2.Click += new System.EventHandler(this.button2_Click);  
  89. //   
  90. // button3  
  91. //   
  92. this.button3.Location = new System.Drawing.Point(208, 154);  
  93. this.button3.Name = "button3";  
  94. this.button3.Size = new System.Drawing.Size(75, 23);  
  95. this.button3.TabIndex = 3;  
  96. this.button3.Text = "打印机设置";  
  97. this.button3.Click += new System.EventHandler(this.button3_Click);  
  98. //   
  99. // textBox1  
  100. //   
  101. this.textBox1.Location = new System.Drawing.Point(16, 16);  
  102. this.textBox1.Multiline = true;  
  103. this.textBox1.Name = "textBox1";  
  104. this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;  
  105. this.textBox1.Size = new System.Drawing.Size(270, 116);  
  106. this.textBox1.TabIndex = 4;  
  107. //   
  108. // Form1  
  109. //   
  110. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);  
  111. this.ClientSize = new System.Drawing.Size(314, 199);  
  112. this.Controls.Add(this.textBox1);  
  113. this.Controls.Add(this.button3);  
  114. this.Controls.Add(this.button2);  
  115. this.Controls.Add(this.button1);  
  116. this.Name = "Form1";  
  117. this.Text = "打印窗体";  
  118. this.Load += new System.EventHandler(this.Form1_Load);  
  119. this.ResumeLayout(false);  
  120. this.PerformLayout();  
  121.  
  122. }  
  123. #endregion  
  124.  
  125. /// <summary>  
  126. /// C#打印设置之应用程序的主入口点。  
  127. /// </summary>  
  128. [STAThread]  
  129. static void Main()   
  130. {  
  131.  Application.Run(new Form1());  
  132. }  
  133.  
  134. private void Form1_Load(object sender, System.EventArgs e)  
  135. {  
  136.  //C#打印设置之创建实例  
  137.  this.pd=new System.Drawing.Printing.PrintDocument();  
  138.  this.ppc=new PrintPreviewControl();  
  139.  this.ppd=new PrintPreviewDialog();  
  140.  this.printDialog1=new PrintDialog();  
  141.  
  142.  //C#打印设置之触发事件  
  143.  this.pd.BeginPrint+=new System.Drawing.Printing.PrintEventHandler(pd_BeginPrint);  
  144.  this.pd.PrintPage+=new System.Drawing.Printing.PrintPageEventHandler(pd_PrintPage);  
  145.    
  146.  
  147. }  
  148.  
  149. private void pd_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)  
  150. {   
  151.  //C#打印设置之设置横向打印  
  152.  this.pd.DefaultPageSettings.Landscape=true;  
  153.  
  154.  //C#打印设置之设置彩色打印  
  155.  this.pd.DefaultPageSettings.Color=true;  
  156.  
  157.  //C#打印设置之设置打印纸张类型和大小  
  158.  this.pd.DefaultPageSettings.PaperSize=  
  159. new System.Drawing.Printing.PaperSize("A4",800,1100);  
  160.  
  161.  
  162. }  
  163.  
  164. private void pd_PrintPage(object sender,   
  165. System.Drawing.Printing.PrintPageEventArgs e)  
  166. {  
  167.  //C#打印设置之获取文本框的内容绘制图形传到打印机打印  
  168.  text=this.textBox1.Text;  
  169.  e.Graphics.DrawString(text,   
  170. new Font("宋体",30, FontStyle.Regular),   
  171. Brushes.Black, 0, 0);  
  172.    
  173. }  
  174.  
  175. private void button1_Click(object sender,   
  176. System.EventArgs e)  
  177. {  
  178.  //C#打印设置之开始打印  
  179. this.pd.Print();  
  180.    
  181. }  
  182.  
  183. private void button2_Click(object sender,   
  184. System.EventArgs e)  
  185. {  
  186.  //C#打印设置之设置打印预览信息  
  187.  ppc.Document=pd;  
  188.  ppc.Columns=2;  
  189.  ppc.Rows=2;  
  190.  ppc.Zoom=0.5;  
  191.  ppc.StartPage=1;  
  192.    
  193.  //C#打印设置之显示预览  
  194.  ppd.Document=pd;  
  195. try 
  196. {  
  197. ppd.ShowDialog();  
  198. }  
  199. catch (Exception excep)  
  200. {  
  201. MessageBox.Show(excep.Message,   
  202. "打印出错", MessageBoxButtons.OK,   
  203. MessageBoxIcon.Error);  
  204. }  
  205.  
  206.    
  207. }  
  208.  
  209. private void button3_Click(object sender,   
  210. System.EventArgs e)  
  211. {  
  212.  //C#打印设置之打印机设置  
  213.  this.printDialog1.Document=pd;  
  214.  this.printDialog1.AllowSomePages=true;  
  215.  this.printDialog1.PrintToFile=false;  
  216.  //C#打印设置之确定打印机信息后开始打印  
  217.  if(this.printDialog1.ShowDialog()==DialogResult.OK)  
  218.  {  
  219. try 
  220. {  
  221. this.pd.Print();  
  222. }  
  223. catch (Exception excep)  
  224. {  
  225. MessageBox.Show(excep.Message,   
  226. "打印出错", MessageBoxButtons.OK,  
  227.  MessageBoxIcon.Error);   
  228. }  
  229.  }  
  230. }  
  231.  }  
  232. }  
  233.  

C#打印设置的相关实例以及介绍就向你讲述到这里,很多具体的操作都在注释中体现,希望对你了解和学习C#打印设置有所帮助。

【编辑推荐】

  1. 实现C#打印窗体实例详解
  2. 实现C#打印文档实例详解
  3. C#打印文本文件实例详解
  4. C#打印设置实例解析
  5. C#Lpt端口打印类的操作浅析
责任编辑:仲衡 来源: CSDN博客
相关推荐

2009-08-26 13:41:58

C#打印源码

2009-08-26 11:32:37

C#打印文档

2009-08-26 11:07:36

C#打印窗体

2009-08-26 09:22:44

C#实现打印功能

2009-08-26 09:54:45

C#打印预览C#打印

2009-08-26 12:14:44

C#打印设置

2009-08-26 13:22:24

C#打印程序

2009-08-26 10:43:14

C#实现打印功能

2009-08-25 18:04:30

C#实现Singlet

2009-08-31 16:23:13

C#接口

2009-09-09 18:50:23

C# 加密RSA

2009-08-26 11:53:56

C#打印文本文件

2009-09-09 18:57:26

C# 加密TripleDES

2009-08-25 17:43:17

C#串口监听

2009-08-25 10:44:50

C#实现多语言

2011-05-20 16:07:29

C#

2009-08-21 10:13:02

C#异步初步

2009-08-20 16:33:44

Socket异步通讯

2009-09-09 12:55:59

C# TextBox事

2009-09-03 14:55:56

C#实现DataGri
点赞
收藏

51CTO技术栈公众号