title: MFC - Printing - Changing page orientation from a custom pagesetup dialog
body: <p>I am developing a custom print dialog and page setup using MFC and VS2008 for my Win32 program. Since the code is legacy, I can't take much advantage from MFC view/doc architecture. As a result, I wrote a printing code completely from scratch. </p>
  
  <p>I setup CPrintInfo, instantiate my custom print dialog box and hook this dialog box to the CPrintInfo I just created. When my custom print dialog is up, I have a radio button to let a user toggles the page orientation. For some reasons, I couldn't modify the current DEVMODE at the run-time. As a result, every page I print will end up as a portrait.</p>
  
  <p>Even if I manually set pDevMode->dmOrientation to DMORIENT_LANDSCAPE from the event handler of the custom print dialog, the printing result is still ended up as portrait. I am really not sure why this is happening and how to modify the DevMode after the print dialog is up.</p>
  
  <p>Thank you in advance for any help. </p>
  
  <p><strong>Here is the code I have:</strong></p>
  
  <pre><code>void PrintSomething(CWnd* currentWnd) {
    // Create CPrintInfo
    CPrintInfo* pPrintInfo = new CPrintInfo;
    SetupPrintInfo(pPrintInfo); // simply setup some member variables of CPrintInfo
  
    // Create a custom print dialog 
    CustomPrintDlg* pCustomPrtDlg = new CustomPrintDlg(FALSE, PD_ALLPAGES | PD_USEDEVMODECOPIES | PD_NOPAGENUMS
             | PD_HIDEPRINTTOFILE | PD_NOSELECTION, pPrintInfo, currentWnd);
    SetupPrintDialog(pPrintInfo,pCustomPrtDlg);
  
    if ( AfxGetApp()-&gt;DoPrintDialog(pCustomPrtDlg) == IDOK ) {
          ... // proceed a print loop 
    }
  }
  </code></pre>
  
  <p><strong>Code for setting up the custom print dialog:</strong></p>
  
  <pre><code>void SetupPrintDialog(CPrintInfo* pPrintInfo,CustomPrintDlg* pCustomPrtDlg) {
    delete pInfo-&gt;m_pPD;
    pInfo-&gt;m_pPD = pCustomPrtDlg;
  
    pInfo-&gt;m_pPD-&gt;m_pd.hInstance = AfxGetInstanceHandle();
    pInfo-&gt;m_pPD-&gt;m_pd.lpPrintTemplateName = MAKEINTRESOURCE(IDD_CUSTOM_PRTDLG);
  
    // Set the Flags of the PRINTDLG structure as shown, else the
    // changes will have no effect.
    pInfo&gt;m_pPD-&gt;m_pd.Flags |= PD_ENABLEPRINTTEMPLATE;
  
    // Set the page range.
    pInfo&gt;m_pPD-&gt;m_pd.nMinPage = 1;         // one based page numbers.
    pInfo&gt;m_pPD-&gt;m_pd.nMaxPage = 0xffff; // how many pages is unknown. 
  }
  </code></pre>
  
  <p><strong>When a user toggles the radio button to Landscape, this function will be invoked:</strong></p>
  
  <pre><code>void CustomPrintDlg::OnLandscapeChecked() {
    // set the current Devmode to landscape
    LPDEVMODE pDevMode = GetDevMode();
    GlobalUnlock(pDevMode);
    pDevMode-&gt;dmOrientation = DMORIENT_LANDSCAPE;
  }
  </code></pre>
  
  <p><strong>A pseucode for the custom print dialog class:</strong></p>
  
  <pre><code>class CustomPrintDlg: public CPrintDialog {
  
    ...   // just override some methods from CPrintDialog
  };
  </code></pre>
  
  <p>Thanks again,
  Unagi</p>