本篇文章為大家展示了如何進(jìn)行C#打印設(shè)置實(shí)現(xiàn)源碼的分析,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
創(chuàng)新互聯(lián)是一家專業(yè)提供夏河企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、成都h5網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為夏河眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站設(shè)計(jì)公司優(yōu)惠進(jìn)行中。
C#打印設(shè)置是如何在實(shí)際編程開(kāi)發(fā)中體現(xiàn)的呢?C#打印設(shè)置需要注意什么呢?C#打印設(shè)置常用屬性是如何進(jìn)行操作的呢?讓我們?cè)趯?shí)例中解決這些問(wèn)題吧:
C#打印設(shè)置實(shí)例代碼:
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace WindowsApplication1 { ////// C#打印設(shè)置之Form1 的摘要說(shuō)明。 /// public class Form1 : System.Windows.Forms.Form { private System.Drawing.Printing.PrintDocument pd; private PrintPreviewControl ppc; private PrintPreviewDialog ppd; private System.Windows.Forms.PrintDialog printDialog1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button3; private System.Windows.Forms.TextBox textBox1; String text=""; ////// C#打印設(shè)置之必需的設(shè)計(jì)器變量。 /// private System.ComponentModel.Container components = null; public Form1() { // // C#打印設(shè)置之Windows 窗體設(shè)計(jì)器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼 // } ////// C#打印設(shè)置之清理所有正在使用的資源。 /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗體設(shè)計(jì)器生成的代碼 ////// C#打印設(shè)置之設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改 /// 此方法的內(nèi)容。 /// private void InitializeComponent() { this.pd = new System.Drawing.Printing.PrintDocument(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.button3 = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.printDialog1 = new System.Windows.Forms.PrintDialog(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(32, 154); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 1; this.button1.Text = "開(kāi)始打印"; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Location = new System.Drawing.Point(120, 154); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(75, 23); this.button2.TabIndex = 2; this.button2.Text = "打印預(yù)覽"; this.button2.Click += new System.EventHandler(this.button2_Click); // // button3 // this.button3.Location = new System.Drawing.Point(208, 154); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(75, 23); this.button3.TabIndex = 3; this.button3.Text = "打印機(jī)設(shè)置"; this.button3.Click += new System.EventHandler(this.button3_Click); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(16, 16); this.textBox1.Multiline = true; this.textBox1.Name = "textBox1"; this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both; this.textBox1.Size = new System.Drawing.Size(270, 116); this.textBox1.TabIndex = 4; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(314, 199); this.Controls.Add(this.textBox1); this.Controls.Add(this.button3); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "打印窗體"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion ////// C#打印設(shè)置之應(yīng)用程序的主入口點(diǎn)。 /// [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) { //C#打印設(shè)置之創(chuàng)建實(shí)例 this.pd=new System.Drawing.Printing.PrintDocument(); this.ppc=new PrintPreviewControl(); this.ppd=new PrintPreviewDialog(); this.printDialog1=new PrintDialog(); //C#打印設(shè)置之觸發(fā)事件 this.pd.BeginPrint+=new System.Drawing.Printing.PrintEventHandler(pd_BeginPrint); this.pd.PrintPage+=new System.Drawing.Printing.PrintPageEventHandler(pd_PrintPage); } private void pd_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e) { //C#打印設(shè)置之設(shè)置橫向打印 this.pd.DefaultPageSettings.Landscape=true; //C#打印設(shè)置之設(shè)置彩色打印 this.pd.DefaultPageSettings.Color=true; //C#打印設(shè)置之設(shè)置打印紙張類型和大小 this.pd.DefaultPageSettings.PaperSize= new System.Drawing.Printing.PaperSize("A4",800,1100); } private void pd_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { //C#打印設(shè)置之獲取文本框的內(nèi)容繪制圖形傳到打印機(jī)打印 text=this.textBox1.Text; e.Graphics.DrawString(text, new Font("宋體",30, FontStyle.Regular), Brushes.Black, 0, 0); } private void button1_Click(object sender, System.EventArgs e) { //C#打印設(shè)置之開(kāi)始打印 this.pd.Print(); } private void button2_Click(object sender, System.EventArgs e) { //C#打印設(shè)置之設(shè)置打印預(yù)覽信息 ppc.Document=pd; ppc.Columns=2; ppc.Rows=2; ppc.Zoom=0.5; ppc.StartPage=1; //C#打印設(shè)置之顯示預(yù)覽 ppd.Document=pd; try { ppd.ShowDialog(); } catch (Exception excep) { MessageBox.Show(excep.Message, "打印出錯(cuò)", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void button3_Click(object sender, System.EventArgs e) { //C#打印設(shè)置之打印機(jī)設(shè)置 this.printDialog1.Document=pd; this.printDialog1.AllowSomePages=true; this.printDialog1.PrintToFile=false; //C#打印設(shè)置之確定打印機(jī)信息后開(kāi)始打印 if(this.printDialog1.ShowDialog()==DialogResult.OK) { try { this.pd.Print(); } catch (Exception excep) { MessageBox.Show(excep.Message, "打印出錯(cuò)", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } }
C#打印設(shè)置的相關(guān)實(shí)例以及介紹就向你講述到這里,很多具體的操作都在注釋中體現(xiàn),希望對(duì)你了解和學(xué)習(xí)C#打印設(shè)置有所幫助。
上述內(nèi)容就是如何進(jìn)行C#打印設(shè)置實(shí)現(xiàn)源碼的分析,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。