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

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » GUI Windows Form » PrintScreenshots 
Printer Caps 5
Printer Caps 5

/*
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;
using System.Drawing.Drawing2D;

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

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

        public PrinterCaps5()
        {
            InitializeComponent();
        }

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

        #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(PrinterCaps5));
            this.printDoc = new System.Drawing.Printing.PrintDocument();
            this.ppDialog = new System.Windows.Forms.PrintPreviewDialog();
            this.btnPrint = new System.Windows.Forms.Button();
            this.oPrintDialog = new System.Windows.Forms.PrintDialog();
            this.SuspendLayout();
            // 
            // printDoc
            // 
            this.printDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDoc_PrintPage);
            // 
            // ppDialog
            // 
            this.ppDialog.AutoScrollMargin = new System.Drawing.Size(00);
            this.ppDialog.AutoScrollMinSize = new System.Drawing.Size(00);
            this.ppDialog.ClientSize = new System.Drawing.Size(400300);
            this.ppDialog.Enabled = true;
    //      this.ppDialog.Icon = ((System.Drawing.Icon)(resources.GetObject("ppDialog.Icon")));
            this.ppDialog.Location = new System.Drawing.Point(14213);
            this.ppDialog.MaximumSize = new System.Drawing.Size(00);
            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(5228);
            this.btnPrint.Name = "btnPrint";
            this.btnPrint.TabIndex = 0;
            this.btnPrint.Text = "Print";
            this.btnPrint.Click += new System.EventHandler(this.btnPrint_Click);
            // 
            // oPrintDialog
            // 
            this.oPrintDialog.AllowSomePages = true;
            // 
            // PrinterCaps5
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(513);
            this.ClientSize = new System.Drawing.Size(17686);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.btnPrint});
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.Name = "PrinterCaps5";
            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 PrinterCaps5());
        }

        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 = * nHeight;

            if(e.PageSettings.PrinterSettings.PrintRange == PrintRange.SomePages)
            {
                while(m_nCurrPage < e.PageSettings.PrinterSettings.FromPage)
                {
                    //Move printer to next page block
                    m_nCurrPrinter += (int)(e.MarginBounds.Height / nBlockHeight);
                    m_nCurrPage++;
                    if(m_nCurrPrinter > PrinterSettings.InstalledPrinters.Count)
                        return;
                }

                if(m_nCurrPage > e.PageSettings.PrinterSettings.ToPage)
                {
                    //Don't print anything more
                    return;
                }
            }

            //Print Background Graphic
            LinearGradientBrush aBrush = 
                new LinearGradientBrush(e.MarginBounds,Color.FromArgb(100,Color.LightBlue),
                    Color.FromArgb(100,Color.Blue),LinearGradientMode.ForwardDiagonal);
            e.Graphics.FillRectangle(aBrush,e.MarginBounds);
                        
            //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 + (+ nHeight));
                        e.Graphics.DrawString("Is Default: " + pSettings.IsDefaultPrinter.ToString()
                            printFont,Brushes.Black, nTextPosX + 10, nTextPosY + (+ nHeight*2));
                        e.Graphics.DrawString("Is Plotter: " + pSettings.IsPlotter.ToString()
                            printFont,Brushes.Black, nTextPosX + 10, nTextPosY + (+ nHeight*3));
                        e.Graphics.DrawString("Landscape Angle: " + pSettings.LandscapeAngle.ToString()
                            printFont,Brushes.Black, nTextPosX + 10, nTextPosY + (+ nHeight*4));
                        e.Graphics.DrawString("Maximum Copies: " + pSettings.MaximumCopies.ToString()
                            printFont,Brushes.Black, nTextPosX + 10, nTextPosY + (+ nHeight*5));
                        e.Graphics.DrawString("Maximum Page: " + pSettings.MaximumPage.ToString()
                            printFont,Brushes.Black, nTextPosX + 10, nTextPosY + (+ nHeight*6));
                        e.Graphics.DrawString("Minimum Page: " + pSettings.MinimumPage.ToString()
                            printFont,Brushes.Black, nTextPosX + 10, nTextPosY + (+ nHeight*7));
                        e.Graphics.DrawString("Supports Color: " + pSettings.SupportsColor.ToString()
                            printFont,Brushes.Black, nTextPosX + 10, nTextPosY + (+ nHeight*8));
                        nTextPosY = nTextPosY + ((+ nHeight*8+ nHeight);

                        //Draw line after each
                        e.Graphics.DrawLine(System.Drawing.Pens.Black,nTextPosX,nTextPosY,e.MarginBounds.Right - 10,nTextPosY);
                        e.Graphics.FillEllipse(System.Drawing.Brushes.Black,e.MarginBounds.Right - 10,nTextPosY-5,10,10);
                        
                    }
                    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;
            m_nCurrPage = 1;

            oPrintDialog.Document = printDoc;
            if (oPrintDialog.ShowDialog() == DialogResult.OK)
            {
                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 3Printer Caps 3
13.Printer Caps 4Printer Caps 4
14.Printer Caps 6Printer Caps 6
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.