Click here to Skip to main content
15,884,099 members
Articles / Desktop Programming / MFC
Article

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

Rate me:
Please Sign up or sign in to vote.
1.20/5 (11 votes)
19 Jan 2005 66.7K   1.1K   14   11
Use DeviceCapabilities function to get the paper name of a printer.

Sample Image - Printer_Paper_Name.jpg

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);
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Japan Japan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
ominion1-Oct-15 12:06
ominion1-Oct-15 12:06 
GeneralMy vote of 1 Pin
ursul_boa7-Feb-14 5:30
ursul_boa7-Feb-14 5:30 
GeneralEnumForms Pin
Duncan Edwards Jones18-Sep-07 1:16
professionalDuncan Edwards Jones18-Sep-07 1:16 
Generalarigatoo gozaimasu! Pin
Windmiller18-Sep-07 0:59
Windmiller18-Sep-07 0:59 
QuestionHow to select a custom paper name? Pin
julycy18-May-07 22:36
julycy18-May-07 22:36 
GeneralProblem Running Included Project Pin
Rommel the iCeMAn17-Jan-05 10:58
Rommel the iCeMAn17-Jan-05 10:58 
GeneralRe: Problem Running Included Project Pin
Anonymous18-Jan-05 12:52
Anonymous18-Jan-05 12:52 
GeneralRe: Problem Running Included Project Pin
Rommel the iCeMAn20-Jan-05 2:34
Rommel the iCeMAn20-Jan-05 2:34 
QuestionDoes anyone knows how to get the paper name using a system function call? Pin
Valentin Mocanu8-Oct-04 3:40
Valentin Mocanu8-Oct-04 3:40 
AnswerRe: Does anyone knows how to get the paper name using a system function call? Pin
Rommel the iCeMAn17-Jan-05 11:07
Rommel the iCeMAn17-Jan-05 11:07 
AnswerRe: Does anyone knows how to get the paper name using a system function call? Pin
Ole Thomsen19-Jan-05 23:28
Ole Thomsen19-Jan-05 23:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.