home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Printing / PrintWithStatusBar / PrintWithStatusBar.cs next >
Encoding:
Text File  |  2001-01-15  |  2.1 KB  |  66 lines

  1. //-------------------------------------------------
  2. // PrintWithStatusBar.cs ⌐ 2001 by Charles Petzold
  3. //-------------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Drawing.Printing;
  7. using System.Windows.Forms;
  8.  
  9. class PrintWithStatusBar: Form
  10. {
  11.      StatusBar      sbar;
  12.      StatusBarPanel sbarpanel;
  13.  
  14.      const int iNumberPages = 3;
  15.      int       iPageNumber;
  16.  
  17.      public static void Main()
  18.      {
  19.           Application.Run(new PrintWithStatusBar());
  20.      }
  21.      public PrintWithStatusBar()
  22.      {
  23.           Text = "Print with Status Bar";
  24.  
  25.           Menu = new MainMenu();
  26.           Menu.MenuItems.Add("&File");
  27.           Menu.MenuItems[0].MenuItems.Add("&Print", 
  28.                                    new EventHandler(MenuFilePrintOnClick));
  29.  
  30.           sbar = new StatusBar();
  31.           sbar.Parent = this;
  32.           sbar.ShowPanels = true;
  33.  
  34.           sbarpanel = new StatusBarPanel();
  35.           sbarpanel.Text = "Ready";
  36.           sbarpanel.Width = Width / 2;
  37.           sbar.Panels.Add(sbarpanel);
  38.      }
  39.      void MenuFilePrintOnClick(object obj, EventArgs ea)
  40.      {
  41.           PrintDocument prndoc   = new PrintDocument();
  42.           prndoc.DocumentName    = Text;
  43.           prndoc.PrintController = new StatusBarPrintController(sbarpanel);
  44.           prndoc.PrintPage      += new PrintPageEventHandler(OnPrintPage);
  45.  
  46.           iPageNumber = 1;
  47.           prndoc.Print();
  48.      }
  49.      void OnPrintPage(object obj, PrintPageEventArgs ppea)
  50.      {
  51.           Graphics grfx  = ppea.Graphics;
  52.           Font     font  = new Font("Times New Roman", 360);
  53.           string   str   = iPageNumber.ToString();
  54.           SizeF    sizef = grfx.MeasureString(str, font);
  55.  
  56.           grfx.DrawString(str, font, Brushes.Black, 
  57.                     (grfx.VisibleClipBounds.Width - sizef.Width) / 2,
  58.                     (grfx.VisibleClipBounds.Height - sizef.Height) / 2);
  59.  
  60.           System.Threading.Thread.Sleep(1000);    
  61.  
  62.           ppea.HasMorePages = iPageNumber < iNumberPages;
  63.           iPageNumber += 1;
  64.      }
  65. }
  66.