8.C#编程学习——画图
PrintableForm源码
usingSystem;
usingSystem.Drawing;
usingSystem.Drawing.Printing;
usingSystem.Windows.Forms;
classPrintableForm : Form
{
public PrintableForm()
{
Text = "Printable Form";
BackColor = SystemColors.Window;
ForeColor = SystemColors.WindowText;
ResizeRedraw = true;
}
protectedoverridevoid OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,
ClientSize.Width,ClientSize.Height);
}
protectedoverridevoid OnClick(EventArgs ea)
{
PrintDocument prndoc = newPrintDocument();
prndoc.DocumentName = Text;
prndoc.PrintPage +=
newPrintPageEventHandler(PrintDocumentOnPrintPage);
prndoc.Print();
}
void PrintDocumentOnPrintPage(objectobj, PrintPageEventArgsppea)
{
Graphics grfx = ppea.Graphics;
SizeF sizef = grfx.VisibleClipBounds.Size;
DoPage(grfx, Color.Black, (int)sizef.Width, (int)sizef.Height);
}
protectedvirtualvoid DoPage(Graphics grfx, Color clr, int cx, int cy)
{
Pen pen = newPen(clr);
grfx.DrawLine(pen, 0, 0, cx - 1, cy -1);
grfx.DrawLine(pen, cx - 1, 0, 0, cy -1);
}
}
主函数代码
usingSystem;
usingSystem.Drawing;
usingSystem.Windows.Forms;
classClientEllipse : PrintableForm
{
publicnewstaticvoidMain()
{
Application.Run(newClientEllipse());
}
public ClientEllipse()
{
Text = "Client Ellipse";
}
protectedoverridevoid DoPage(Graphics grfx, Color clr, int cx, int cy)
{
grfx.DrawEllipse(newPen(clr),0, 0, cx - 1, cy - 1);
}
}