Print Events : 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 
Print Events
Print Events

/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds

Publisher: Apress
ISBN: 159059035X
*/

using System;
using System.IO;
using System.Drawing;
using System.Drawing.Printing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace PrintEvents_c
{
    public class PrintEvents : System.Windows.Forms.Form
    {
    private Font PrintFont;
    private StreamReader PrintStream;

    private System.Windows.Forms.Button cmdPrint;
    private System.Windows.Forms.Label lblEvents;

        private System.ComponentModel.Container components = null;

        public PrintEvents()
        {
            InitializeComponent();
    }

        protected override void Disposebool disposing )
        {
            ifdisposing )
            {
                if (components != null
                {
                    components.Dispose();
                }
                PrintFont.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()
        {
      this.cmdPrint = new System.Windows.Forms.Button();
      this.lblEvents = new System.Windows.Forms.Label();
      this.SuspendLayout();
      // 
      // cmdPrint
      // 
      this.cmdPrint.Location = new System.Drawing.Point(104280);
      this.cmdPrint.Name = "cmdPrint";
      this.cmdPrint.Size = new System.Drawing.Size(8024);
      this.cmdPrint.TabIndex = 0;
      this.cmdPrint.Text = "Print";
      this.cmdPrint.Click += new System.EventHandler(this.cmdPrint_Click);
      // 
      // lblEvents
      // 
      this.lblEvents.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
      this.lblEvents.Location = new System.Drawing.Point(2416);
      this.lblEvents.Name = "lblEvents";
      this.lblEvents.Size = new System.Drawing.Size(248248);
      this.lblEvents.TabIndex = 1;
      // 
      // PrintEvents
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(513);
      this.ClientSize = new System.Drawing.Size(292323);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                  this.lblEvents,
                                                                  this.cmdPrint});
      this.MaximizeBox = false;
      this.MinimizeBox = false;
      this.Name = "PrintEvents";
      this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
      this.Text = "PrintEvents";
      this.Load += new System.EventHandler(this.PrintEvents_Load);
      this.ResumeLayout(false);

    }
        #endregion

        [STAThread]
        static void Main() 
        {
            Application.Run(new PrintEvents());
        }

    private void PrintEvents_Load(object sender, System.EventArgs e)
    {
    }

    // Print the file.
    public void Print_It()
    {
      try 
      {
        //Get the file to print
        PrintStream = new StreamReader ("Test.txt");
        try 
        {
          PrintFont = new Font("Arial"10);
          PrintDocument pd = new PrintDocument()

          //Assign my overloaded version of the standard print controller
          //Send it a reference to the label so it can tell us what is 
          //going on.
          pd.PrintController = new MyPrintController(ref lblEvents);

          //Install event handlers
          pd.BeginPrint += new PrintEventHandler(this.pd_StartPrint);
          pd.PrintPage  += new PrintPageEventHandler(this.pd_PrintPage);
          pd.EndPrint   += new PrintEventHandler(this.pd_EndPrint);

          // Print the document.
          pd.Print();
        
        finally 
        {
          PrintStream.Close();
        }
      
      catch(Exception ex
      
        MessageBox.Show(ex.Message);
      }
    }


    private void pd_StartPrint(object sender, PrintEventArgs ev)
    {
      lblEvents.Text += "PrintDocument: BeginPrint\n";
    }

    private void pd_EndPrint(object sender, PrintEventArgs ev)
    {
      lblEvents.Text += "PrintDocument: EndPrinting\n";
    }

    // The PrintPage event is raised for each page to be printed.
    private void pd_PrintPage(object sender, PrintPageEventArgs ev
    {
      float linesPerPage = 0;
      float yPos =  0;
      int   count = 0;
      float leftMargin = ev.MarginBounds.Left;
      float topMargin = ev.MarginBounds.Top;
      String line=null;
            
      lblEvents.Text += "PrintDocument:  PagePrint\n";

      // Calculate the number of lines per page.
      linesPerPage = ev.MarginBounds.Height / PrintFont.GetHeight(ev.Graphics);

      // Iterate over the file, printing each line. Use a basic StringFormat
      while (count++ < linesPerPage && ((line=PrintStream.ReadLine()) != null)) 
      {
        //Calculate vertical position of the line.
        yPos = topMargin + (count * PrintFont.GetHeight(ev.Graphics));
        //This is the graphics object obtained by the PrintController
        //OnStartPage method.  We are drawing to the printer!!
        ev.Graphics.DrawString (line, PrintFont, Brushes.Black, 
                                leftMargin, yPos, new StringFormat());
      }

      // If more lines exist, print another page.
      if (line != null
        ev.HasMorePages = true;
      else 
        ev.HasMorePages = false;
    }

    private void cmdPrint_Click(object sender, System.EventArgs e)
    {
      Print_It();
    }
    }

    public class MyPrintController : StandardPrintController
    {
    private Label lblEvents;
        public MyPrintController(ref Label lbl): base()
        {
      lblEvents = lbl;
    }

    public override void OnStartPrint(PrintDocument doc, PrintEventArgs e)
    {
      lblEvents.Text += "      PrintController: OnStartPrint\n";
      base.OnStartPrint(doc, e);
    }
    public override Graphics OnStartPage(PrintDocument doc, PrintPageEventArgs e)
    {
      lblEvents.Text += "      PrintController: OnStartPage\n";
      returnbase.OnStartPage(doc, e) );
    }
    public override void OnEndPage(PrintDocument doc, PrintPageEventArgs e)
    {
      lblEvents.Text += "      PrintController: OnEndPage\n";
      base.OnEndPage(doc, e);
    }
    public override void OnEndPrint(PrintDocument doc, PrintEventArgs e)
    {
      lblEvents.Text += "      PrintController: OnEndPrint\n";
      base.OnEndPrint(doc, e);
    }
  }

}


           
       
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.UI PrintUI Print
7.Basic Printing
8.Grid PrintingGrid Printing
9.Printer Caps 1
10.Printer Caps 2Printer Caps 2
11.Printer Caps 3Printer Caps 3
12.Printer Caps 4Printer Caps 4
13.Printer Caps 5Printer Caps 5
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.