如何:在 Windows Form 應用程式中顯示預覽列印
您可以使用 PrintPreviewDialog 控制項讓使用者在列印檔案之前經常顯示檔。
若要這樣做,您必須指定 類別的 PrintDocument 實例;這是要列印的檔。 如需搭配元件使用預覽列印 PrintDocument 的詳細資訊,請參閱 How to: Print In Windows Forms Using Print Preview 。
注意
若要在執行時間使用 PrintPreviewDialog 控制項,使用者必須在本機或透過網路安裝印表機,因為元件 PrintPreviewDialog 部分決定列印檔案時檔的外觀。
控制項 PrintPreviewDialog 會 PrinterSettings 使用 類別。 此外,控制項會 PrintPreviewDialog 使用 類別 PageSettings ,就像元件一 PrintPreviewDialog 樣。 控制項 Document 的 屬性中指定的 PrintPreviewDialog 列印檔案是指 和 PageSettings 類別的 PrinterSettings 實例,這些是用來在預覽視窗中轉譯檔。
使用 PrintPreviewDialog 控制項檢視頁面
使用 ShowDialog 方法顯示對話方塊,並指定要使用的 PrintDocument 。
在下列程式碼範例中, Button 控制項的 Click 事件處理常式會開啟 控制項的 PrintPreviewDialog 實例。 列印檔案是在 屬性中 Document 指定。 在下列範例中,未指定列印檔案。
此範例會要求您的表單具有 Button 控制項、 PrintDocument 名為
myDocument
的元件和 PrintPreviewDialog 控制項。Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click ' The print document 'myDocument' used below ' is merely for an example. ' You will have to specify your own print document. PrintPreviewDialog1.Document = myDocument PrintPreviewDialog1.ShowDialog() End Sub
private void button1_Click(object sender, System.EventArgs e) { // The print document 'myDocument' used below // is merely for an example. // You will have to specify your own print document. printPreviewDialog1.Document = myDocument; printPreviewDialog1.ShowDialog(); }
private: void button1_Click(System::Object ^ sender, System::EventArgs ^ e) { // The print document 'myDocument' used below // is merely for an example. // You will have to specify your own print document. printPreviewDialog1->Document = myDocument; printPreviewDialog1->ShowDialog(); }
(Visual C#、Visual C++)將下列程式碼放在表單的建構函式中,以註冊事件處理常式。
this.button1.Click += new System.EventHandler(this.button1_Click);
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應