`
hududumo
  • 浏览: 238734 次
文章分类
社区版块
存档分类
最新评论

.net 使用打印

 
阅读更多
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing;

namespace printApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void pageSetupToolStripMenuItem_Click(object sender, EventArgs e)
        {
                printDialog1.ShowDialog();
         
        }

        private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                printDocument1.Print();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message,"打印出错",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }

        private void printPreviewToolStripMunuItem_Click(object sender, EventArgs e)
        {
            try
            {
                printPreviewDialog1.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void PrintDoucument1_Print(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;
            float linesPerPage = 0;
            float yPosition = 0;
            int count = 0;
            float leftMargin = e.MarginBounds.Left;
            float topMargin = e.MarginBounds.Top;
            string line = null;
            StringReader linenReader = new StringReader(richTextBox1.Text);
            Font PrintFont = this.richTextBox1.Font;
            SolidBrush myBrush = new SolidBrush(Color.Black);
            linesPerPage = e.MarginBounds.Height / PrintFont.GetHeight(g);
            while (count < linesPerPage && ((line = linenReader.ReadLine()) != null))
            {
                yPosition=topMargin+(count*PrintFont.GetHeight(g));
               g.DrawString(line,PrintFont,myBrush,leftMargin,yPosition,new StringFormat());
                count++;
            }
            if (line != null)
                e.HasMorePages = true;
            else
                e.HasMorePages = false;
        }

   

    
    }
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics