Csharp/C Sharp by API/System.Windows.Forms/PageSetupDialog
Версия от 15:31, 26 мая 2010; (обсуждение)
PageSetupDialog.PageSettings
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) {
try {
PageSetupDialog psDlg = new PageSetupDialog();
if (this.storedPageSettings == null)
this.storedPageSettings = new PageSettings();
psDlg.PageSettings = this.storedPageSettings;
psDlg.ShowDialog();
} catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
protected void PrintPageEventHandler(Object obj, PrintPageEventArgs ev) {
Graphics g = ev.Graphics;
PaintDocument(g);
ev.HasMorePages = false;
}
private void menuFilePrint_Click(object sender, EventArgs e) {
try {
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();
} catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e) {
try {
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();
} catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
private void InitializeComponent() {
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.menuFilePrintPreview = new System.Windows.Forms.ToolStripMenuItem();
this.menuFilePageSetup = new System.Windows.Forms.ToolStripMenuItem();
this.printPreviewToolStripMenuItem = 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.menuFilePrintPreview});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Size = new System.Drawing.Size(292, 25);
this.menuStrip1.Text = "menuStrip1";
this.menuFilePrintPreview.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.menuFilePageSetup,
this.printPreviewToolStripMenuItem,
this.menuFilePrint});
this.menuFilePrintPreview.Name = "menuFilePrintPreview";
this.menuFilePrintPreview.Text = "File";
this.menuFilePageSetup.Name = "menuFilePageSetup";
this.menuFilePageSetup.Text = "Page Setup";
this.menuFilePageSetup.Click += new System.EventHandler(this.menuFilePageSetup_Click);
this.printPreviewToolStripMenuItem.Name = "printPreviewToolStripMenuItem";
this.printPreviewToolStripMenuItem.Text = "Print Preview";
this.printPreviewToolStripMenuItem.Click += new System.EventHandler(this.printPreviewToolStripMenuItem_Click);
this.menuFilePrint.Name = "menuFilePrint";
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.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 menuFilePrintPreview;
private System.Windows.Forms.ToolStripMenuItem menuFilePageSetup;
private System.Windows.Forms.ToolStripMenuItem printPreviewToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem menuFilePrint;
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
PageSetupDialog.ShowDialog();
/*
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.ruponentModel;
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.ruponentModel.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();
}
}
}