Csharp/C Sharp by API/System.Windows.Forms/PrintPreviewDialog

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

PrintPreviewDialog.Document

<source lang="csharp"> using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data;

public class Form1 : System.Windows.Forms.Form {

   private System.Windows.Forms.Button button1;
   private System.Windows.Forms.Button button2;
   
   private System.ruponentModel.Container components = null;
   private System.Drawing.Printing.PrintDocument ThePrintDocument = null;
   private System.IO.StringReader myStringReader = null;
   public Form1() {
       ThePrintDocument = new System.Drawing.Printing.PrintDocument();
       this.button1 = new System.Windows.Forms.Button();
       this.button2 = new System.Windows.Forms.Button();
       this.SuspendLayout();
       this.button1.Location = new System.Drawing.Point(112, 352);
       this.button1.Text = "&Preview";
       this.button1.Click += new System.EventHandler(this.button1_Click);
       this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
       this.ClientSize = new System.Drawing.Size(512, 397);
       this.Controls.AddRange(new System.Windows.Forms.Control[] { this.button1 });
       this.ResumeLayout(false);
   }
   [STAThread]
   static void Main() {
       Application.Run(new Form1());
   }
   protected void PrintPage(object sender,
       System.Drawing.Printing.PrintPageEventArgs ev) {
       float linesPerPage = 0;
       float yPosition = 0;
       int count = 0;
       float leftMargin = ev.MarginBounds.Left;
       float topMargin = ev.MarginBounds.Top;
       string line = null;
       Font printFont = this.Font;
       SolidBrush myBrush = new SolidBrush(Color.Black);
       linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);
       while (count < linesPerPage && ((line = myStringReader.ReadLine()) != null)) {
           yPosition = topMargin + (count * printFont.GetHeight(ev.Graphics));
           ev.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
           count++;
       }
       if (line != null)
           ev.HasMorePages = true;
       else
           ev.HasMorePages = false;
       myBrush.Dispose();
   }
   private void button1_Click(object sender, System.EventArgs e) {
       ThePrintDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintPage);
       string strText = "STRING";
       myStringReader = new System.IO.StringReader(strText);
       PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
       printPreviewDialog1.Document = this.ThePrintDocument;
       printPreviewDialog1.ShowDialog();
   }

}

 </source>


PrintPreviewDialog.ShowDialog

<source lang="csharp"> using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing.Printing;

public class Form1 : Form {

