HOW TO:在 Windows Form 中選擇附加至使用者電腦的印表機
更新:2007 年 11 月
使用者通常會想要選用預設印表機以外的印表機來進行列印工作。經由使用 PrintDialog 元件,您可以讓使用者從目前安裝的印表機中選擇他要的印表機。透過 PrintDialog 元件,PrintDialog 元件的 DialogResult 會被擷取並用來選取印表機。
在下列程序中,已經選取了要列印至預設印表機的文字檔案,接著 PrintDialog 類別會具現化。
若要選取印表機然後列印檔案
請用 PrintDialog 元件來選取要使用的印表機。
在下列程式碼範例中,正在處理的有兩個事件。第一個是 Button 控制項的 Click 事件,PrintDialog 類別會具現化,而 DialogResult 屬性中則會擷取使用者所選取的印表機。
第二個事件是 PrintDocument 元件的 PrintPage 事件,用來將範例文件列印至所指定的印表機。
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim PrintDialog1 As New PrintDialog() PrintDialog1.Document = PrintDocument1 Dim result As DialogResult = PrintDialog1.ShowDialog() If (result = DialogResult.OK) Then PrintDocument1.Print() End If End Sub Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage e.Graphics.FillRectangle(Brushes.Red, New Rectangle(500, 500, 500, 500)) End Sub
private void button1_Click(object sender, System.EventArgs e) { PrintDialog printDialog1 = new PrintDialog(); printDialog1.Document = printDocument1; DialogResult result = printDialog1.ShowDialog(); if (result == DialogResult.OK) { printDocument1.Print(); } } private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { e.Graphics.FillRectangle(Brushes.Red, new Rectangle(500, 500, 500, 500)); }
private void button1_Click(Object sender, System.EventArgs e) { PrintDialog printDialog1 = new PrintDialog(); printDialog1.set_Document(printDocument1); DialogResult result = printDialog1.ShowDialog(); if (result == DialogResult.OK) { printDocument1.Print(); } } private void printDocument1_PrintPage(Object sender, System.Drawing.Printing.PrintPageEventArgs e) { e.get_Graphics().FillRectangle(Brushes.get_Red(), new Rectangle(500, 500, 500, 500)); }
private: void button1_Click(System::Object ^ sender, System::EventArgs ^ e) { PrintDialog ^ printDialog1 = gcnew PrintDialog(); printDialog1->Document = printDocument1; System::Windows::Forms::DialogResult result = printDialog1->ShowDialog(); if (result == DialogResult::OK) { printDocument1->Print(); } } private: void printDocument1_PrintPage(System::Object ^ sender, System::Drawing::Printing::PrintPageEventArgs ^ e) { e->Graphics->FillRectangle(Brushes::Red, Rectangle(500, 500, 500, 500)); }
(Visual C#、Visual J# 和 Visual C++) 將下列程式碼加入表單的建構函式以註冊事件處理常式。
this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler (this.printDocument1_PrintPage); this.button1.Click += new System.EventHandler(this.button1_Click);
this.printDocument1.add_PrintPage(new System.Drawing.Printing.PrintPageEventHandler (this.printDocument1_PrintPage)); this.button1.add_Click(new System.EventHandler(this.button1_Click));
this->printDocument1->PrintPage += gcnew System::Drawing::Printing::PrintPageEventHandler (this, &Form1::printDocument1_PrintPage); this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);