共用方式為


如何:從使用者收集列印作業資訊

本主題描述如何從使用者收集列印作業資訊。

概述

藉由呼叫 PrintDlg 函式,從使用者收集列印作業資訊。 此函式會顯示 列印 一般對話框給使用者,並在 PRINTDLG 數據結構中傳回列印作業資訊。

當使用者啟動列印作業時,會顯示 [列印 一般] 對話框列印 通用對話框是模態對話方塊,這表示在關閉通用對話框之前,使用者無法與主視窗互動。

收集列印作業資訊

  1. 初始化 PRINTDLG 結構元素。

    在程式顯示 Print common dialog box 之前,必須配置和初始化 PRINTDLG 結構。 然後,它會將此結構傳遞至 PrintDlg 函式,此函式會顯示對話方塊,並傳回相同結構中的列印作業數據。 下列程式代碼範例示範範例程式如何執行此步驟。

    // Initialize the print dialog box's data structure.
    pd.lStructSize = sizeof( pd );
    pd.Flags = 
        // Return a printer device context
        PD_RETURNDC 
        // Don't allow separate print to file.
        | PD_HIDEPRINTTOFILE        
        | PD_DISABLEPRINTTOFILE 
        // Don't allow selecting individual document pages to print.
        | PD_NOSELECTION;
    
  2. 顯示 列印 一般對話框。

    使用初始化 PRINTDLG 結構呼叫 printDlg ,以顯示 Print 一般對話框並收集用戶數據,如下列程式代碼範例所示。

    // Display the printer dialog and retrieve the printer DC
    pdReturn = PrintDlg(&pd);
    
  3. PRINTDLG 結構儲存欄位,然後啟動列印作業。

    PRINTDLG 結構包含描述使用者在列印對話方塊中進行的選項的數據。 PRINTDLG 結構的一些成員是全域記憶體對象的控制代碼。 列印範例程式 會將數據從全域記憶體物件複製到程式管理的其他欄位,並將其他欄位從 PRINTDLG 結構複製到程式所定義之數據結構中的欄位。

    當您將資料從 PRINTDLG 結構儲存在程式的數據結構之後,您可以開啟 [列印進度] 對話方塊。 列印進度對話框程式會處理對話框訊息,並啟動列印處理線程。

    下列程式代碼範例示範如何將數據從 PRINTDLG 結構複製到程式的數據結構,以及如何啟動列印作業。

    // A printer was returned so copy the information from 
    //  the dialog box structure and save it to the application's
    //  data structure.
    //
    //  Lock the handles to get pointers to the memory they refer to.
    PDEVMODE    devmode = (PDEVMODE)GlobalLock(pd.hDevMode);
    LPDEVNAMES  devnames = (LPDEVNAMES)GlobalLock(pd.hDevNames);
    
    // Free any old devmode structures and allocate a new one and
    // copy the data to the application's data structure.
    if (NULL != threadInfo->devmode)
    {
        HeapFree(GetProcessHeap(), 0L, threadInfo->devmode);
    }
    
    threadInfo->devmode = (LPDEVMODE)HeapAlloc(
        GetProcessHeap(), 
        PRINT_SAMPLE_HEAP_FLAGS, 
        devmode->dmSize);
    
    if (NULL != threadInfo->devmode) 
    {
        memcpy(
            (LPVOID)threadInfo->devmode,
            devmode, 
            devmode->dmSize);
    }
    else
    {
        // Unable to allocate a new structure so leave
        // the pointer as NULL to indicate that it's empty.
    }
    
    // Save the printer name from the devmode structure
    //  This is to make it easier to use. It could be
    //  used directly from the devmode structure.
    threadInfo->printerName = threadInfo->devmode->dmDeviceName;
    
    // Set the number of copies as entered by the user
    threadInfo->copies = pd.nCopies;
    
    // Some implementations might support printing more than
    // one package in a print job. For this program, only
    // one package (XPS document) can be printed per print job.
    threadInfo->packages = 1;
    
    // free allocated buffers from PRINTDLG structure
    if (NULL != pd.hDevMode) GlobalFree(pd.hDevMode);
    if (NULL != pd.hDevNames) GlobalFree(pd.hDevNames);
    
    // Display the print progress dialog box
    DialogBox(
        threadInfo->applicationInstance, 
        MAKEINTRESOURCE(IDD_PRINT_DLG), 
        hWnd, 
        PrintDlgProc);
    
  4. 如果使用者在 [列印 共用] 對話框中點擊 [取消] 按鈕,則不會執行任何進一步的處理。