   private Font mainTextFont = new Font("Times New Roman", 14);
   private Font subTextFont = new Font("Times New Roman", 12);
   private PageSettings storedPageSettings;
   public Form1() {
       InitializeComponent();
   }
   private void PaintDocument(Graphics g) {
       g.PageUnit = GraphicsUnit.Point;
       g.DrawString("Simple Printing Sample",
                    this.mainTextFont,
                    Brushes.Black,
                    new Rectangle(10, 20, 180, 30));
       g.DrawRectangle(Pens.Blue,
                       new Rectangle(new Point(10, 100), new Size(100, 50)));
   }
   private void Form1_Paint(object sender, PaintEventArgs e) {
       Graphics g = e.Graphics;
       PaintDocument(g);
   }
   private void menuFilePageSetup_Click(object sender, EventArgs e) {
       PageSetupDialog psDlg = new PageSetupDialog();
       if (this.storedPageSettings == null)
           this.storedPageSettings = new PageSettings();
       psDlg.PageSettings = this.storedPageSettings;
       psDlg.ShowDialog();
   }
   private void WriteMetricsToConsole(PrintPageEventArgs ev) {
       Graphics g = ev.Graphics;
       Console.WriteLine("ev.PageSettings.PaperSize: " + ev.PageSettings.PaperSize);
       Console.WriteLine("ev.PageSettings.PrinterResolution: " + ev.PageSettings.PrinterResolution);
       Console.WriteLine("ev.PageSettings.PrinterSettings.LandscapeAngle: " + ev.PageSettings.PrinterSettings.LandscapeAngle);
       Console.WriteLine("ev.PageSettings.Bounds: " + ev.PageSettings.Bounds);
       Console.WriteLine("ev.PageBounds: " + ev.PageBounds);
       Console.WriteLine("ev.PageSettings.Margins: " + ev.PageSettings.Margins);
       Console.WriteLine("ev.MarginBounds: " + ev.MarginBounds);
       Console.WriteLine("Horizontal resolution: " + g.DpiX);
       Console.WriteLine("Vertical resolution: " + g.DpiY);
       g.SetClip(ev.PageBounds);
       Console.WriteLine("g.VisibleClipBounds: " + g.VisibleClipBounds);
       SizeF drawingSurfaceSize = new SizeF( g.VisibleClipBounds.Width * g.DpiX / 100,g.VisibleClipBounds.Height * g.DpiY / 100);
       Console.WriteLine("Drawing Surface Size in Pixels: " + drawingSurfaceSize);
   }
   protected void PrintPageEventHandler(Object obj, PrintPageEventArgs ev) {
       WriteMetricsToConsole(ev);
       Graphics g = ev.Graphics;
       PaintDocument(g);
       ev.HasMorePages = false;
   }
   private void menuFilePrint_Click(object sender, EventArgs e) {
       PrintDocument pd = new PrintDocument();
       pd.PrintPage += new PrintPageEventHandler(this.PrintPageEventHandler);
       if (this.storedPageSettings != null)
           pd.DefaultPageSettings = this.storedPageSettings;
       PrintDialog dlg = new PrintDialog();
       dlg.Document = pd;
       DialogResult result = dlg.ShowDialog();
       if (result == System.Windows.Forms.DialogResult.OK)
           pd.Print();
   }
   private void menuFilePrintPreview_Click(object sender, EventArgs e) {
       PrintDocument pd = new PrintDocument();
       pd.PrintPage += new PrintPageEventHandler(this.PrintPageEventHandler);
       if (this.storedPageSettings != null)
           pd.DefaultPageSettings = this.storedPageSettings;
       PrintPreviewDialog dlg = new PrintPreviewDialog();
       dlg.Document = pd;
       dlg.ShowDialog();
   }
   private void InitializeComponent() {
       this.menuStrip1 = new System.Windows.Forms.MenuStrip();
       this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
       this.menuFilePageSetup = new System.Windows.Forms.ToolStripMenuItem();
       this.menuFilePrintPreview = new System.Windows.Forms.ToolStripMenuItem();
       this.menuFilePrint = new System.Windows.Forms.ToolStripMenuItem();
       this.menuStrip1.SuspendLayout();
       this.SuspendLayout();
       this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
           this.fileToolStripMenuItem});
       this.menuStrip1.Location = new System.Drawing.Point(0, 0);
       this.menuStrip1.Size = new System.Drawing.Size(292, 25);
       this.menuStrip1.Text = "menuStrip1";
       this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
           this.menuFilePageSetup,
           this.menuFilePrintPreview,
           this.menuFilePrint});
       this.fileToolStripMenuItem.Text = "File";
       this.menuFilePageSetup.Text = "Page Setup";
       this.menuFilePageSetup.Click += new System.EventHandler(this.menuFilePageSetup_Click);
       this.menuFilePrintPreview.Text = "Print Preview";
       this.menuFilePrintPreview.Click += new System.EventHandler(this.menuFilePrintPreview_Click);
       this.menuFilePrint.Text = "Print";
       this.menuFilePrint.Click += new System.EventHandler(this.menuFilePrint_Click);
       this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
       this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
       this.BackColor = System.Drawing.SystemColors.Window;
       this.ClientSize = new System.Drawing.Size(292, 268);
       this.Controls.Add(this.menuStrip1);
       this.MainMenuStrip = this.menuStrip1;
       this.Text = "SimplePrintingExample";
       this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
       this.menuStrip1.ResumeLayout(false);
       this.ResumeLayout(false);
       this.PerformLayout();
   }
   private System.Windows.Forms.MenuStrip menuStrip1;
   private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
   private System.Windows.Forms.ToolStripMenuItem menuFilePageSetup;
   private System.Windows.Forms.ToolStripMenuItem menuFilePrintPreview;
   private System.Windows.Forms.ToolStripMenuItem menuFilePrint;
   [STAThread]
   static void Main() {
       Application.Run(new Form1());
   }

}

 </source>