CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 1999
    Location
    Australia
    Posts
    370

    Initialising CPrintDialog with my own DevMode....

    In my application I am bringing up a CPrintDialog to allow the user to choose a printer, page orientation, etc. However, I cannot find a way to remember the settings so that the next time it comes up it will show the user the options they chose previously.

    Thanks

    Christian


  2. #2
    Join Date
    Oct 1999
    Location
    WA
    Posts
    2,393

    Re: Initialising CPrintDialog with my own DevMode....

    Keep the PRINTDLG.hDevMode handle(or make a copy of DEVMODE), make sure it's not deleted, set it before calling the print dialog box.

    The GDI book to read: http://www.fengyuan.com

  3. #3
    Join Date
    Nov 1999
    Location
    Copenhagen, Denmark
    Posts
    265

    Re: Initialising CPrintDialog with my own DevMode....

    How about writing them to the Registry? You could read them in like:


    BOOL CMyListView::OnPreparePrinting(CPrintInfo *pInfo)
    {
    CWinApp *pApp = ::AfxGetApp();
    ASSERT(pApp);
    PRINTDLG *pDlg = &(pInfo->m_pPD->m_pd);

    //get printer defaults
    if(!pInfo->m_pPD->GetDefaults())
    {
    MessageBox("Unable to get printer defaults.\n", "OnPreparePrinting", STOP);
    return FALSE;
    }

    /////////////////////////////////////////////////////////////////////
    // load printer settings from registry

    //lock, initialize, unlock device mode memory
    DEVMODE *pDevMode = NULL;
    if(!(pDevMode = (DEVMODE *)::GlobalLock(pDlg->hDevMode)))
    {
    TRACE0("GlobalLock (DEVMODE) failed.\n");
    ::GlobalFree(pDlg->hDevMode);
    ::GlobalFree(pDlg->hDevNames);
    return FALSE;
    }

    //printer mode
    ::lstrcpy
    (
    (LPTSTR)(pDevMode->dmDeviceName),
    (LPCTSTR)(pApp->GetProfileString(REG_SECTION_PRINTING, REG_ENTRY_DEVICENAME, (LPCTSTR)(pDevMode->dmDeviceName)))
    );

    pDevMode->dmSpecVersion = pApp->GetProfileInt(REG_SECTION_PRINTING, REG_ENTRY_SPECVERSION, pDevMode->dmSpecVersion);
    pDevMode->dmDriverVersion = pApp->GetProfileInt(REG_SECTION_PRINTING, REG_ENTRY_DRIVERVERSION, pDevMode->dmDriverVersion);
    pDevMode->dmSize = sizeof(DEVMODE); pDevMode->dmDriverExtra = 0;

    //page mode

    pDevMode->dmOrientation = pApp->GetProfileInt(REG_SECTION_PRINTING, REG_ENTRY_PAGEORIENTATION, REG_DEFAULT_PAGEORIENTATION);
    pDevMode->dmPaperSize = pApp->GetProfileInt(REG_SECTION_PRINTING, REG_ENTRY_PAGESIZE, REG_DEFAULT_PAGESIZE);
    pDevMode->dmFields = DM_ORIENTATION | DM_PAPERSIZE; ::GlobalUnlock(pDlg->hDevMode);

    //lock, initialize, unlock device names memory
    DEVNAMES *pDevNames = NULL;
    if(!(pDevNames = (DEVNAMES *)::GlobalLock(pDlg->hDevNames)))
    {
    TRACE0("GlobalLock (DEVNAMES) failed.\n");
    ::GlobalFree(pDlg->hDevMode);
    ::GlobalFree(pDlg->hDevNames);
    return FALSE;
    }

    //printer names
    LPTSTR pStr = (LPTSTR)pDevNames;
    pStr += pDevNames->wDriverOffset;
    ::lstrcpy(pStr, pApp->GetProfileString(REG_SECTION_PRINTING, REG_ENTRY_DRIVER, pStr));
    pDevNames->wDeviceOffset = pDevNames->wDriverOffset + strlen(pStr) + 1;
    pStr = (LPTSTR)pDevNames;
    pStr += pDevNames->wDeviceOffset;
    ::lstrcpy(pStr, pApp->GetProfileString(REG_SECTION_PRINTING, REG_ENTRY_DEVICE, pStr));
    pDevNames->wOutputOffset = pDevNames->wDeviceOffset + strlen(pStr) + 1;
    pStr = (LPTSTR)pDevNames;
    pStr += pDevNames->wOutputOffset;
    ::lstrcpy(pStr, pApp->GetProfileString(REG_SECTION_PRINTING, REG_ENTRY_OUTPUT, pStr));
    pDevNames->wDefault = GetProfileInt(REG_SECTION_PRINTING, REG_ENTRY_DEFAULT, pDevNames->wDefault);
    ::GlobalUnlock(pDlg->hDevNames);

    if(!pInfo->m_bPreview) //if we're printing
    {
    //commit print dialog box to user manipulation
    pDlg->Flags &= ~PD_RETURNDEFAULT;
    if(pInfo->m_pPD->DoModal() == IDCANCEL)
    {
    ::GlobalFree(pDlg->hDevMode);
    ::GlobalFree(pDlg->hDevNames);
    return FALSE;
    }
    }

    //create device context
    if(!(pDlg->hDC = pInfo->m_pPD->CreatePrinterDC()))
    {
    MessageBox("Unable to create printer device context.\n", "OnPreparePrinting", STOP);
    ::GlobalFree(pDlg->hDevMode);
    ::GlobalFree(pDlg->hDevNames);
    return FALSE;
    }

    return TRUE;
    }




    Respectfully,
    S. Bro




    /* I would be happy to deal with my problems one at the time if they would only line up! */





    Best regards,
    S. Bro

    "I would be happy to deal with my problems one at the time if they would only line up!"
    -Paul Cilwa, "Borland C++ Insider".

    Other useful fora some of which I ruthlessly clipboarded from other peoples footers.

    MSDN: http://search.microsoft.com/us/dev/default.asp
    WIN 32 Assembler: http://board.win32asmcommunity.net/
    RDBMS: http://www.dbforums.com
    Robert's Perl Tutorial: http://www.sthomas.net/roberts-perl-tutorial.htm
    Merriam-Webster Online: http://www.m-w.com/home.htm
    Writing Unmaintainable Code: http://mindprod.com/unmain.html

  4. #4
    Join Date
    Mar 2001
    Location
    Czech Republic
    Posts
    78

    Re: Initialising CPrintDialog with my own DevMode....

    Quote Originally Posted by Feng Yuan View Post
    Keep the PRINTDLG.hDevMode handle(or make a copy of DEVMODE), make sure it's not deleted, set it before calling the print dialog box.
    I am trying to do exactly the same thing but it doesn't work so far. MSDN says that ...the values of hDevMode and hDevNames in PRINTDLG may change when they are passed into PrintDlg. This is because these members are filled on both input and output.

    When I set my stored hDevMode handle in the PRINTDLG structure before calling
    Code:
    CPrintDialog::DoModal()
    , its value is changed during the call. Does it mean that I always have to store the current value of PRINTDLG.hDevMode?
    In other words, I thought the hDevMode (being a HGLOBAL variable) acts like a pointer - I can pass it into the dialog, the data this variable points to is changed, but I can still use the same value of the hDevMode variable. Am I missing something?

  5. #5
    Join Date
    Mar 2001
    Location
    Czech Republic
    Posts
    78

    Resolved Re: Initialising CPrintDialog with my own DevMode....

    Quote Originally Posted by Pavel Kotrc View Post
    Does it mean that I always have to store the current value of PRINTDLG.hDevMode?
    According to Microsoft KB article 193103, the answer is yes, the stored DEVMODE handle has to be updated each time you call
    Code:
    CPrintDialog::DoModal
    because it discards the handle we passed into it and makes its own copy.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured