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. Coordinating between Print Setup dialog and Print dialog

Coordinating between Print Setup dialog and Print dialog

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionworkspace
6 Posts 3 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
    theCPkid
    wrote on last edited by
    #1

    Is there any way to set the paper size, say A4, and orientation in OnPreparePrinting() function which is called before the print dialog is displayed? The problem is that I am not able to use CWinApp::OnFilePrintSetup function since the code is inside a dll and there's no class derived from CWinApp. So, in my own code for OnFilePrintSetup, I display a page setup dialog box and takes various inputs from user. Now, I want these changes to appear in print dialog also(which I get by using CVIew::OnFilePrint) and not the default settings that are displayed by print dialog box. So, the only way that I could find is to make changes to pInfo member in OnPreparePrinting but I could not get any way to set paper size. m_pPD member does not provide any way to set paper size? Please help. Thanks.

    L 1 Reply Last reply
    0
    • T theCPkid

      Is there any way to set the paper size, say A4, and orientation in OnPreparePrinting() function which is called before the print dialog is displayed? The problem is that I am not able to use CWinApp::OnFilePrintSetup function since the code is inside a dll and there's no class derived from CWinApp. So, in my own code for OnFilePrintSetup, I display a page setup dialog box and takes various inputs from user. Now, I want these changes to appear in print dialog also(which I get by using CVIew::OnFilePrint) and not the default settings that are displayed by print dialog box. So, the only way that I could find is to make changes to pInfo member in OnPreparePrinting but I could not get any way to set paper size. m_pPD member does not provide any way to set paper size? Please help. Thanks.

      L Offline
      L Offline
      leonigah
      wrote on last edited by
      #2

      DEVMODE *pDevMode=NULL; CPrintDialog dlg(TRUE, PD_ALLPAGES | PD_USEDEVMODECOPIES | PD_NOPAGENUMS | PD_HIDEPRINTTOFILE | PD_NOSELECTION | PD_RETURNDC, this); if(dlg.DoModal()== IDOK) { pDevMode = dlg.GetDevMode(); pDevMode->dmPaperWidth = 8.27; pDevMode->dmPanningHeight = 11.69; }

      Nigah M Manzoor

      T 1 Reply Last reply
      0
      • L leonigah

        DEVMODE *pDevMode=NULL; CPrintDialog dlg(TRUE, PD_ALLPAGES | PD_USEDEVMODECOPIES | PD_NOPAGENUMS | PD_HIDEPRINTTOFILE | PD_NOSELECTION | PD_RETURNDC, this); if(dlg.DoModal()== IDOK) { pDevMode = dlg.GetDevMode(); pDevMode->dmPaperWidth = 8.27; pDevMode->dmPanningHeight = 11.69; }

        Nigah M Manzoor

        T Offline
        T Offline
        theCPkid
        wrote on last edited by
        #3

        My problem is because of the fact that I am using my own dialog box for print setup but using the mfc provided dialog box for print. When I use my own dialog box, I dont update the internal m_hDevMode structure which has the information for all settings like page size, orientation, dpi etc etc. I did not update because I did not know how to update and precisely speaking, i did not know about GetPrinterDeviceDefaults API. This is what I needed to do to set the default settings before the print dialog is displayed. No matter what you do, you will always get a print dialog box with a4 & landscape set. It's sufficient for my purpose. [code] OnPreparePrinting(CPrintInfo* pInfo) { if(GetPrinterDeviceDefaults(&pInfo->m_pPD->m_pd)) { LPDEVMODE dev = pInfo->m_pPD->GetDevMode(); GlobalUnlock(dev); dev->dmOrientation = DMORIENT_LANDSCAPE; dev->dmPaperSize = DMPAPER_A4; } return CView::DoPreparePrinting(pInfo); } [/code] Thanks!

        N 1 Reply Last reply
        0
        • T theCPkid

          My problem is because of the fact that I am using my own dialog box for print setup but using the mfc provided dialog box for print. When I use my own dialog box, I dont update the internal m_hDevMode structure which has the information for all settings like page size, orientation, dpi etc etc. I did not update because I did not know how to update and precisely speaking, i did not know about GetPrinterDeviceDefaults API. This is what I needed to do to set the default settings before the print dialog is displayed. No matter what you do, you will always get a print dialog box with a4 & landscape set. It's sufficient for my purpose. [code] OnPreparePrinting(CPrintInfo* pInfo) { if(GetPrinterDeviceDefaults(&pInfo->m_pPD->m_pd)) { LPDEVMODE dev = pInfo->m_pPD->GetDevMode(); GlobalUnlock(dev); dev->dmOrientation = DMORIENT_LANDSCAPE; dev->dmPaperSize = DMPAPER_A4; } return CView::DoPreparePrinting(pInfo); } [/code] Thanks!

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

          I made it as follows:

          BOOL CMyTabView::OnPreparePrinting(CPrintInfo* pInfo)
          { // Standardvorbereitung
          delete pInfo->m_pPD ; // Release previous MFC allocated dialog object
          // Attach the new CDlgPrinting (Usesrdefined)
          pInfo->m_pPD = new CDlgPrinting(FALSE, PD_ALLPAGES | PD_USEDEVMODECOPIES | PD_NOPAGENUMS
          | PD_HIDEPRINTTOFILE | PD_NOSELECTION, this) ;
          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;

          int nPages = 1 + (m\_nTotalRules / 30);
          pInfo->SetMaxPage (nPages);
          pInfo->m\_pPD->m\_pd.nFromPage = (WORD) pInfo->GetMinPage ();
          pInfo->m\_pPD->m\_pd.nToPage = (WORD) pInfo->GetMaxPage ();
          
          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 CMyTabView::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; //<- Important
          pDC->ResetDC (devMode);
          }

          CFormView::OnPrepareDC(pDC, pInfo);
          

          }

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

          void CMyTabView::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;
          

          // Sending the data to be printed
          //...
          }

          I hope it helps.

          T 1 Reply Last reply
          0
          • N Nelek

            I made it as follows:

            BOOL CMyTabView::OnPreparePrinting(CPrintInfo* pInfo)
            { // Standardvorbereitung
            delete pInfo->m_pPD ; // Release previous MFC allocated dialog object
            // Attach the new CDlgPrinting (Usesrdefined)
            pInfo->m_pPD = new CDlgPrinting(FALSE, PD_ALLPAGES | PD_USEDEVMODECOPIES | PD_NOPAGENUMS
            | PD_HIDEPRINTTOFILE | PD_NOSELECTION, this) ;
            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;

            int nPages = 1 + (m\_nTotalRules / 30);
            pInfo->SetMaxPage (nPages);
            pInfo->m\_pPD->m\_pd.nFromPage = (WORD) pInfo->GetMinPage ();
            pInfo->m\_pPD->m\_pd.nToPage = (WORD) pInfo->GetMaxPage ();
            
            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 CMyTabView::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; //<- Important
            pDC->ResetDC (devMode);
            }

            CFormView::OnPrepareDC(pDC, pInfo);
            

            }

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

            void CMyTabView::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;
            

            // Sending the data to be printed
            //...
            }

            I hope it helps.

            T Offline
            T Offline
            theCPkid
            wrote on last edited by
            #5

            thanks for the code. I can see that you have modified the standard DoPreparePrinting code. But I am using the standard DoPreparePrinting code as it provides better error checking like what if CreatePrinterDC fails? your funciton will still return TRUE but not so in case of standard code. also, I have not tried your code inside OnPreparePrinting but if you deleting the print dialog at beginning, then wont the settings entered by user in print setup box will be lost? Say, you select A4 size in print setup box and when you go to print->properties, will it show A4 size selected or default size, probably letter selected? And I am using the MM_TEXT mode throughout. Just adjusting the font size etc accordingly whether we are in print preview mode or displaying on monitor or sending to printer. One weird thing I noticed is that if you are in print preview mode, then ::GetDeviceCaps (pDC->GetSafeHDC(), LOGPIXELSY) returns a different value from pDC->GetDeviceCaps(LOGPIXELSY) and the value returned by latter is correct one. And dmPaperLength and dmPaperWidth were always zero although dmPaperSize was changing properly. I initially thought of storing length, width in a table but that would have been very very and very tedious. I found a better solution of creating a printer dc using dc.Attach(pd.GetPrinterDC()); and then you can obtain paper length and width from dc. I have not still tried multiple pages, so cant say if there can be problem with that.

            the fruits of your success will be in direct ratio to the honesty and sincerity of your own efforts in keeping your own records, doing your own thinking and, reaching your own conclusions. ..surviving in autumn..in love with spring..

            N 1 Reply Last reply
            0
            • T theCPkid

              thanks for the code. I can see that you have modified the standard DoPreparePrinting code. But I am using the standard DoPreparePrinting code as it provides better error checking like what if CreatePrinterDC fails? your funciton will still return TRUE but not so in case of standard code. also, I have not tried your code inside OnPreparePrinting but if you deleting the print dialog at beginning, then wont the settings entered by user in print setup box will be lost? Say, you select A4 size in print setup box and when you go to print->properties, will it show A4 size selected or default size, probably letter selected? And I am using the MM_TEXT mode throughout. Just adjusting the font size etc accordingly whether we are in print preview mode or displaying on monitor or sending to printer. One weird thing I noticed is that if you are in print preview mode, then ::GetDeviceCaps (pDC->GetSafeHDC(), LOGPIXELSY) returns a different value from pDC->GetDeviceCaps(LOGPIXELSY) and the value returned by latter is correct one. And dmPaperLength and dmPaperWidth were always zero although dmPaperSize was changing properly. I initially thought of storing length, width in a table but that would have been very very and very tedious. I found a better solution of creating a printer dc using dc.Attach(pd.GetPrinterDC()); and then you can obtain paper length and width from dc. I have not still tried multiple pages, so cant say if there can be problem with that.

              the fruits of your success will be in direct ratio to the honesty and sincerity of your own efforts in keeping your own records, doing your own thinking and, reaching your own conclusions. ..surviving in autumn..in love with spring..

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

              First of all, thank you for your comments. They have make me notice some things I can improve in my code :)

              theCPkid wrote:

              the standard DoPreparePrinting code as it provides better error checking like what if CreatePrinterDC fails? your funciton will still return TRUE but not so in case of standard code.

              Yes, you are right, but you can add that error checking all the security you want/need before returning TRUE. (I will change it)

              theCPkid wrote:

              but if you deleting the print dialog at beginning, then wont the settings entered by user in print setup box will be lost?

              No. You are right, I am deleting the standard CPrintDialog, but changing it with my CDlgPrinting that is derivation of it (I actually made a copy of the standard and modified it to fit my needs), so it has the support for print-setup as well.

              theCPkid wrote:

              Say, you select A4 size in print setup box and when you go to print->properties, will it show A4 size selected or default size, probably letter selected?

              The trick is that I have no "print setup" in the main frame menu, it has to be made through my dialog if you don't want to print with the standards (A4, portrait, black and white). I made a test in a plotter and it was successfull with A4 and A3, I don't allow smaller pages because of my application's workspace. I print graphics, tables and other things, needed info wouldn't fit otherwise or, I have to zoom it out it would be too small to be clearly understood. But I restrict some setup because of depending on where is being printed it "must" be printed with some specifical configuration, so I give it "manually" in the code of the view.

              theCPkid wrote:

              Just adjusting the font size etc accordingly whether we are in print preview mode or displaying on monitor or sending to printer.

              Why? I am using the same font for both and it works good. I create them with:

              CFont fontLabels, fontInfo;
              fontLabels.CreatePointFont (80, "MS Sans Serif", pDC);
              fontInfo.CreatePointFont (140, "MS Sans Serif", pDC);

              theCPkid wrote:

              And dmPaperLength and dmPaperWidth were always zero although dmPaperSize was changing properly. I initially thought of storing length, width in a table but that would have been very very and very tedious.

              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