//------------------------------------------------------------------------------
/// <copyright from='1997' to='2001' company='Microsoft Corporation'>
/// 版权所有 (c) Microsoft Corporation。保留所有权利。
///
/// 此源代码仅作为 Microsoft 开发工具和/或联机文档
/// 的补充。有关 Microsoft 代码示例的详细信息,请
/// 参阅这些其他资料。
///
/// </copyright>
//------------------------------------------------------------------------------
namespace Microsoft.Samples.WinForms.Cs.PrintingExample5
{
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
public TextFilePrintDocument(StreamReader streamToPrint) : base ()
{
this.streamToPrint = streamToPrint ;
}
//Override OnBeginPrint to set up the font we are going to use
protected override void OnBeginPrint(PrintEventArgs ev)
{
base.OnBeginPrint(ev) ;
printFont = new Font("Arial", 10);
}
//Override the OnPrintPage to provide the printing logic for the document
protected override void OnPrintPage(PrintPageEventArgs ev)
{
base.OnPrintPage(ev) ;
//Work out the number of lines per page
//Use the MarginBounds on the event to do this
lpp = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics) ;
//Now iterate over the file printing out each line
//NOTE WELL: This assumes that a single line is not wider than the page width
//Check count first so that we don't read line that we won't print
while (count < lpp && ((line=streamToPrint.ReadLine()) != null))
{
yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
//Print Preview control will not work.
ev.Graphics.DrawString (line, printFont, Brushes.Black, leftMargin,
yPos, new StringFormat());
count++;
}
//If we have more lines then print another page
if (line != null)
ev.HasMorePages = true ;
else
ev.HasMorePages = false ;
}