using System.Data;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
class MyForm : Form
{
private Button button1;
private Font myFont;
private StreamReader read;
public MyForm()
{
button1 = new Button();
button1.Text = "打印";
button1.Location = new Point(10, 10);
button1.Size = new Size(80, 40);
button1.Click+=new EventHandler(button1_Click);
this.Controls.Add(button1);
}
private void button1_Click(object o, EventArgs eg)
{
read = new StreamReader("e:\\abc.txt");
PrintDocument dc = new PrintDocument();
dc.BeginPrint+=new PrintEventHandler(dc_BeginPrint);
dc.EndPrint+=new PrintEventHandler(dc_EndPrint);
dc.PrintPage+=new PrintPageEventHandler(dc_PrintPage);
dc.QueryPageSettings+=new QueryPageSettingsEventHandler(dc_QueryPageSettings);
dc.Print();
}
private void dc_BeginPrint(Object o,PrintEventArgs eg)
{
MessageBox.Show("BeginPrint");
}
private void dc_EndPrint(object o, PrintEventArgs eg)
{
read.Dispose();
MessageBox.Show("EndPrint");
}
private void dc_PrintPage(object o, PrintPageEventArgs eg)
{
try
{
myFont = new Font("Arial", 100);
float left = eg.MarginBounds.Left;//左边距
float top = eg.MarginBounds.Top;//上边距
float linesPerPage = eg.MarginBounds.Height / myFont.GetHeight(eg.Graphics);//每页能打印的总行数
int lines = 0;//记数器,记录已打印的行数
string TextLine = read.ReadLine();//将要打印的行内容
float yPos = 0;
while(TextLine!=null && lines
yPos = top + lines * myFont.GetHeight(eg.Graphics);//垂直位置
eg.Graphics.DrawString(TextLine, myFont, Brushes.Black, left, yPos);
//绘制文本
TextLine = read.ReadLine();//读取下一行内容;
lines++;
}//对一页的每一行进行打印;
if (TextLine != null)
eg.HasMorePages = true;
else
eg.HasMorePages = false;
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
}
}
private void dc_QueryPageSettings(object o, PrintEventArgs eg)
{
MessageBox.Show("QueryPageSetting");
}
}
class pragrame
{
public static void Main()
{
MyForm form = new MyForm();
form.ShowDialog();
}
}
沒有留言:
張貼留言