Microsoft KB Archive/117565

= How To Getting the Current Printer Settings in MFC =

Article ID: 117565

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 1.0 Professional Edition

 Microsoft Visual C++ 1.5 Professional Edition

 Microsoft Visual C++ 1.51

 Microsoft Visual C++ 1.52 Professional Edition</li></ul>

 Microsoft Visual C++ 1.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.1</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 4.1 Subscription</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q117565

<div class="summary_section">

SUMMARY
MFC provides a great deal of information on the current printer in the framework printing functions. However, information on the current printer is limited outside of these functions.

<div class="moreinformation_section">

MORE INFORMATION
The only way to gain access to the current printers settings is through the m_hDevMode and m_hDevNames structures stored in CWinApp. These members are accessed through a PRINTDLG structure whose pointer is passed to the GetPrinterDeviceDefaults member function of a class derived from CWinApp class. Because the return values, hDevMode and hDevNames, are HGLOBAL variables, ::GlobalLock must be called to return a pointer to the structures. Using this pointer, you can extract information from the structure to determine the current state of the printer. Please see DEVMODE and DEVNAMES in the SDK online Help for more information on the contents of these structures.

The sample code below is a member function of a CView-derived class that returns the current page size of the printer by using GetDeviceCaps on a CDC object created with the printer settings:

Sample Code
/* Compile options needed: none */

BOOL CMyView::GetPageSize(CSize &nRetVal) {        PRINTDLG FAR * pPrintDlg = new PRINTDLG; BOOL bRet = FALSE;

// Get the current printer's settings.

if(AfxGetApp->GetPrinterDeviceDefaults(pPrintDlg)) {

// Get pointers to the two setting structures.

DEVNAMES FAR *lpDevNames = (DEVNAMES FAR *)::GlobalLock(pPrintDlg->hDevNames);

DEVMODE FAR *lpDevMode = (DEVMODE FAR *)::GlobalLock(pPrintDlg->hDevMode);

// Get the specific driver information.

CString szDriver((LPTSTR)lpDevNames +                                lpDevNames->wDriverOffset); CString szDevice((LPTSTR)lpDevNames +                                lpDevNames->wDeviceOffset); CString szOutput((LPTSTR)lpDevNames +                                lpDevNames->wOutputOffset);

// Create a CDC object according to the current settings.

CDC pDC; pDC.CreateDC(szDriver, szDevice, szOutput, lpDevMode);

// Query this CDC object for the width and height of the current // page.

nRetVal.cx = pDC.GetDeviceCaps(HORZSIZE); nRetVal.cy = pDC.GetDeviceCaps(VERTSIZE);

// Get rid of the CDC object.

pDC.DeleteDC;

// Unlock the pointers to the setting structures.

::GlobalUnlock(pPrintDlg->hDevMode);

bRet = TRUE; }      delete pPrintDlg; return bRet; }

Additional query words: kbNoUpdate

Keywords: kbhowto kbprint kbcmndlgprint KB117565

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.