Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Cannot print landscape with default printer

Cannot print landscape with default printer

Scheduled Pinned Locked Moved C / C++ / MFC
comquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tataxin
    wrote on last edited by
    #1

    I try to print a window, using this article: http://support.microsoft.com/kb/186736 It works, but I want to user default printer with landscape. So I try to modify the GetPrinterDC(), like this:

    HDC CLogACTsPrintHelper::GetPrinterDC(void)
    {
    CWaitCursor wait;
    PRINTDLG pdlg;
    memset( &pdlg, 0, sizeof(PRINTDLG));
    pdlg.lStructSize = sizeof(PRINTDLG);

    // use default printer
    pdlg.Flags = PD_RETURNDEFAULT | PD_RETURNDC;

    // set landscape
    AfxGetApp()->GetPrinterDeviceDefaults(&pdlg);
    DEVMODE* lpDevMode = (DEVMODE*)::GlobalLock(pdlg.hDevMode);
    lpDevMode->dmOrientation = (short)DMORIENT_LANDSCAPE;
    ::ResetDC(pdlg.hDC,lpDevMode);
    ::GlobalUnlock(pdlg.hDevMode);
    // end set landscape

    PrintDlg( &pdlg );      
    
    
    return pdlg.hDC;
    

    }

    But I don't know why it return NULL??? If I comment all source code in set landscape block, it works. Or if I do not use default printer and set landscape, it work

    // do not use default printer
    pdlg.Flags = PD_RETURNDC;
    // set landscape
    ...

    Can anyone explain me what's wrong here? Thank you in advance, :)

    T N 2 Replies Last reply
    0
    • T tataxin

      I try to print a window, using this article: http://support.microsoft.com/kb/186736 It works, but I want to user default printer with landscape. So I try to modify the GetPrinterDC(), like this:

      HDC CLogACTsPrintHelper::GetPrinterDC(void)
      {
      CWaitCursor wait;
      PRINTDLG pdlg;
      memset( &pdlg, 0, sizeof(PRINTDLG));
      pdlg.lStructSize = sizeof(PRINTDLG);

      // use default printer
      pdlg.Flags = PD_RETURNDEFAULT | PD_RETURNDC;

      // set landscape
      AfxGetApp()->GetPrinterDeviceDefaults(&pdlg);
      DEVMODE* lpDevMode = (DEVMODE*)::GlobalLock(pdlg.hDevMode);
      lpDevMode->dmOrientation = (short)DMORIENT_LANDSCAPE;
      ::ResetDC(pdlg.hDC,lpDevMode);
      ::GlobalUnlock(pdlg.hDevMode);
      // end set landscape

      PrintDlg( &pdlg );      
      
      
      return pdlg.hDC;
      

      }

      But I don't know why it return NULL??? If I comment all source code in set landscape block, it works. Or if I do not use default printer and set landscape, it work

      // do not use default printer
      pdlg.Flags = PD_RETURNDC;
      // set landscape
      ...

      Can anyone explain me what's wrong here? Thank you in advance, :)

      T Offline
      T Offline
      tataxin
      wrote on last edited by
      #2

      help me, pleasee!!!

      1 Reply Last reply
      0
      • T tataxin

        I try to print a window, using this article: http://support.microsoft.com/kb/186736 It works, but I want to user default printer with landscape. So I try to modify the GetPrinterDC(), like this:

        HDC CLogACTsPrintHelper::GetPrinterDC(void)
        {
        CWaitCursor wait;
        PRINTDLG pdlg;
        memset( &pdlg, 0, sizeof(PRINTDLG));
        pdlg.lStructSize = sizeof(PRINTDLG);

        // use default printer
        pdlg.Flags = PD_RETURNDEFAULT | PD_RETURNDC;

        // set landscape
        AfxGetApp()->GetPrinterDeviceDefaults(&pdlg);
        DEVMODE* lpDevMode = (DEVMODE*)::GlobalLock(pdlg.hDevMode);
        lpDevMode->dmOrientation = (short)DMORIENT_LANDSCAPE;
        ::ResetDC(pdlg.hDC,lpDevMode);
        ::GlobalUnlock(pdlg.hDevMode);
        // end set landscape

        PrintDlg( &pdlg );      
        
        
        return pdlg.hDC;
        

        }

        But I don't know why it return NULL??? If I comment all source code in set landscape block, it works. Or if I do not use default printer and set landscape, it work

        // do not use default printer
        pdlg.Flags = PD_RETURNDC;
        // set landscape
        ...

        Can anyone explain me what's wrong here? Thank you in advance, :)

        N Offline
        N Offline
        Nelek
        wrote on last edited by
        #3

        Well, I don't know if you are using MFC, but I made it like that:

        BOOL CMyFormView::OnPreparePrinting(CPrintInfo* pInfo)
        { // Standardvorbereitung
        delete pInfo->m_pPD ; // Release previous MFC allocated standard dialog object
        //Miguel: Attach the new CDlgPrinting
        pInfo->m_pPD = new CDlgPrinting(FALSE, PD_ALLPAGES | PD_USEDEVMODECOPIES | PD_NOPAGENUMS
        | PD_HIDEPRINTTOFILE | PD_NOSELECTION, this) ; //My own CPrintDialog self defined
        pInfo->m_pPD->m_pd.hInstance = AfxGetInstanceHandle();
        pInfo->m_pPD->m_pd.lpPrintTemplateName = MAKEINTRESOURCE(IDD_PRINT_PRDLG);
        pInfo->m_pPD->m_pd.Flags |= PD_ENABLEPRINTTEMPLATE;
        pInfo->m_pPD->m_pd.nMinPage = 1;
        .
        // Other checks and configurations
        .
        if (pInfo->m_bPreview)
        return DoPreparePrinting(pInfo);

        int nAnswer = pInfo->m\_pPD->DoModal ();
        if (nAnswer == IDCANCEL)
        	return FALSE;
        
        pInfo->m\_pPD->m\_pd.hDC = pInfo->m\_pPD->CreatePrinterDC ();
        
        return TRUE;
        

        }

        //*********************************

        void CMyFormView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
        { // TODO: Speziellen Code hier einfügen und/oder Basisklasse aufrufen
        if (pDC->IsPrinting ())
        { DEVMODE* devMode = pInfo->m_pPD->GetDevMode ();
        devMode->dmOrientation = DMORIENT_LANDSCAPE;
        devMode->dmFields |= DM_ORIENTATION;
        pDC->ResetDC (devMode);
        }

        CFormView::OnPrepareDC(pDC, pInfo);
        

        }

        //*********************************

        void CMyFormView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
        { // TODO: Speziellen Code hier einfügen und/oder Basisklasse aufrufen
        // Setting printing parameters
        pDC->SetMapMode (MM_LOMETRIC);

        double dLeftOffset = 200 - (pDC->GetDeviceCaps (PHYSICALOFFSETX) \* 254.0) / pDC->GetDeviceCaps (LOGPIXELSX);
        double dTopOffset = 200 - (pDC->GetDeviceCaps (PHYSICALOFFSETY) \* 254.0) / pDC->GetDeviceCaps (LOGPIXELSY);
        double dRightMargin = 2770 - (pDC->GetDeviceCaps (PHYSICALOFFSETX) \* 254.0) / pDC->GetDeviceCaps (LOGPIXELSX);
        double dBottomMargin = 1950 - (pDC->GetDeviceCaps (PHYSICALOFFSETY) \* 254.0) / pDC->GetDeviceCaps (LOGPIXELSY);
        
        pInfo->m\_rectDraw.left += (int) dLeftOffset;
        pInfo->m\_rectDraw.top += (int) dTopOffset;
        pInfo->m\_rectDraw.right = (int) dRightMargin;
        pInfo->m\_rectDraw.bottom = (int) dBottomMargin;
        

        ..........
        .......
        ,....

        I don't know if it is totally correct and I guess it can be optimized, but it works. I hope it helps you

        Regards. --------

        T 1 Reply Last reply
        0
        • N Nelek

          Well, I don't know if you are using MFC, but I made it like that:

          BOOL CMyFormView::OnPreparePrinting(CPrintInfo* pInfo)
          { // Standardvorbereitung
          delete pInfo->m_pPD ; // Release previous MFC allocated standard dialog object
          //Miguel: Attach the new CDlgPrinting
          pInfo->m_pPD = new CDlgPrinting(FALSE, PD_ALLPAGES | PD_USEDEVMODECOPIES | PD_NOPAGENUMS
          | PD_HIDEPRINTTOFILE | PD_NOSELECTION, this) ; //My own CPrintDialog self defined
          pInfo->m_pPD->m_pd.hInstance = AfxGetInstanceHandle();
          pInfo->m_pPD->m_pd.lpPrintTemplateName = MAKEINTRESOURCE(IDD_PRINT_PRDLG);
          pInfo->m_pPD->m_pd.Flags |= PD_ENABLEPRINTTEMPLATE;
          pInfo->m_pPD->m_pd.nMinPage = 1;
          .
          // Other checks and configurations
          .
          if (pInfo->m_bPreview)
          return DoPreparePrinting(pInfo);

          int nAnswer = pInfo->m\_pPD->DoModal ();
          if (nAnswer == IDCANCEL)
          	return FALSE;
          
          pInfo->m\_pPD->m\_pd.hDC = pInfo->m\_pPD->CreatePrinterDC ();
          
          return TRUE;
          

          }

          //*********************************

          void CMyFormView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
          { // TODO: Speziellen Code hier einfügen und/oder Basisklasse aufrufen
          if (pDC->IsPrinting ())
          { DEVMODE* devMode = pInfo->m_pPD->GetDevMode ();
          devMode->dmOrientation = DMORIENT_LANDSCAPE;
          devMode->dmFields |= DM_ORIENTATION;
          pDC->ResetDC (devMode);
          }

          CFormView::OnPrepareDC(pDC, pInfo);
          

          }

          //*********************************

          void CMyFormView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
          { // TODO: Speziellen Code hier einfügen und/oder Basisklasse aufrufen
          // Setting printing parameters
          pDC->SetMapMode (MM_LOMETRIC);

          double dLeftOffset = 200 - (pDC->GetDeviceCaps (PHYSICALOFFSETX) \* 254.0) / pDC->GetDeviceCaps (LOGPIXELSX);
          double dTopOffset = 200 - (pDC->GetDeviceCaps (PHYSICALOFFSETY) \* 254.0) / pDC->GetDeviceCaps (LOGPIXELSY);
          double dRightMargin = 2770 - (pDC->GetDeviceCaps (PHYSICALOFFSETX) \* 254.0) / pDC->GetDeviceCaps (LOGPIXELSX);
          double dBottomMargin = 1950 - (pDC->GetDeviceCaps (PHYSICALOFFSETY) \* 254.0) / pDC->GetDeviceCaps (LOGPIXELSY);
          
          pInfo->m\_rectDraw.left += (int) dLeftOffset;
          pInfo->m\_rectDraw.top += (int) dTopOffset;
          pInfo->m\_rectDraw.right = (int) dRightMargin;
          pInfo->m\_rectDraw.bottom = (int) dBottomMargin;
          

          ..........
          .......
          ,....

          I don't know if it is totally correct and I guess it can be optimized, but it works. I hope it helps you

          Regards. --------

          T Offline
          T Offline
          tataxin
          wrote on last edited by
          #4

          Thank you, Nelek. I will try it now!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups