国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C# 實現簡單打印的實例代碼

2020-01-24 03:34:05
字體:
來源:轉載
供稿:網友

主窗體代碼如下:

復制代碼 代碼如下:

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;
            //設置一頁的行數=打印區域的高度除以字體高度.
            float pageLine = e.MarginBounds.Height / printFont.GetHeight(e.Graphics);
            //循環打印每一行
            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);
            }
            //判斷是否繼續打印
            if (line != null)
                e.HasMorePages = true;
            else
                e.HasMorePages = false;

        }
    }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 商南县| 万全县| 清苑县| 忻城县| 盐池县| 揭阳市| 抚远县| 牙克石市| 闻喜县| 响水县| 湘乡市| 屯昌县| 庆阳市| 名山县| 宜章县| 南阳市| 寿阳县| 文昌市| 霍林郭勒市| 九台市| 葵青区| 顺昌县| 湘乡市| 汉源县| 礼泉县| 白城市| 五大连池市| 海伦市| 德化县| 保康县| 江北区| 山阳县| 玉溪市| 永川市| 宁化县| 西宁市| 海门市| 孟村| 分宜县| 宁远县| 江安县|