Simple way to get the name of the paper a printer supports






1.20/5 (9 votes)
Jul 21, 2004

67583

1068
Use DeviceCapabilities function to get the paper name of a printer.
Introduction
There are lots of articles on Code Project about printing procedure. We think this is a topic which is missing and so decided to give some idea. During one of our projects, we had to select the paper names and size, a particular printer supports. For the time being, we are giving the exe and some code; when time permits, we will describe in detail the procedure.
First we define a structure:
typedef struct PaperName{ WORD dwType; char szName[100]; }PAPERNAME;
and initialize it with:
PAPERNAME g_objPaperName[MAX_PAPER_COUNT] = { { DMPAPER_LETTER, "Letter, 8 1/2- by 11-inches" }, { DMPAPER_LEGAL, "Legal, 8 1/2- by 14-inches " }, { DMPAPER_A4, "A4, Sheet, 210- by 297-millimeters " }, { DMPAPER_CSHEET, "C Sheet, 17- by 22-inches " }, { DMPAPER_DSHEET, "D Sheet, 22- by 34-inches " }, { DMPAPER_ESHEET, "E Sheet, 34- by 44-inches " }, { DMPAPER_LETTERSMALL, "Letter Small, 8 1/2- by 11-inches " }, { DMPAPER_TABLOID, "Tabloid, 11- by 17-inches " }, { DMPAPER_LEDGER, "Ledger, 17- by 11-inches " }, { DMPAPER_STATEMENT, "Statement, 5 1/2- by 8 1/2-inches " }, { DMPAPER_EXECUTIVE, "Executive, 7 1/4- by 10 1/2-inches" }, { DMPAPER_A3, "A3 sheet, 297- by 420-millimeters " }, { DMPAPER_A4SMALL, "A4 small sheet, 210- by 297-millimeters " }, { DMPAPER_A5, "A5 sheet, 148- by 210-millimeters " }, { DMPAPER_B4, "B4 sheet, 250- by 354-millimeters " }, { DMPAPER_B5, "B5 sheet, 182- by 257-millimeter paper" }, { DMPAPER_FOLIO, "Folio, 8 1/2- by 13-inch paper " }, { DMPAPER_QUARTO, "Quarto, 215- by 275-millimeter paper " }, { DMPAPER_NOTE, "Note, 8 1/2- by 11-inches " }, { DMPAPER_ENV_9, "#9 Envelope, 3 7/8- by 8 7/8-inches " }, { DMPAPER_ENV_10, "#10 Envelope, 4 1/8- by 9 1/2-inches " }, { DMPAPER_ENV_11, "#11 Envelope, 4 1/2- by 10 3/8-inches " }, { DMPAPER_ENV_12, "#12 Envelope, 4 3/4- by 11-inches " }, { DMPAPER_ENV_14, "#14 Envelope, 5- by 11 1/2-inches " }, { DMPAPER_ENV_DL, "DL Envelope, 110- by 220-millimeters " }, { DMPAPER_ENV_C5, "C5 Envelope, 162- by 229-millimeters " }, { DMPAPER_ENV_C3, "C3 Envelope, 324- by 458-millimeters " }, { DMPAPER_ENV_C4, "C4 Envelope, 229- by 324-millimeters " }, { DMPAPER_ENV_C6, "C6 Envelope, 114- by 162-millimeters " }, { DMPAPER_ENV_C65, "C65 Envelope, 114- by 229-millimeters " }, { DMPAPER_ENV_B4, "B4 Envelope, 250- by 353-millimeters " }, { DMPAPER_ENV_B5, "B5 Envelope, 176- by 250-millimeters " }, { DMPAPER_ENV_B6, "B6 Envelope, 176- by 125-millimeters " }, { DMPAPER_ENV_ITALY, "Italy Envelope, 110- by 230-millimeters " }, { DMPAPER_ENV_MONARCH, "Monarch Envelope, 3 7/8- by 7 1/2-inches " }, { DMPAPER_ENV_PERSONAL, "6 3/4 Envelope, 3 5/8- by 6 1/2-inches " }, { DMPAPER_FANFOLD_US, "US Std Fanfold, 14 7/8- by 11-inches " }, { DMPAPER_FANFOLD_STD_GERMAN, "German Std Fanfold, 8 1/2- by 12-inches " }, { DMPAPER_FANFOLD_LGL_GERMAN, "German Legal Fanfold, 8 1/2- by 13-inches " } };
Next step is to get the devmode from the name of the printer.
On selecting the printer name from the list box, we call GetDeviceMode
function to get the device mode (DEVMODE
) struct
. Using the DEVMODE
, we call DeviceCapabilities
function which returns the number of papers the printer supports. Then create an array of that size, and call the DeviceCapabilities
function again to fill the array. Now, each member of the array represents a paper type. We compare each member of the array with the (WORD
) dwType
variable of the structure initialized earlier. The WORD
that matches represents the paper name from the structure. The following function describes the above procedure:
void CPrintInfoDlg::OnSelchangeListPrntName() { DEVMODE DeviceMode; /* Remove All Data from the ComboBox */ m_wndPaperCombo.ResetContent(); /* Get the Selected Position */ int nPos = m_wndListName.GetCurSel(); if(nPos < 0){ return; } /* Get the name of the printer */ CString strPrntName = ""; m_wndListName.GetText(nPos, strPrntName); /* Get the DEVMOCE structure */ GetDeviceMode(&DeviceMode, strPrntName); /* Get the Printer Paper Settings */ int nPaperCount = DeviceCapabilities(strPrntName, "LPT1", DC_PAPERS, NULL, &DeviceMode); /* Allocate Memory */ LPSTR lpArray = new char[nPaperCount * sizeof(WORD)]; /* Get the Paper Types */ DeviceCapabilities(strPrntName, "LPT1", DC_PAPERS, lpArray, &DeviceMode); /* Fill The Combo Box */ FillCombo(lpArray, nPaperCount); delete [] lpArray; /* Set the combobox text */ CMainFrame *mf = (CMainFrame*)AfxGetApp()->m_pMainWnd; mf->m_objPrinter.m_strMyClPageSize.TrimRight(); if(m_wndPaperCombo.SelectString(0, mf->m_objPrinter.m_strMyClPageSize) == CB_ERR){ m_wndPaperCombo.SetCurSel(0); } }
BOOL CPrintInfoDlg::FillCombo(LPSTR lpArray, int nCount) { /* Traverse the array to get the paper type */ for(int nCnt=0; nCnt<nCount; nCnt++){ /* Get the Paper Name */ CString strPaperName = (GetPaperName(lpArray[nCnt])); if(strPaperName.IsEmpty()){ continue; } m_wndPaperCombo.AddString(strPaperName); } return(TRUE); }
CString CPrintInfoDlg::GetPaperName(WORD dwType) { CString strPaperName; for(int nCnt=0; nCnt<MAX_PAPER_COUNT; nCnt++){ if(dwType == g_objPaperName[nCnt].dwType){ strPaperName = g_objPaperName[nCnt].szName; break; } } return(strPaperName); }