如何:在 Windows 窗体中打印图形
通常,需要在基于 Windows 的应用程序中打印图形。 Graphics 类提供了将对象绘制到设备的方法,例如屏幕或打印机。
打印图形
将 PrintDocument 组件添加到窗体。
在 PrintPage 事件处理程序中,使用 PrintPageEventArgs 类的 Graphics 属性指示打印机打印哪种图形。
以下代码示例显示了一个事件处理程序,用于在边框内创建一个蓝色椭圆。 该矩形具有以下位置和尺寸:从 100 开始,150 宽度为 250,高度为 250。
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage e.Graphics.FillEllipse(Brushes.Blue, New Rectangle(100, 150, 250, 250)) End Sub
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { e.Graphics.FillRectangle(Brushes.Blue, new Rectangle(100, 150, 250, 250)); }
private: void printDocument1_PrintPage(System::Object ^ sender, System::Drawing::Printing::PrintPageEventArgs ^ e) { e->Graphics->FillRectangle(Brushes::Blue, Rectangle(100, 150, 250, 250)); }
(Visual C# 和 Visual C++)将以下代码置于表单的构造函数中以注册事件处理程序。
this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler (this.printDocument1_PrintPage);
this->printDocument1->PrintPage += gcnew System::Drawing::Printing::PrintPageEventHandler (this, &Form1::printDocument1_PrintPage);