winapiprintingmfc

MFC: How do you get the page orientation in OnPreparePrinting() prior to calling base class?


I need to determine if the print will be Landscape or Portrait in the CViews' OnPreparePrinting() function before calling the base class.

I tried a couple ways, one works except the first call, the other not at all:

// Method that always says it's Portrait even after changing "Page Setup" to Landscape.   

BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo)
{
  bool landscape=false;
        
  {
    CPrintDialog pd(TRUE);
    if (pd.GetDefaults()) {
        DEVMODE* pdevmode=pd.GetDevMode();
        if (pdevmode) {
            landscape=(pdevmode->dmOrientation==DMORIENT_LANDSCAPE);
        }
    }
  }

   // ...
}

// Method that fails theApp.CreatePrinterDC(dc) the first time it's called, but works correctly after that.

BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo)
{
    bool landscape=false;
    
    {
        CDC dc;
        if (theApp.CreatePrinterDC(dc)) {
            pInfo->m_rectDraw.SetRect(0, 0, dc.GetDeviceCaps(HORZRES), dc.GetDeviceCaps(VERTRES));
            if (pInfo->m_rectDraw.Width()>pInfo->m_rectDraw.Height()) {
                landscape=true;
            }
            dc.DeleteDC();
        }
    }
    // ....
} 

What is the correct way to determine Landscape or Portrait prior to calling the base class (or showing any Dialogs) in OnPreparePrinting() ?

TIA!!


Solution

  • The pd.GetDefaults() call is not the correct function to use. The following works:

    BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo)
    {
        bool landscape=false;
     
        {
            CPrintDialog pd(TRUE);
            if (theApp.GetPrinterDeviceDefaults(&pd.m_pd)) {
                DEVMODE* pdevmode=pd.GetDevMode();
                if (pdevmode) {
                    landscape=(pdevmode->dmOrientation==DMORIENT_LANDSCAPE);
                }
            }
        }
        // ....
    }
    

    Reference: CWinApp:GetPrinterDeviceDefaults