Csharp/C Sharp by API/System.Drawing.Printing/PrinterSettings

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

PrinterSettings.CanDuplex

  
/*
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.ruponentModel;
using System.Windows.Forms;
using System.Data;
//Need this namespace to access the Printing functionality
using System.Drawing.Printing;
namespace PrinterCaps2
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class PrinterCaps2 : System.Windows.Forms.Form
    {
        private System.Drawing.Printing.PrintDocument printDoc;
        private System.Windows.Forms.PrintPreviewDialog ppDialog;
        private System.Windows.Forms.Button btnPrint;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ruponentModel.Container components = null;
        public PrinterCaps2()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }
        /// <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(PrinterCaps2));
            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);
            // 
            // PrinterCaps2
            // 
            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 = "PrinterCaps2";
            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 PrinterCaps2());
        }
        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);
            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)
        {
            try
            {   
                ppDialog.Document = printDoc;
                ppDialog.ShowDialog();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}


PrinterSettings.InstalledPrinters

  
/*
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.Drawing.Printing;
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
//Need Printing namespace
using System.Drawing.Printing;
namespace PrinterCaps1
{
    public class PrinterCaps1 : System.Windows.Forms.Form
    {
        private System.ruponentModel.Container components = null;
        public PrinterCaps1()
        {
            InitializeComponent();
        }
        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()
        {
            // 
            // PrinterCaps1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Name = "PrinterCaps1";
            this.Text = "Printer Caps";
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.PrinterCaps1_Paint);
        }
        #endregion
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new PrinterCaps1());
        }

        private void PrinterCaps1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            PrinterSettings pSettings = new PrinterSettings();
            Font printFont = new Font("Arial", 10);
   
            //Position
            int nTextPosY = 0;
            int nTextPosX = 5;
            int nHeight = (int)printFont.GetHeight(e.Graphics);
            foreach(string sPtr in PrinterSettings.InstalledPrinters)
            {
                //pSettings.PrinterName = sPtr;
                pSettings.PrinterName = "BadName";
                //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);
                //}
            }
            return;
        }
    }
}


PrinterSettings.PrinterName

  
using System;
using System.Drawing.Printing;
class MainClass {
    static void Main(string[] args) {
        foreach (string printerName in PrinterSettings.InstalledPrinters) {
            Console.WriteLine("Printer: {0}", printerName);
            PrinterSettings printer = new PrinterSettings();
            printer.PrinterName = printerName;
            if (printer.IsValid) {
                Console.WriteLine("Supported Paper Sizes:");
                foreach (PaperSize size in printer.PaperSizes) {
                    if (Enum.IsDefined(size.Kind.GetType(), size.Kind)) {
                        Console.WriteLine("  {0}", size);
                    }
                }
            }
        }
    }
}


PrinterSettings.PrinterResolutions

  

using System;
using System.Drawing.Printing;
class MainClass {
    static void Main(string[] args) {
        foreach (string printerName in PrinterSettings.InstalledPrinters) {
            Console.WriteLine("Printer: {0}", printerName);
            PrinterSettings printer = new PrinterSettings();
            printer.PrinterName = printerName;
            if (printer.IsValid) {
                Console.WriteLine("Supported Resolutions:");
                foreach (PrinterResolution resolution in printer.PrinterResolutions) {
                    Console.WriteLine("  {0}", resolution);
                }
            }
        }
    }
}