Share via


방법: 프린터 디바이스 컨텍스트 검색

이 항목에서는 프린터 디바이스 컨텍스트를 검색하는 방법을 설명합니다. CreateDC 함수를 직접 호출하여 프린터 디바이스 컨텍스트를 검색하거나 인쇄 일반 대화 상자에서 반환할 수 있습니다.

일반 인쇄 대화 상자를 표시하면 사용자가 프린터, 문서 페이지 및 인쇄하려는 문서 복사본 수를 선택할 수 있습니다. 공통 인쇄 대화 상자는 데이터 구조에서 이러한 선택 항목을 반환합니다.

이 항목에서는 다음 방법을 사용하여 프린터 디바이스 컨텍스트를 가져오는 방법을 설명합니다.

CreateDC 호출

인쇄할 디바이스를 알고 있는 경우 CreateDC 를 호출하고 해당 정보를 함수에 직접 전달할 수 있습니다. CreateDC 는 인쇄할 콘텐츠를 렌더링할 수 있는 디바이스 컨텍스트를 반환합니다.

디바이스 컨텍스트를 검색하는 가장 간단한 호출은 다음 코드 예제에 나와 있습니다. 이 예제의 코드는 기본 디스플레이 디바이스에 대한 디바이스 컨텍스트를 검색합니다.

    hDC = CreateDC(TEXT("DISPLAY"),NULL,NULL,NULL);

특정 프린터로 렌더링하려면 "WINSPOOL"을 디바이스로 지정하고 프린터의 올바른 이름을 CreateDC에 전달해야 합니다. 디바이스 컨텍스트를 만들 때 디바이스 드라이버에 대한 디바이스별 초기화 데이터를 제공하려는 경우 CreateDC 호출에서 DEVMODE 구조를 전달할 수도 있습니다.

다음 예제에서는 "WINSPOOL" 드라이버가 선택되고 프린터 이름이 이름으로 지정된 CreateDC 에 대한 호출을 보여줍니다.

    printerDC = CreateDC( L"WINSPOOL", printerName, NULL, NULL);

EnumPrinters 함수를 호출하여 CreateDC에 전달할 정확한 프린터 이름 문자열을 가져올 수 있습니다. 다음 코드 예제에서는 EnumPrinters 를 호출하고 로컬 및 로컬로 연결된 프린터의 이름을 가져오는 방법을 보여 줍니다. 필요한 버퍼의 크기를 미리 알 수 없으므로 EnumPrinters 를 두 번 호출합니다. 첫 번째 호출은 필요한 버퍼의 크기를 반환합니다. 이 정보는 필요한 크기의 버퍼를 할당하는 데 사용되며 EnumPrinters 에 대한 두 번째 호출은 원하는 데이터를 반환합니다.

    fnReturn = EnumPrinters(
                PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS,
                NULL,
                1L,                // printer info level
                (LPBYTE)NULL,
                0L,
                &dwNeeded,
                &dwReturned);
    
    if (dwNeeded > 0)
    {
        pInfo = (PRINTER_INFO_1 *)HeapAlloc(
                    GetProcessHeap(), 0L, dwNeeded);
    }

    if (NULL != pInfo)
    {
        dwReturned = 0;
        fnReturn = EnumPrinters(
                PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS,
                NULL,
                1L,                // printer info level
                (LPBYTE)pInfo,
                dwNeeded,
                &dwNeeded,
                &dwReturned);
    }

    if (fnReturn)
    {
        // Review the information from all the printers
        //  returned by EnumPrinters.
        for (i=0; i < dwReturned; i++)
        {
            // pThisInfo[i]->pName contains the printer
            //  name to use in the CreateDC function call.
            //
            // When this desired printer is found in the list of
            //  returned printer, set the printerName value to 
            //  use in the call to CreateDC.

            // printerName = pThisInfo[i]->pName
        }
    }

인쇄 일반 대화 상자 표시

두 개의 일반 인쇄 대화 상자 중에서 선택하여 사용자에게 표시할 수 있습니다. PrintDlgEx 함수를 호출하여 표시할 수 있는 최신 대화 상자와 PrintDlg 함수를 호출하여 표시할 수 있는 이전 스타일 대화 상자입니다. 다음 섹션에서는 애플리케이션에서 각 대화 상자를 호출하는 방법을 설명합니다.

PrintDlgEx 함수 사용

PrintDlgEx 함수를 호출하여 Print 속성 시트를 표시합니다. 사용자는 속성 시트를 사용하여 인쇄 작업에 대한 정보를 지정할 수 있습니다. 예를 들어 사용자는 인쇄할 페이지 범위, 복사본 수 등을 선택할 수 있습니다. PrintDlgEx는 선택한 프린터의 디바이스 컨텍스트에 대한 핸들을 검색할 수도 있습니다. 핸들을 사용하여 프린터에서 출력을 렌더링할 수 있습니다.

