文章編號: 322710 - 上次校閱: 2007年3月29日 - 版次: 2.3

如何: 引發和控制從 Visual Basic 的列印對話方塊

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。

在此頁中

全部展開 | 全部摺疊

結論

您可以使用 Vbprndlg.dll 檔案而不是 Visual Basic CommonDialog 控制項 [列印] 對話方塊方塊部分。這個檔案公開 (Expose) 屬性,並提供 CommonDialog 控制項中沒有的功能。

CommonDialog 控制項在 Visual Basic 中的允許要顯示 [列印] 對話方塊,但它不授予存取的許多屬性,可以在 [列印] 對話方塊中設定及由 印表機 物件。因為的結構對齊問題 (也就是使用者定義型別) 從 Visual Basic 進行 Win32 應用程式設計介面 (API) 函數時, 您不能可靠地藉由使用這些 API 呼叫取得其他資訊。為一的解決方法本文會提供您可以使用來代替 CommonDialog 控制項的 [印表機] 部份的 DLL。



下載控制項

下列檔案是可以從 Microsoft 下載中心 」 下載:
Vbprndlg.exe (http://download.microsoft.com/download//vb60pro/utility/1.0/win98mexp/en-us/vbprndlg.exe)
發行日期: 年九月 30 2002

如需有關如何下載 Microsoft 支援檔案的詳細資訊,按一下 [下列面的文件編號,檢視 「 Microsoft 知識庫 」 中的發行項]:
119591? (http://support.microsoft.com/kb/119591/EN-US/ ) 如何從線上服務取得 Microsoft 支援檔案
Microsoft 已掃描這個檔案有無病毒。Microsoft 使用已張貼檔案的日期中的 [可用的最新病毒偵測軟體。檔案儲存在安全性強化的伺服器上,以避免任何未經授權的更改至檔案。 Vbprndlg.exe 檔案包含下列檔案:

摺疊此表格展開此表格
檔案名稱大小
Vbprndlg.dll100.0 KB
Vbprndlg.exp1.0 KB
Vbprndlg.lib1.72 KB
Vbprndlg.dep118 位元組
Readme.txt10.9 KB


安裝 Vbprndlg.exe

  1. 將檔案解壓縮到一個位置,將不會被刪除且具有簡單的路徑。這些類型的檔案通常置於 [Windows 下的 [System32 目錄或 Winnt 目錄。
  2. 按一下 [開始],然後按一下 [執行]。
  3. 輸入下列,然後按一下 [確定]
    RegSvr32 C:\Windows\System32\VBPrnDlg.dll
    請確定您使用實際的完整路徑至 DLL,這個命令的第二個的一部分。

    您會收到成功登錄 DLL 的訊息。

Printerdlg 類別文件

這個 DLL 是會新增為參照到 Visual Basic 應用程式的 COM 元件。您將加入 DLL 後您可以在程式碼中使用它,將顯示 [列印] 對話方塊,並取得使用者選取的資訊。 這項行為是在 Visual Basic CommonDialog 控制項中的 [列印] 對話方塊的行為相同。

對話方塊屬性

關於任何這些屬性的文件請參閱 CommonDialog 控制項之文件。Printer.hdc 是唯讀的但所有下列屬性除了那些直接標示有星號 (*) 對應到 印表機 上的物件當您使用下列語法:
Printer.Copies = printDlg.Copies
				
  • 複本
  • 方向
  • ColorMode
  • DriverName
  • 雙工
  • PaperBin
  • PaperSize
  • 連接埠
  • PrintQuality
  • hDC
  • PrinterName *
  • 分鐘 *
  • 最大值 *
  • FromPage *
  • ToPage *
PrinterName 對應到 DeviceName印表機 物件中。最後四個屬性標示有星號 (*) 用來決定是否列印的可用的頁數子集。 若要以便 列印] 對話方塊中 選取頁 部分 最大值 必須設為一個數字大於 最小值。如果 最小值 大於 最大,[] 對話方塊隨即出現時,就會發生錯誤。如果使用者選取了子集 PrinterDlg 物件確保它是內部的 最小值最大,界限,而且它會傳回在 FromPage] 和 [ToPage 屬性。這些屬性並不直接對應到 印表機] 物件讓程式,會引發 [] 對話方塊,並控制列印必須處理這些屬性。

對話方塊控制項方法和屬性

  • 旗標-這是 CommonDialog 控制項上的 [標幟] 屬性一樣。它可控制的一些在對話方塊的功能。這個值會設定使用 「 或者 」 具有不同常數中 VBPrinterConstants 結構。例如:
    printDlg.Flags = VBPrinterConstants.cdlPDDisablePrintToFile _
                         Or VBPrinterConstants.cdlPDNoPageNums
    					
    這些常數是:
    Constant                  Value       Description
    ---------------------------------------------------------------------------
    cdlPDAllPages             &H0         Returns or sets the state of the All 
                                          Pages option button.
    
    cdlPDCollate              &H10        Returns or sets the state of the  
                                          Collate check box.
    
    cdlPDDisablePrintToFile   &H80000     Disables the Print To File check box.
    
    cdlPDHelpButton           &H800       Causes the dialog box to display the 
                                          Help button.
    
    cdlPDHidePrintToFile      &H100000    Hides the Print To File check box.
    
    cdlPDNoPageNums           &H8         Disables the Pages option button and 
                                          the associated edit control.
    
    cdlPDNoSelection          &H4         Disables the Selection option button.
    
    cdlPDNoWarning            &H80        Prevents a warning message from being
                                          displayed when there is no default 
                                          printer.
    
    cdlPDPageNums             &H2         Returns or sets the state of the 
                                          Pages option button.
    
    cdlPDPrintSetup           &H40        Causes the system to display the 
                                          Print Setup dialog box instead of 
                                          the Print dialog box.
    
    cdlPDPrintToFile          &H20        Returns or sets the state of the 
                                          Print To File check box.
    
    cdlPDReturnDC             &H100       Returns a device context for the 
                                          printer selection made in the dialog 
                                          box. The device context is returned 
                                          in the hDC property of the dialog 
                                          box.
    
    cdlPDReturnDefault        &H400       Returns the default printer name.
    
    cdlPDReturnIC             &H200       Returns an information context for 
                                          the printer selection made in the 
                                          dialog box. An information context 
                                          provides a fast way to get 
                                          information about the device without 
                                          creating a device context. The 
                                          information context is returned in 
                                          the hDC property of the dialog box.
    
    cdlPDSelection            &H1         Returns or sets the state of the
                                          Selection option button. If neither 
                                          cdlPDPageNums nor cdlPDSelection is 
                                          specified, the All option button is 
                                          in the selected state.
    
    cdlPDUseDevModeCopies     &H40000     If a printer driver does not support
                                          multiple copies, setting this flag 
                                          disables the Number of Copies spinner
                                          control in the Print dialog box. If a
                                          driver does support multiple copies, 
                                          setting this flag indicates that the 
                                          dialog box stores the requested 
                                          number of copies in the Copies 
                                          property.
    
    					
    您可以使用 Flags 屬性來決定是否使用者選擇一個欄位,如 [列印至檔案。執行此動作查看控制項具有傳回從顯示對話方塊時,是否已經設定了 cdlPDPrintToFile 旗標,請參閱。

  • CancelError-這是布林旗標,以判斷是否會傳回錯誤給程式是否使用者按一下 [取消]。預設情況下,這是 False,因此沒有發生一個錯誤,如果使用者按一下 [取消]。當這設定為 True 時,錯誤號碼 32755 (& H7FF3) 發生於使用者按一下[取消]。您可以進行這項錯誤來處理使用者取消 [] 對話方塊的情況下補漏白。
  • ShowPrinter-這是唯一的方法,這個類別中。呼叫這個方法會顯示 [] 對話方塊。這會做為參數接受呼叫視窗的控制代碼。比方說下列方法會傳回布林值:
    printDlg.ShowPrinter Me.hWnd
    						
    這可以忽略。如果使用者按一下 列印] 和 [False 否則 (也就是如果使用者按一下 [取消]),則值為 True。
注意: 在所有這些範例 printDlg 是包含在這個 DLL 的類別的執行個體。它使用下列程式碼來宣告:
Dim printDlg As PrinterDlg
Set printDlg = New PrinterDlg
				

若要建置範例的步驟

若要建立範例應用程式將用來判斷使用者的喜好設定,然後將它們寫入 [偵錯] 視窗的 DLL,請依照下列步驟執行:
  1. 在 [Microsoft Visual Basic,建立新的標準 EXE 專案。依照預設值,會建立 Form1。
  2. 按一下 [專案] 功能表 引用項目
  3. 選取 Microsoft VB 印表機 Dialog(PSS),然後按一下 [確定]
  4. 命令按鈕 控制項加入至表單。根據預設值,Command1,即建立。
  5. 連按兩下 Command1
  6. 下列程式碼貼 Command1_Click 事件中:
    Dim printDlg As PrinterDlg
    Set printDlg = New PrinterDlg
    ' Set the starting information for the dialog box based on the current
    ' printer settings.
    printDlg.PrinterName = Printer.DeviceName
    printDlg.DriverName = Printer.DriverName
    printDlg.Port = Printer.Port
    
    ' Set the default PaperBin so that a valid value is returned even
    ' in the Cancel case.
    printDlg.PaperBin = Printer.PaperBin
    
    ' Set the flags for the PrinterDlg object using the same flags as in the
    ' common dialog control. The structure starts with VBPrinterConstants.
    printDlg.Flags = VBPrinterConstants.cdlPDNoSelection _
                     Or VBPrinterConstants.cdlPDNoPageNums _
                     Or VBPrinterConstants.cdlPDReturnDC
    Printer.TrackDefault = False
    
    ' When CancelError is set to True the ShowPrinterDlg will return error
    ' 32755. You can handle the error to know when the Cancel button was
    ' clicked. Enable this by uncommenting the lines prefixed with "'**".
    '**printDlg.CancelError = True
    
    ' Add error handling for Cancel.
    '**On Error GoTo Cancel
    If Not printDlg.ShowPrinter(Me.hWnd) Then
        Debug.Print "Cancel Selected"
        Exit Sub
    End If
    
    'Turn off Error Handling for Cancel.
    '**On Error GoTo 0
    Dim NewPrinterName As String
    Dim objPrinter As Printer
    Dim strsetting As String
    
    ' Locate the printer that the user selected in the Printers collection.
    NewPrinterName = UCase$(printDlg.PrinterName)
    If Printer.DeviceName <> NewPrinterName Then
        For Each objPrinter In Printers
           If UCase$(objPrinter.DeviceName) = NewPrinterName Then
                Set Printer = objPrinter
           End If
        Next
    End If
    
    ' Copy user input from the dialog box to the properties of the selected printer.
    Printer.Copies = printDlg.Copies
    Printer.Orientation = printDlg.Orientation
    Printer.ColorMode = printDlg.ColorMode
    Printer.Duplex = printDlg.Duplex
    Printer.PaperBin = printDlg.PaperBin
    Printer.PaperSize = printDlg.PaperSize
    Printer.PrintQuality = printDlg.PrintQuality
    
    ' Display the results in the immediate (Debug) window.
    ' NOTE: Supported values for PaperBin and Size are printer specific. Some
    ' common defaults are defined in the Win32 SDK in MSDN and in Visual Basic.
    ' Print quality is the number of dots per inch.
    With Printer
        Debug.Print .DeviceName
        If .Orientation = 1 Then
            strsetting = "Portrait. "
        Else
            strsetting = "Landscape. "
        End If
        Debug.Print "Copies = " & .Copies, "Orientation = " & _
           strsetting
        If .ColorMode = 1 Then
            strsetting = "Black and White. "
        Else
            strsetting = "Color. "
        End If
        Debug.Print "ColorMode = " & strsetting
        If .Duplex = 1 Then
            strsetting = "None. "
        ElseIf .Duplex = 2 Then
            strsetting = "Horizontal/Long Edge. "
        ElseIf .Duplex = 3 Then
            strsetting = "Vertical/Short Edge. "
        Else
            strsetting = "Unknown. "
        End If
        Debug.Print "Duplex = " & strsetting
        Debug.Print "PaperBin = " & .PaperBin
        Debug.Print "PaperSize = " & .PaperSize
        Debug.Print "PrintQuality = " & .PrintQuality
        If (printDlg.Flags And VBPrinterConstants.cdlPDPrintToFile) = _
           VBPrinterConstants.cdlPDPrintToFile Then
             Debug.Print "Print to File Selected"
        Else
             Debug.Print "Print to File Not Selected"
        End If
        Debug.Print "hDC = " & printDlg.hDC
    End With
    Exit Sub
    '**Cancel:
    '**If Err.Number = 32755 Then
    '**    Debug.Print "Cancel Selected"
    '**Else
    '**    Debug.Print "A nonCancel Error Occured - "; Err.Number
    '**End If
    					
  7. 按下 F5 鍵以開始偵錯,並啟動應用程式。按一下以開啟 [列印] 對話方塊的 Command1
  8. 從 [列印] 對話方塊中傳回之後尋找在傳回的輸出的 [偵錯] 視窗中)。
注意: 如果您想要引發並處理當使用者按一下 [取消] (與 CommonDialog 控制項就會發生的行為類似) 的特定錯誤,取消註解程式行前面加上"' 貴 」。

?考

如需有關使用 Visual Basic 中的 [列印] 對話方塊的詳細資訊,按一下下面的文件編號,檢視 「 Microsoft 知識庫 」 中的發行項:
173981? (http://support.microsoft.com/kb/173981/EN-US/ ) PRB: 行為差異的不同平台上的列印對話方塊
如需有關印表機設定的詳細資訊,按一下 [下面的文件編號,檢視 「 Microsoft 知識庫 」 中 「 文件]:
190218? (http://support.microsoft.com/kb/190218/EN-US/ ) HOWTO: 從印表機驅動程式擷取設定
194789? (http://support.microsoft.com/kb/194789/EN-US/ ) HOWTO: 決定與 DeviceCapabilities API 可用 PaperBins

這篇文章中的資訊適用於:
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
關鍵字:?
kbmt kbdownload kbcmndlgprint kbfile kbhowto kbhowtomaster kbsample KB322710 KbMtzh
機器翻譯機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:322710? (http://support.microsoft.com/kb/322710/en-us/ )
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。