Csharp/C Sharp by API/System.Drawing.Printing/PrintDocument — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:08, 26 мая 2010
Содержание
extends PrintDocument
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;
public class PrintParagraph : Form
{
public PrintParagraph()
{
this.cmdPrint = new System.Windows.Forms.Button();
this.SuspendLayout();
this.cmdPrint.Location = new System.Drawing.Point(109, 122);
this.cmdPrint.Size = new System.Drawing.Size(75, 23);
this.cmdPrint.Text = "Print";
this.cmdPrint.UseVisualStyleBackColor = true;
this.cmdPrint.Click += new System.EventHandler(this.cmdPrint_Click);
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(282, 259);
this.Controls.Add(this.cmdPrint);
this.Text = "Wrapped Print";
this.ResumeLayout(false);
}
private void cmdPrint_Click(object sender, EventArgs e)
{
string text = "a paragraph";
PrintDocument doc = new ParagraphDocument(text);
doc.PrintPage += new PrintPageEventHandler(this.Doc_PrintPage);
PrintDialog dlgSettings = new PrintDialog();
dlgSettings.Document = doc;
if (dlgSettings.ShowDialog() == DialogResult.OK)
{
doc.Print();
}
}
private void Doc_PrintPage(object sender, PrintPageEventArgs e)
{
ParagraphDocument doc = (ParagraphDocument)sender;
Font font = new Font("Arial", 15);
e.Graphics.DrawString(doc.Text, font, Brushes.Black,
e.MarginBounds, StringFormat.GenericDefault);
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new PrintParagraph());
}
private System.Windows.Forms.Button cmdPrint;
}
public class ParagraphDocument : PrintDocument
{
public string Text;
public ParagraphDocument(string text)
{
this.Text = text;
}
}
new PrintDocument()
using System;
using System.Drawing.Printing;
using System.Drawing;
class PrintSample
{
[STAThread]
static void Main(string[] args)
{
PrintSample oSample = new PrintSample();
oSample.RunSample();
}
public void RunSample()
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.PrintPageEvent);
pd.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);
}
}
PrintDocument.DefaultPageSettings
using System;
using System.Drawing.Printing;
namespace DefPrintSettings_c
{
public class DefPrintSettings
{
[STAThread]
static void Main(string[] args)
{
PrintDocument pd = new PrintDocument();
PageSettings pg = pd.DefaultPageSettings;
PrinterSettings ps = pg.PrinterSettings;
Console.WriteLine("Printer Settings");
Console.WriteLine("PrinterName = " + pd.PrinterSettings.PrinterName);
Console.WriteLine("Is default Printer = " +
ps.IsDefaultPrinter.ToString());
Console.WriteLine("Is plotter = " + ps.IsPlotter.ToString());
Console.WriteLine("Is printer valid = " + ps.IsValid.ToString());
Console.WriteLine("Can Duplex = " + ps.IsValid.ToString());
Console.WriteLine("Num copies = " + ps.Copies.ToString());
Console.WriteLine("Max Copies = " + ps.MaximumCopies.ToString());
Console.WriteLine("Max Page = " + ps.MaximumPage.ToString());
Console.WriteLine("Min Page = " + ps.MinimumPage.ToString());
Console.WriteLine("Supports Color = " + ps.SupportsColor.ToString());
foreach (PaperSize p in ps.PaperSizes)
Console.WriteLine("Supports Paper Size: " + p.PaperName);
foreach (PaperSource p in ps.PaperSources)
Console.WriteLine("Supports Paper Source: " + p.SourceName);
Console.WriteLine("\nPage Settings");
Console.WriteLine("Is Color = " + pg.Color.ToString());
Console.WriteLine("Top Bound = " + pg.Bounds.Top.ToString());
Console.WriteLine("Bottom Bound = " + pg.Bounds.Bottom.ToString());
Console.WriteLine("Left Bound = " + pg.Bounds.Left.ToString());
Console.WriteLine("Right Bound = " + pg.Bounds.Right.ToString());
Console.WriteLine("Top Margin = " + pg.Margins.Top.ToString());
Console.WriteLine("Bottom Margin = " + pg.Margins.Bottom.ToString());
Console.WriteLine("Left Margin = " + pg.Margins.Left.ToString());
Console.WriteLine("Right Margin = " + pg.Margins.Right.ToString());
Console.WriteLine("Landscape = " + pg.Landscape.ToString());
Console.WriteLine("PaperSize = " + pg.PaperSize.PaperName);
Console.WriteLine("PaperSource = " + pg.PaperSource.SourceName);
Console.WriteLine("PrinterResolution = " +
pg.PrinterResolution.Kind.ToString());
Console.WriteLine("PrinterResolution X = " +
pg.PrinterResolution.X.ToString());
Console.WriteLine("PrinterResolution Y = " +
pg.PrinterResolution.Y.ToString());
Console.ReadLine();
}
}
}
PrintDocument.DocumentName
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
class HelloPrinter: Form
{
public static void Main()
{
Application.Run(new HelloPrinter());
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;
StringFormat strfmt = new StringFormat();
grfx.DrawString("Click to print", Font, new SolidBrush(ForeColor),ClientRectangle, strfmt);
}
protected override void OnClick(EventArgs ea)
{
PrintDocument prndoc = new PrintDocument();
prndoc.DocumentName = Text;
prndoc.PrintPage += new PrintPageEventHandler(PrintDocumentOnPrintPage);
prndoc.Print();
}
void PrintDocumentOnPrintPage(object obj, PrintPageEventArgs ppea)
{
Graphics grfx = ppea.Graphics;
grfx.DrawString(Text, Font, Brushes.Black, 0, 0);
SizeF sizef = grfx.MeasureString(Text, Font);
grfx.DrawLine(Pens.Black, sizef.ToPointF(), grfx.VisibleClipBounds.Size.ToPointF());
}
}
PrintDocument.Print()
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;
public class PrintDocumentSubClass : Form
{
public PrintDocumentSubClass()
{
this.cmdPrint = new System.Windows.Forms.Button();
this.SuspendLayout();
//
this.cmdPrint.Location = new System.Drawing.Point(109, 122);
this.cmdPrint.Size = new System.Drawing.Size(75, 23);
this.cmdPrint.Text = "Print";
this.cmdPrint.UseVisualStyleBackColor = true;
this.cmdPrint.Click += new System.EventHandler(this.cmdPrint_Click);
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(278, 259);
this.Controls.Add(this.cmdPrint);
this.Text = "Multi Page Print";
this.ResumeLayout(false);
}
private void cmdPrint_Click(object sender, EventArgs e)
{
PrintDocument doc = new TextDocument();
doc.PrintPage += this.Doc_PrintPage;
PrintDialog dlgSettings = new PrintDialog();
dlgSettings.Document = doc;
if (dlgSettings.ShowDialog() == DialogResult.OK)
{
doc.Print();
}
}
private void Doc_PrintPage(object sender, PrintPageEventArgs e)
{
TextDocument doc = (TextDocument)sender;
Font font = new Font("Arial", 10);
float lineHeight = font.GetHeight(e.Graphics);
float x = e.MarginBounds.Left;
float y = e.MarginBounds.Top;
doc.PageNumber += 1;
while ((y + lineHeight) < e.MarginBounds.Bottom && doc.Offset <= doc.Text.GetUpperBound(0))
{
e.Graphics.DrawString(doc.Text[doc.Offset], font,Brushes.Black, x, y);
doc.Offset += 1;
y += lineHeight;
}
if (doc.Offset < doc.Text.GetUpperBound(0))
{
e.HasMorePages = true;
} else {
doc.Offset = 0;
}
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new PrintDocumentSubClass());
}
private System.Windows.Forms.Button cmdPrint;
}
class TextDocument : PrintDocument{
private string[] text;
public string[] Text;
public int PageNumber;
public int Offset;
public TextDocument()
{
this.Text = new string[100];
for (int i = 0; i < 100; i++)
{
this.Text[i] += "string Text ";
}
}
}
PrintDocument.PrintPage
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
class PrintableForm: Form
{
public static void Main()
{
Application.Run(new PrintableForm());
}
public PrintableForm()
{
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected override void OnClick(EventArgs ea)
{
PrintDocument prndoc = new PrintDocument();
prndoc.DocumentName = Text;
prndoc.PrintPage += new PrintPageEventHandler(PrintDocumentOnPrintPage);
prndoc.Print();
}
void PrintDocumentOnPrintPage(object obj, PrintPageEventArgs ppea)
{
Graphics grfx = ppea.Graphics;
SizeF sizef = grfx.VisibleClipBounds.Size;
DoPage(grfx, Color.Black, (int)sizef.Width, (int)sizef.Height);
}
protected virtual void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
Pen pen = new Pen(clr);
grfx.DrawLine(pen, 0, 0, cx - 1, cy - 1);
grfx.DrawLine(pen, cx - 1, 0, 0, cy - 1);
}
}