|
To assist you with actually designing what you want to display in the preview area, the PrintDocument class fires an event named PrintPage. This event is of type PrintPageEventArgs. The PrintPageEventArgs class is equipped with a property named Graphics, which is of type Graphics. You can then use your knowledge of the Graphics class to create or design the preview. Here is an example: using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;
public class Exercise : Form
{
PrintDialog dlgPrint;
PrintDocument docPrint;
PageSetupDialog dlgPageSetup;
PrintPreviewDialog dlgPrintPreview;
Button btnPrint, btnPageSetup, btnPrintPreview;
public Exercise()
{
InitializeComponent();
}
void InitializeComponent()
{
btnPrint = new Button();
btnPrint.Width = 120;
btnPrint.Text = "&Print...";
btnPrint.Location = new Point(12, 12);
btnPrint.Click += new EventHandler(btnPrintDocumentClick);
btnPageSetup = new Button();
btnPageSetup.Width = 120;
btnPageSetup.Text = "P&age Setup...";
btnPageSetup.Location = new Point(12, 44);
btnPageSetup.Click += new EventHandler(btnPageSetupClick);
btnPrintPreview = new Button();
btnPrintPreview.Width = 120;
btnPrintPreview.Text = "Print Pre&view...";
btnPrintPreview.Location = new Point(12, 76);
btnPrintPreview.Click += new EventHandler(btnPrintPreviewClick);
dlgPrint = new PrintDialog();
docPrint = new PrintDocument();
dlgPrint.Document = docPrint;
docPrint.PrintPage += new PrintPageEventHandler(docPrintPrintPage);
dlgPageSetup = new PageSetupDialog();
dlgPageSetup.Document = docPrint;
PageSettings psSetup = new PageSettings();
psSetup.Margins = new Margins(160, 120, 80, 200);
psSetup.Landscape = true;
dlgPageSetup.PageSettings = psSetup;
dlgPrintPreview = new PrintPreviewDialog();
dlgPrintPreview.Document = docPrint;
Controls.Add(btnPrint);
Controls.Add(btnPageSetup);
Controls.Add(btnPrintPreview);
Text = "Printing";
}
private void docPrintPrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawString("Picture Preview", new Font("Times New Roman", 28, FontStyle.Bold), Brushes.Red, 60F, 80F);
e.Graphics.DrawLine(new Pen(Color.Black, 2), 60, 120, 700, 120);
e.Graphics.DrawImage(Image.FromFile(@"C:\Exercise\Student.jpg"), 60F, 200F);
}
void btnPrintDocumentClick(object sender, EventArgs e)
{
if (dlgPrint.ShowDialog() == System.Windows.Forms.DialogResult.OK)
docPrint.Print();
}
void btnPageSetupClick(object sender, EventArgs e)
{
dlgPageSetup.ShowDialog();
}
void btnPrintPreviewClick(object sender, EventArgs e)
{
dlgPrintPreview.ShowDialog();
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Exercise());
}
}
On our computer, this produced:
To print the contents of the preview area, the user can
click the Print button
By default, when the print preview window appears to the user, it assumes some default dimensions that may make it small. Because it is derived from the Form class, you can maximize it if you want. Here is an example: void PreviewDocumentClick(object sender, EventArgs e)
{
dlgPrintPreview.WindowState = FormWindowState.Maximized;
dlgPrintPreview.ShowDialog();
}
If the print preview is not maximized, the content of the preview area may appear (too) small for the user, especially if it is made of text. To enlarge it, the user has two alternatives. If the user maximizes the window, the preview area would also be enlarged and the content would be easier to see. As an alternative, the user can click the arrow of the Zoom button. This would display a list of the zoom rates:
The user can then click one of the values. |
|||||