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;
}
}
}
.net 使用打印
最新推荐文章于 2018-03-09 11:26:31 发布