다음을 통해 공유


방법: 사용자로부터 인쇄 작업 정보 수집

이 항목에서는 사용자로부터 인쇄 작업 정보를 수집하는 방법을 설명합니다.

개요

PrintDlg 함수를 호출하여 사용자로부터 인쇄 작업 정보를 수집합니다. 이 함수는 일반 인쇄 대화 상자를 사용자에게 표시하고 PRINTDLG 데이터 구조에서 인쇄 작업 정보를 반환합니다.

사용자가 인쇄 작업을 시작할 때 일반 인쇄 대화 상자가 표시됩니다. 일반 인쇄 대화 상자는 모달 대화 상자입니다. 즉, 사용자가 공통 대화 상자를 닫을 때까지 기본 창과 상호 작용할 수 없습니다.

인쇄 작업 정보 수집

  1. PRINTDLG 구조체 요소를 초기화합니다.

    프로그램에서 일반 인쇄 대화 상자를 표시하려면 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를 호출하여 일반 인쇄 대화 상자를 표시하고 사용자 데이터를 수집합니다.

    // 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. 사용자가 일반 인쇄 대화 상자에서 취소 단추를 클릭하면 추가 처리가 수행되지 않습니다.