Csharp/C Sharp/GUI Windows Form/Print Dialog
Версия от 15:31, 26 мая 2010; (обсуждение)
Steps through the common dialogs. Does nothing else useful
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// CmnDlgs.cs -- steps through the common dialogs. Does nothing else useful.
//
// Compile this program with the following command line:
// C:>csc CmnDlgs.cs
using System;
using System.Windows.Forms;
using System.Drawing.Printing;
namespace clsCommonDialogs
{
public class CommonDialogs
{
[STAThread]
static public void Main ()
{
// Create and display a Choose Color dialog box.
ColorDialog cd = new ColorDialog ();
cd.ShowDialog ();
cd.Dispose ();
// Create and display a Choose Font dialog box.
FontDialog fd = new FontDialog ();
fd.ShowDialog ();
fd.Dispose ();
// Create and display an Open File dialog box.
OpenFileDialog ofd = new OpenFileDialog ();
ofd.ShowDialog ();
ofd.Dispose ();
// Create and display a Save File dialog box.
SaveFileDialog sfd = new SaveFileDialog();
sfd.ShowDialog ();
sfd.Dispose ();
// Create and display a Page Setup dialog box.
PrintDocument printDoc = new PrintDocument();
PageSetupDialog psd = new PageSetupDialog ();
psd.Document = printDoc;
psd.ShowDialog ();
psd.Dispose ();
// Create and display an Print dialog box.
PrintDialog pd = new PrintDialog ();
pd.Document = printDoc;
pd.ShowDialog ();
pd.Dispose ();
// Create and display an Print Preview File dialog box.
// This dialog is not a part of the common dialog library.
PrintPreviewDialog ppd = new PrintPreviewDialog ();
ppd.ShowDialog ();
ppd.Dispose ();
printDoc.Dispose ();
}
}
}
Using PrintPreviewDialog
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Printing;
public class frmMain : System.Windows.Forms.Form {
private System.Drawing.Printing.PrintDocument printDoc;
private System.Windows.Forms.PrintPreviewDialog ppDialog;
private System.Windows.Forms.Button btnPrint;
public frmMain() {
this.printDoc = new System.Drawing.Printing.PrintDocument();
this.ppDialog = new System.Windows.Forms.PrintPreviewDialog();
this.btnPrint = new System.Windows.Forms.Button();
this.SuspendLayout();
this.printDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDoc_PrintPage);
this.ppDialog.AutoScrollMargin = new System.Drawing.Size(0, 0);
this.ppDialog.AutoScrollMinSize = new System.Drawing.Size(0, 0);
this.ppDialog.ClientSize = new System.Drawing.Size(400, 300);
this.ppDialog.Enabled = true;
this.ppDialog.Location = new System.Drawing.Point(195, 22);
this.ppDialog.MaximumSize = new System.Drawing.Size(0, 0);
this.ppDialog.Opacity = 1;
this.ppDialog.TransparencyKey = System.Drawing.Color.Empty;
this.ppDialog.Visible = false;
this.btnPrint.Location = new System.Drawing.Point(52, 28);
this.btnPrint.Text = "Print";
this.btnPrint.Click += new System.EventHandler(this.btnPrint_Click);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(176, 86);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.btnPrint});
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.ResumeLayout(false);
}
[STAThread]
static void Main() {
Application.Run(new frmMain());
}
private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) {
PrinterSettings pSettings = new PrinterSettings();
Font printFont = new Font("Arial", 12);
int nTextPosY = e.MarginBounds.Top;
int nTextPosX = e.MarginBounds.Left;
int nHeight = (int)printFont.GetHeight(e.Graphics);
foreach (string sPtr in PrinterSettings.InstalledPrinters) {
pSettings.PrinterName = sPtr;
if (pSettings.IsValid) {
e.Graphics.DrawString(sPtr, printFont, Brushes.Black, nTextPosX, nTextPosY + 5);
e.Graphics.DrawString("Can Duplex: " + pSettings.CanDuplex.ToString(),
printFont, Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight));
e.Graphics.DrawString("Is Default: " + pSettings.IsDefaultPrinter.ToString(),
printFont, Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight * 2));
e.Graphics.DrawString("Is Plotter: " + pSettings.IsPlotter.ToString(),
printFont, Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight * 3));
e.Graphics.DrawString("Landscape Angle: " + pSettings.LandscapeAngle.ToString(),
printFont, Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight * 4));
e.Graphics.DrawString("Maximum Copies: " + pSettings.MaximumCopies.ToString(),
printFont, Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight * 5));
e.Graphics.DrawString("Maximum Page: " + pSettings.MaximumPage.ToString(),
printFont, Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight * 6));
e.Graphics.DrawString("Minimum Page: " + pSettings.MinimumPage.ToString(),
printFont, Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight * 7));
e.Graphics.DrawString("Supports Color: " + pSettings.SupportsColor.ToString(),
printFont, Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight * 8));
nTextPosY = nTextPosY + ((5 + nHeight * 8) + nHeight);
}
}
}
private void btnPrint_Click(object sender, System.EventArgs e) {
ppDialog.Document = printDoc;
ppDialog.ShowDialog();
}
}