Mega Code Archive

 
Categories / C# / GUI Windows Form
 

Using PrintPreviewDialog

using System; using System.Drawing; using System.Collections; using System.ComponentModel; 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();     } }