PrintDlgEx를 사용하여 프린터 디바이스 컨텍스트를 검색하는 방법을 보여 주는 샘플 코드는 일반 대화 상자 사용의 "인쇄 속성 시트 사용"을 참조하세요.

PrintDlg 함수 사용

Windows 2000 이전 버전의 Windows를 실행하는 시스템과 같이 PrintDlgEx 함수를 지원하지 않는 시스템에서 애플리케이션을 실행해야 하거나 PrintDlgEx 함수가 제공하는 추가 기능이 필요하지 않은 경우 PrintDlg 함수를 사용합니다. 다음 단계에서는 이전 스타일 일반 인쇄 대화 상자를 표시하는 방법을 설명합니다.

  1. PRINTDLG 데이터 구조를 초기화합니다.
  2. PrintDlg를 호출하여 일반 인쇄 대화 상자를 사용자에게 표시합니다.
  3. PrintDlg 호출이 TRUE를 반환하는 경우 반환된 DEVMODE 구조체 메모리를 잠급니다. PrintDlg 호출이 FALSE를 반환하는 경우 사용자는 일반 인쇄 대화 상자에서 취소 단추를 눌렀기 때문에 더 이상 처리할 것이 없습니다.
  4. DEVMODE 구조체의 복사본을 포함할 수 있을 만큼 큰 로컬 메모리 버퍼를 할당합니다.
  5. 반환된 DEVMODE 구조를 로컬로 할당된 구조체에 복사합니다.
  6. PRINTDLG 구조에 반환되고 인쇄 작업을 처리해야 하는 다른 정보를 저장합니다.
  7. PRINTDLG 및 참조하는 메모리 버퍼를 해제합니다.

다음 예제 코드에서는 PrintDlg 함수를 사용하여 디바이스 컨텍스트와 선택한 프린터의 이름을 가져오는 방법을 보여 줍니다.

// Display the printer dialog box so the user can select the 
//  printer and the number of copies to print.
BOOL            printDlgReturn = FALSE;
HDC                printerDC = NULL;
PRINTDLG        printDlgInfo = {0};
LPWSTR            localPrinterName = NULL;
PDEVMODE        returnedDevmode = NULL;
PDEVMODE        localDevmode = NULL;
int                localNumberOfCopies = 0;

// Initialize the print dialog box's data structure.
printDlgInfo.lStructSize = sizeof( printDlgInfo );
printDlgInfo.Flags = 
    // Return a printer device context.
    PD_RETURNDC 
    // Don't allow separate print to file.
    // Remove these flags if you want to support this feature.
    | PD_HIDEPRINTTOFILE        
    | PD_DISABLEPRINTTOFILE 
    // Don't allow selecting individual document pages to print.
    // Remove this flag if you want to support this feature.
    | PD_NOSELECTION;

// Display the printer dialog and retrieve the printer DC.
printDlgReturn = PrintDlg(&printDlgInfo);

// Check the return value.
if (TRUE == printDlgReturn)
{
    // The user clicked OK so the printer dialog box data 
    //  structure was returned with the user's selections.
    //  Copy the relevant data from the data structure and 
    //  save them to a local data structure.

    //
    // Get the HDC of the selected printer
    printerDC = printDlgInfo.hDC;
    
    // In this example, the DEVMODE structure returned by 
    //    the printer dialog box is copied to a local memory
    //    block and a pointer to the printer name that is 
    //    stored in the copied DEVMODE structure is saved.

    //
    //  Lock the handle to get a pointer to the DEVMODE structure.
    returnedDevmode = (PDEVMODE)GlobalLock(printDlgInfo.hDevMode);

    localDevmode = (LPDEVMODE)HeapAlloc(
                        GetProcessHeap(), 
                        HEAP_ZERO_MEMORY | HEAP_GENERATE_EXCEPTIONS, 
                        returnedDevmode->dmSize);

    if (NULL != localDevmode) 
    {
        memcpy(
            (LPVOID)localDevmode,
            (LPVOID)returnedDevmode, 
            returnedDevmode->dmSize);

        // Save the printer name from the DEVMODE structure.
        //  This is done here just to illustrate how to access
        //  the name field. The printer name can also be accessed
        //  by referring to the dmDeviceName in the local 
        //  copy of the DEVMODE structure.
        localPrinterName = localDevmode->dmDeviceName;

        // Save the number of copies as entered by the user
        localNumberOfCopies = printDlgInfo.nCopies;    
    }
    else
    {
        // Unable to allocate a new structure so leave
        //  the pointer as NULL to indicate that it's empty.
    }

    // Free the DEVMODE structure returned by the print 
    //  dialog box.
    if (NULL != printDlgInfo.hDevMode) 
    {
        GlobalFree(printDlgInfo.hDevMode);
    }
}
else
{
    // The user cancelled out of the print dialog box.
}

PrintDlg 함수에 대한 자세한 내용은 일반 대화 상자 사용에서 "인쇄 대화 상자 표시"를 참조하세요.