Printer Caps 3 : Print « GUI Windows Form « C# / C Sharp






Printer Caps 3

Printer Caps 3
/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury, 
   Zach Greenvoss, Shripad Kulkarni, Neil Whitlow

Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.Drawing.Printing;

namespace PrinterCaps3
{
    public class PrinterCaps3 : System.Windows.Forms.Form
    {
        private int m_nCurrPrinter;

        private System.Drawing.Printing.PrintDocument printDoc;
        private System.Windows.Forms.PrintPreviewDialog ppDialog;
        private System.Windows.Forms.Button btnPrint;
        private System.ComponentModel.Container components = null;

        public PrinterCaps3()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
    //      System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(PrinterCaps3));
            this.printDoc = new System.Drawing.Printing.PrintDocument();
            this.ppDialog = new System.Windows.Forms.PrintPreviewDialog();
            this.btnPrint = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // printDoc
            // 
            this.printDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDoc_PrintPage);
            // 
            // ppDialog
            // 
            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.Icon = ((System.Drawing.Icon)(resources.GetObject("ppDialog.Icon")));
            this.ppDialog.Location = new System.Drawing.Point(195, 22);
            this.ppDialog.MaximumSize = new System.Drawing.Size(0, 0);
            this.ppDialog.Name = "ppDialog";
            this.ppDialog.Opacity = 1;
            this.ppDialog.TransparencyKey = System.Drawing.Color.Empty;
            this.ppDialog.Visible = false;
            // 
            // btnPrint
            // 
            this.btnPrint.Location = new System.Drawing.Point(52, 28);
            this.btnPrint.Name = "btnPrint";
            this.btnPrint.TabIndex = 0;
            this.btnPrint.Text = "Print";
            this.btnPrint.Click += new System.EventHandler(this.btnPrint_Click);
            // 
            // PrinterCaps3
            // 
            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.Name = "PrinterCaps3";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Printer Caps";
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new PrinterCaps3());
        }

        private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            PrinterSettings pSettings = new PrinterSettings();
            Font printFont = new Font("Arial", 12);
   
            //Use the Margins
            int nTextPosY = e.MarginBounds.Top;
            int nTextPosX = e.MarginBounds.Left;
            int nHeight = (int)printFont.GetHeight(e.Graphics);
            
            //Height of a printer block 
            int nBlockHeight = 9 * nHeight;

            //Loop through using indexor now
            //Start with the previous index in m_nCurrPrinter
            for(int x = m_nCurrPrinter;x< PrinterSettings.InstalledPrinters.Count; x++)//each(string sPtr in PrinterSettings.InstalledPrinters)
            {
                pSettings.PrinterName = PrinterSettings.InstalledPrinters[x];
                if(pSettings.IsValid)
                {
                    //Ensure this printer block can fit on the page
                    if(nTextPosY + nBlockHeight < e.MarginBounds.Bottom)
                    {
                        //Print the caps of the printer
                        e.Graphics.DrawString(PrinterSettings.InstalledPrinters[x], 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);
                        
                    }
                    else
                    {
                        //Could nto fit block on the page - need more pages
                        m_nCurrPrinter = x;
                        e.HasMorePages = true;
                        return;
                    }
                }

            }

            //Last page if we reached here
            e.HasMorePages = false;
            m_nCurrPrinter = 0;
            return;
        }


        private void btnPrint_Click(object sender, System.EventArgs e)
        {
            //Set to defaults
            m_nCurrPrinter = 0;

            try
            {   
                ppDialog.Document = printDoc;
                ppDialog.ShowDialog();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}


           
       








Related examples in the same category

1.Control PrintControl Print
2.Simple Report PrinterSimple Report Printer
3.Begin Print
4.Define Print SettingsDefine Print Settings
5.Print Dialogs
6.Print EventsPrint Events
7.UI PrintUI Print
8.Basic Printing
9.Grid PrintingGrid Printing
10.Printer Caps 1
11.Printer Caps 2Printer Caps 2
12.Printer Caps 4Printer Caps 4
13.Printer Caps 5Printer Caps 5
14.Printer Caps 6Printer Caps 6