C# 實(shí)現(xiàn)簡(jiǎn)單打印的實(shí)例代碼
主窗體代碼如下:
public partial class PrintFileForm : Form
{
public PrintFileForm()
{
InitializeComponent();
PrintFile prinFile = new PrintFile();
prinFile.Print();
}
}
打印文件類如下:
class PrintFile
{
StreamReader sr = null;
Font printFont = new Font("宋體", 12);
public void Print()
{
try
{
sr = new StreamReader(@"F:\Temp.txt");
try
{
PrintDocument printDoc = new PrintDocument();
printDoc.PrintPage += printDoc_PrintPage;
printDoc.Print();
}
finally
{
sr.Close();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
void printDoc_PrintPage(object sender, PrintPageEventArgs e)
{
string line = null;
//設(shè)置一頁(yè)的行數(shù)=打印區(qū)域的高度除以字體高度.
float pageLine = e.MarginBounds.Height / printFont.GetHeight(e.Graphics);
//循環(huán)打印每一行
for (int count = 0; count < pageLine && ((line=sr.ReadLine())!=null); count++)
{
float singleLine=e.MarginBounds.Top+(count*printFont.GetHeight(e.Graphics));
e.Graphics.DrawString(line, printFont, Brushes.Black, e.MarginBounds.Left, singleLine);
}
//判斷是否繼續(xù)打印
if (line != null)
e.HasMorePages = true;
else
e.HasMorePages = false;
}
}
相關(guān)文章
C#實(shí)現(xiàn)將程序鎖定到Win7任務(wù)欄的方法
這篇文章主要介紹了C#實(shí)現(xiàn)將程序鎖定到Win7任務(wù)欄的方法,涉及C#調(diào)用Shell類的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
C#中DropDownList動(dòng)態(tài)生成的方法
這篇文章主要介紹了C#中DropDownList動(dòng)態(tài)生成的方法,實(shí)例分析了C#中DropDownList的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03
c#利用webmail郵件系統(tǒng)發(fā)送郵件示例分享
在C#中發(fā)送郵件的方式有2種,一種是使用webmail方式進(jìn)行發(fā)送,另外一種就是采用netmail發(fā)送的方式,這篇文章介紹了c#使用webmail方式發(fā)送郵件示例,大家參考使用吧2014-01-01
使用C#對(duì)JSON進(jìn)行序列化和反序列化處理的兩種方法
本指南探討了如何使用 C# 編程語(yǔ)言進(jìn)行 JSON 序列化和反序列化,我們將介紹 .NET 生態(tài)系統(tǒng)中可用的兩個(gè)本機(jī)選項(xiàng),即命名空間和廣泛使用的 Newtonsoft.Json 庫(kù)(也稱為 Json.NET),需要的朋友可以參考下2024-06-06
C#創(chuàng)建一個(gè)小型Web Server(Socket實(shí)現(xiàn))
這篇文章主要介紹了關(guān)于C#利用Socket實(shí)現(xiàn)創(chuàng)建一個(gè)小型Web Server的相關(guān)資料,文中通過示例代碼介紹的很詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-02-02
C#中控制遠(yuǎn)程計(jì)算機(jī)的服務(wù)的方法
C#中控制遠(yuǎn)程計(jì)算機(jī)的服務(wù)的方法...2007-04-04

