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);
}
}
}
}
}
}
Month: February 2011
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);
}
}
}
}
}
InstalledPrinters
using System;
using System.Drawing.Printing;
class MainClass {
static void Main(string[] args) {
foreach (string printerName in PrinterSettings.InstalledPrinters) {
Console.WriteLine("Printer: {0}", printerName);
}
}
}
PrinterName, IsValid printer
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;
Console.WriteLine(printer.IsValid);
}
}
}
ProgressBar in C#
// I am trying to use Class ProgressBar in C#
// Editor Used : Antechinus http://www.c-point.com
// Dt. 9th Jan. 2001
// ... Special thanks Saurabh for help & guidance ... Thanks! someday i would
// do the way you do.... programming
// ... thanks Mahesh for loading my codes. Your site rules!
// ... thanks to you for reading it you know better who you are 🙂
// if you have any comments or doubts or whatever please don't
// hesitate to e-mail me.
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
/// <summary>
/// Summary description for Win32Form2.
/// </summary>
public class Win32Form2 : System.Windows.Forms.Form {
/// <summary>
/// Required by the Win Forms designer
/// </summary>
private System.ComponentModel.Container components;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ProgressBar progressBar1;
public Win32Form2() {
// Required for Win Form Designer support
InitializeComponent();
}
/// <summary>
/// The main entry point for the application.
/// </summary>
public static void Main(string[] args) {
Application.Run(new Win32Form2());
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with an editor
/// </summary>
private void InitializeComponent() {
this.components = new System.ComponentModel.Container();
this.label1 = new System.Windows.Forms.Label();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
//@design this.TrayHeight = 0;
//@design this.TrayLargeIcon = false;
//@design this.TrayAutoArrange = true;
label1.Location = new System.Drawing.Point(32, 40);
label1.Text = "Progress Value";
label1.Size = new System.Drawing.Size(88, 24);
label1.TabIndex = 2;
progressBar1.Maximum = 10;
progressBar1.Location = new System.Drawing.Point(8, 312);
progressBar1.Minimum = 0;
progressBar1.TabIndex = 0;
progressBar1.Value = 0;
//We have calculated the excat size which will result in only 20 boxes to be drawn
progressBar1.Size = new System.Drawing.Size(520, 40);
progressBar1.Step = 1;
button1.Location = new System.Drawing.Point(152, 168);
button1.Size = new System.Drawing.Size(144, 48);
button1.TabIndex = 1;
button1.Text = "button1";
button1.Click += new System.EventHandler(button1_Click);
textBox1.Location = new System.Drawing.Point(136, 40);
textBox1.Text = "0";
textBox1.TabIndex = 3;
textBox1.Size = new System.Drawing.Size(184, 20);
this.Text = "Win32Form2";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(616, 393);
this.Click += new System.EventHandler(Win32Form2_Click);
this.Controls.Add(textBox1);
this.Controls.Add(label1);
this.Controls.Add(button1);
this.Controls.Add(progressBar1);
}
protected void button1_Click(object sender, System.EventArgs e) {
//this checking is automatically done as stated in the Ref Documentation
//but it does not work , BUGssssss
//so we have to do it shhhhh ....
if (progressBar1.Value == progressBar1.Maximum){
progressBar1.Value = progressBar1.Minimum;
}
progressBar1.PerformStep();
textBox1.Text=progressBar1.Value.ToString() ; // Displays the values of progressbar in textbox
}
protected void Win32Form2_Click(object sender, System.EventArgs e) {
}
}
UI Print
/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds
Publisher: Apress
ISBN: 159059035X
*/
using System;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace UIPrint_c
{
public class UIPrint : System.Windows.Forms.Form
{
private PrintPreviewDialog Pv;
private PageSetupDialog Ps;
private PrintDocument Pd;
private PrintDialog Pr;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem mnuFile;
private System.Windows.Forms.MenuItem mnuSetup;
private System.Windows.Forms.MenuItem mnuPreview;
private System.Windows.Forms.MenuItem mnuPrint;
private System.Windows.Forms.Button cmdQuit;
private System.ComponentModel.Container components = null;
public UIPrint()
{
InitializeComponent();
Pv = new PrintPreviewDialog();
Ps = new PageSetupDialog();
Pr = new PrintDialog();
Pd = new PrintDocument();
Pd.DocumentName = "My New Document";
Pv.Document = Pd;
Ps.Document = Pd;
Pr.Document = Pd;
}
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()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.mnuFile = new System.Windows.Forms.MenuItem();
this.mnuSetup = new System.Windows.Forms.MenuItem();
this.mnuPreview = new System.Windows.Forms.MenuItem();
this.mnuPrint = new System.Windows.Forms.MenuItem();
this.cmdQuit = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuFile});
//
// mnuFile
//
this.mnuFile.Index = 0;
this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuSetup,
this.mnuPreview,
this.mnuPrint});
this.mnuFile.Text = "File";
//
// mnuSetup
//
this.mnuSetup.Index = 0;
this.mnuSetup.Text = "Page Setup";
this.mnuSetup.Click += new System.EventHandler(this.mnuSetup_Click);
//
// mnuPreview
//
this.mnuPreview.Index = 1;
this.mnuPreview.Text = "Print Preview";
this.mnuPreview.Click += new System.EventHandler(this.mnuPreview_Click);
//
// mnuPrint
//
this.mnuPrint.Index = 2;
this.mnuPrint.Text = "Print";
this.mnuPrint.Click += new System.EventHandler(this.mnuPrint_Click);
//
// cmdQuit
//
this.cmdQuit.Location = new System.Drawing.Point(256, 256);
this.cmdQuit.Name = "cmdQuit";
this.cmdQuit.Size = new System.Drawing.Size(64, 32);
this.cmdQuit.TabIndex = 0;
this.cmdQuit.Text = "Quit";
this.cmdQuit.Click += new System.EventHandler(this.cmdQuit_Click);
//
// UIPrint
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(342, 303);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.cmdQuit});
this.MaximizeBox = false;
this.Menu = this.mainMenu1;
this.MinimizeBox = false;
this.Name = "UIPrint";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "UIPrint";
this.Load += new System.EventHandler(this.UIPrint_Load);
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new UIPrint());
}
private void UIPrint_Load(object sender, System.EventArgs e)
{
Pd.PrintPage += new PrintPageEventHandler(this.pd_Print);
}
protected override void OnPaint(PaintEventArgs e)
{
DrawIt(e.Graphics);
}
private void DrawIt(Graphics G)
{
G.SmoothingMode = SmoothingMode.AntiAlias;
Pen P1 = new Pen(Brushes.Violet, 5);
G.DrawString("Test of Print dialog and page setup",
new Font("Time New Roman", 16),
Brushes.Blue,
new Point(5, 5));
G.DrawPie(P1, 10, 10, 150, 150, 28, 57);
G.FillEllipse(Brushes.BurlyWood, 10, 200, this.Width-50, 50);
}
private void pd_Print(object sender, PrintPageEventArgs e)
{
DrawIt(e.Graphics);
}
private void mnuSetup_Click(object sender, System.EventArgs e)
{
Ps.ShowDialog();
Pd.DefaultPageSettings = Ps.PageSettings;
Pd.PrinterSettings = Ps.PrinterSettings;
}
private void mnuPreview_Click(object sender, System.EventArgs e)
{
Pv.WindowState = FormWindowState.Maximized;
Pv.ShowDialog();
}
private void mnuPrint_Click(object sender, System.EventArgs e)
{
if (Pr.ShowDialog() == DialogResult.OK)
Pd.Print();
}
private void cmdQuit_Click(object sender, System.EventArgs e)
{
this.Dispose();
}
}
}
Basic Printing
/*
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.Printing;
using System.Drawing;
namespace BasicPrinting
{
public class PrintSample
{
[STAThread]
static void Main(string[] args)
{
PrintSample oSample = new PrintSample();
oSample.RunSample();
}
public void RunSample()
{
Console.WriteLine("Printing to the default printer...");
try
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.PrintPageEvent);
pd.Print();
}
catch(Exception ex)
{
Console.WriteLine("Error printing -- " + ex.ToString());
}
//Read input - to delay the closing of the DOS shell
Console.ReadLine();
}
//Event fired for each page to print
private void PrintPageEvent(object sender, PrintPageEventArgs ev)
{
string strHello = "Hello Printer!";
Font oFont = new Font("Arial",10);
Rectangle marginRect = ev.MarginBounds;
ev.Graphics.DrawRectangle(new Pen(System.Drawing.Color.Black),marginRect);
ev.Graphics.DrawString(strHello,oFont,new SolidBrush(System.Drawing.Color.Blue),
(ev.PageBounds.Right/2), ev.PageBounds.Bottom/2);
}
}
}

