您好,登錄后才能下訂單哦!
本篇內容主要講解“實現C#打印文檔的步驟”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“實現C#打印文檔的步驟”吧!
C#打印文檔操作方式:
C#打印文檔1.新建一個項目
項目中有兩個form(Form1,Form2)
C#打印文檔2.在Form1中添加菜單
mainMenu1,一個richTextBox1(定義為Public),一個打印文檔控件PrintDocument,名稱為MyPrintDC。一個狀態欄名稱為myStatus。
菜單項有:
文件(mnFile){新建(mnNew),打開(mnOpen),保存(mnSave),頁面設置(mnPageSetup),打印預覽(mnPrintView),打印(mnPint),退出(mnClose)}
編輯(mnEdit){復制(mnCopy),剪切(mnCut),粘貼(mnPaste),查找(mnSearch)}
關于(mnAbout)
C#打印文檔3.在Form2中添加一個標簽:
查找內容,文本(txtSearch),命令按鈕(btnSearch) 查找一下個,命令按鈕(btnCancel)取消4.Form1中代碼:
C#打印文檔之加入引用:
using System.IO;
C#打印文檔之在控件定義階段中加入:
private StringReader myReader; private Form2 f;
C#打印文檔之Form1窗體的構造函數中:
f=new Form2(); f.Owner =this; f.Hide();
C#打印文檔之Form1窗體中定義一個方法CheckSave ()
private void CheckSave() { if (this.richTextBox1.Text!="") { if (MessageBox.Show("是否保存當前文件?","確認", MessageBoxButtons.OKCancel,MessageBoxIcon.Question)==DialogResult.OK) { this.myStatus.Text ="保存文件"; SaveFileDialog svfDialog=new SaveFileDialog(); svfDialog.Filter ="文本文件|*.*|富文本格式文件|*.rtf|所有文件|*.*"; if (svfDialog.ShowDialog()==DialogResult.OK) { this.richTextBox1.SaveFile(svfDialog.FileName, RichTextBoxStreamType.PlainText); } } } }
C#打印文檔之新建菜單(mnNew):
this.CheckSave(); this.richTextBox1.Clear(); this.myStatus.Text ="新建文件";
C#打印文檔之打開菜單(mnOpen):
this.CheckSave(); OpenFileDialog opfDialog=new OpenFileDialog (); opfDialog.Filter ="文本文件|*.*|富文本格式文件|*.rtf|所有文件|*.*"; if (opfDialog.ShowDialog()==DialogResult.OK) { this.richTextBox1.LoadFile( opfDialog.FileName,RichTextBoxStreamType.PlainText); } this.myStatus.Text ="打開文件";
C#打印文檔之保存菜單(mnSave):
this.myStatus.Text ="保存文件"; SaveFileDialog svfDialog=new SaveFileDialog(); svfDialog.Filter ="文本文件|*.*|富文本格式文件|*.rtf|所有文件|*.*"; if (svfDialog.ShowDialog()==DialogResult.OK) { this.richTextBox1.SaveFile(svfDialog.FileName, RichTextBoxStreamType.PlainText); }
C#打印文檔之控件的PrintPage事件代碼(MyPrintDC):
private void MyPrintDC_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { //打印文檔打印頁面事件代碼 this.myReader=new StringReader(this.richTextBox1.Text);//定義字符讀流 Graphics myGraphics=e.Graphics; Font myPrintFont=this.richTextBox1.Font; //計算一頁行數 float iLinePage= e.MarginBounds.Height/myPrintFont.GetHeight(myGraphics); int iLineNumber=0;//打印行數 float fyPosition=0;//打印時的縱坐標 float fMarginLeft=e.MarginBounds.Left;//紙頁面左邊界 float fMarginTop=e.MarginBounds.Top; string strLine=""; while ((iLineNumber<iLinePage)&&(strLine=myReader.ReadLine())!=null) { fyPosition=fMarginTop+iLineNumber*myPrintFont.GetHeight(myGraphics); myGraphics.DrawString(strLine,myPrintFont, new SolidBrush(Color.Black),fMarginLeft, fyPosition,new StringFormat()); iLineNumber++; } if (strLine!=null) { e.HasMorePages=true; } else { e.HasMorePages =false; } }
C#打印文檔之頁面設置菜單(mnPageSetup):
PageSetupDialog mypgDialog=new PageSetupDialog(); mypgDialog.Document =this.MyPrintDC; try { mypgDialog.ShowDialog(); } catch { this.MyPrintDC.PrintController.OnEndPrint( this.MyPrintDC,new System.Drawing.Printing.PrintEventArgs()); }
C#打印文檔之打印預覽菜單(mnPrintView):
PrintPreviewDialog myptViewDialog=new PrintPreviewDialog(); myptViewDialog.Document =this.MyPrintDC; try { myptViewDialog.ShowDialog(); } catch { this.MyPrintDC.PrintController.OnEndPrint( this.MyPrintDC,new System.Drawing.Printing.PrintEventArgs()); } 打印菜單(mnPrint): PrintDialog ptDialog=new PrintDialog(); ptDialog.Document =this.MyPrintDC; if (ptDialog.ShowDialog()==DialogResult.OK) { try { this.MyPrintDC.Print(); } catch { this.MyPrintDC.PrintController.OnEndPrint( this.MyPrintDC,new System.Drawing.Printing.PrintEventArgs()); } }
C#打印文檔之復制菜單(mnCopy):
if (this.richTextBox1.SelectedText!="") { Clipboard.SetDataObject(this.richTextBox1.SelectedText); this.mnCopy.Enabled =false; this.mnCut.Enabled =false; this.mnPaste.Enabled =true; }
C#打印文檔之剪切菜單(mnCut):
if (this.richTextBox1.SelectedText!="") { Clipboard.SetDataObject(this.richTextBox1.SelectedText); this.richTextBox1.SelectedText =""; this.mnCopy.Enabled =false; this.mnCut.Enabled =false; this.mnPaste.Enabled =true; }
C#打印文檔之粘貼菜單(mnPaste):
IDataObject d=Clipboard.GetDataObject(); this.richTextBox1.SelectedText =(string)d.GetData(DataFormats.Text);
C#打印文檔之查找菜單(mnSearch):
f.Show();
C#打印文檔之富文本框richTextBox1的文件選擇改變事件(SelectionChanged)
if (this.richTextBox1.SelectedText!="") { this.mnCut.Enabled =true; this.mnCopy.Enabled =true; } else { this.mnCut.Enabled =false; this.mnCopy.Enabled =false; this.mnPaste.Enabled =true; }
C#打印文檔4.Form2中的代碼:
定義一個整型變量:
private int findPlace=0;
命令按鈕"查找下一個"代碼
if (this.txtSearch.Text !="") { Form1 mainform=(Form1)this.Owner; if (mainform.richTextBox1.Text.Length>0) {if( (this.findPlace= mainform.richTextBox1.Text.IndexOf( this.txtSearch.Text,this.findPlace))==-1) { MessageBox.Show("沒有找到!"); this.findPlace =0; } else {mainform.richTextBox1.Select( this.findPlace,this.txtSearch.Text.Length); this.findPlace= this.findPlace+this.txtSearch.Text.Length; mainform.Activate(); } } }
命令按鈕"取消"代碼:
this.Hide(); this.Owner.Show();
到此,相信大家對“實現C#打印文檔的步驟”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。