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. CPrintDialog default printer.

CPrintDialog default printer.

Scheduled Pinned Locked Moved C / C++ / MFC
c++visual-studiographicsdevopsquestion
6 Posts 3 Posters 4 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.
  • M Offline
    M Offline
    Maximilien
    wrote on last edited by
    #1

    I don't have a physical printer, so I cannot test (Am I wrong here?) I'm trying to print something (a bitmap) using MFC. I set my default printer to "Microsoft Print To PDF" in Windows 10 settings. I have a skeleton code like this : The GetDefaults() fails. Am I missing something ? Obviously, other print commands in other software work (VS, Word, Chrome... )

    {
    CPrintDialog dlg(FALSE);

    if (!dlg.GetDefaults())
    {
    	AfxMessageBox(\_T("You have no default printer!"));
    }
    else
    {
    	// attach to the DC we were given
    	CDC dc;
    	dc.Attach(dlg.m\_pd.hDC);
    	//....
    }
    

    }

    CI/CD = Continuous Impediment/Continuous Despair

    V 1 Reply Last reply
    0
    • M Maximilien

      I don't have a physical printer, so I cannot test (Am I wrong here?) I'm trying to print something (a bitmap) using MFC. I set my default printer to "Microsoft Print To PDF" in Windows 10 settings. I have a skeleton code like this : The GetDefaults() fails. Am I missing something ? Obviously, other print commands in other software work (VS, Word, Chrome... )

      {
      CPrintDialog dlg(FALSE);

      if (!dlg.GetDefaults())
      {
      	AfxMessageBox(\_T("You have no default printer!"));
      }
      else
      {
      	// attach to the DC we were given
      	CDC dc;
      	dc.Attach(dlg.m\_pd.hDC);
      	//....
      }
      

      }

      CI/CD = Continuous Impediment/Continuous Despair

      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #2

      You don't need the CPrintDialog if you already know the printer name. Just use something like

      HANDLE printerHandle = nullptr;
      TCHAR name[] = _T("Microsoft Print To PDF");
      if (!::OpenPrinter(name, &printerHandle, nullptr))
      {
      // handle the error
      ...
      }

      M 1 Reply Last reply
      0
      • V Victor Nijegorodov

        You don't need the CPrintDialog if you already know the printer name. Just use something like

        HANDLE printerHandle = nullptr;
        TCHAR name[] = _T("Microsoft Print To PDF");
        if (!::OpenPrinter(name, &printerHandle, nullptr))
        {
        // handle the error
        ...
        }

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #3

        the problem is not the device name, but GetDefaults not returning the default printer. Obviously, it should work for any device.

        CI/CD = Continuous Impediment/Continuous Despair

        L 1 Reply Last reply
        0
        • M Maximilien

          the problem is not the device name, but GetDefaults not returning the default printer. Obviously, it should work for any device.

          CI/CD = Continuous Impediment/Continuous Despair

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Max, The default constructor is in the documentation[^]:

          CPrintDialog(
          BOOL bPrintSetupOnly,
          DWORD dwFlags = PD_ALLPAGES | PD_USEDEVMODECOPIES | PD_NOPAGENUMS | PD_HIDEPRINTTOFILE | PD_NOSELECTION,
          CWnd* pParentWnd = NULL);

          The default constructor does not contain PD_RETURNDEFAULT. You will need to use the PD_RETURNDEFAULT flag[^] to get the CPrintDialog class to simply return the default printer. In fact by using this flag the DoModal(); function won't even open a window.

          CPrintDialog dlg(FALSE,PD\_RETURNDEFAULT,NULL);
          dlg.DoModal();
          LPDEVMODE lpdev = dlg.GetDevMode();
          MessageBox(lpdev->dmDeviceName,0,0);
          

          Windows 10 makes decisions for the novice users. So Windows 10 will sometimes decide which printer to use and override the user choice. To disable this programmatically you could set the HKCU\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows\LegacyDefaultPrinterMode to TRUE. Best Wishes, -David Delaune

          M 1 Reply Last reply
          0
          • L Lost User

            Max, The default constructor is in the documentation[^]:

            CPrintDialog(
            BOOL bPrintSetupOnly,
            DWORD dwFlags = PD_ALLPAGES | PD_USEDEVMODECOPIES | PD_NOPAGENUMS | PD_HIDEPRINTTOFILE | PD_NOSELECTION,
            CWnd* pParentWnd = NULL);

            The default constructor does not contain PD_RETURNDEFAULT. You will need to use the PD_RETURNDEFAULT flag[^] to get the CPrintDialog class to simply return the default printer. In fact by using this flag the DoModal(); function won't even open a window.

            CPrintDialog dlg(FALSE,PD\_RETURNDEFAULT,NULL);
            dlg.DoModal();
            LPDEVMODE lpdev = dlg.GetDevMode();
            MessageBox(lpdev->dmDeviceName,0,0);
            

            Windows 10 makes decisions for the novice users. So Windows 10 will sometimes decide which printer to use and override the user choice. To disable this programmatically you could set the HKCU\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows\LegacyDefaultPrinterMode to TRUE. Best Wishes, -David Delaune

            M Offline
            M Offline
            Maximilien
            wrote on last edited by
            #5

            lol, thanks. I should really pickup this technical skill of RTFM.

            CI/CD = Continuous Impediment/Continuous Despair

            L 1 Reply Last reply
            0
            • M Maximilien

              lol, thanks. I should really pickup this technical skill of RTFM.

              CI/CD = Continuous Impediment/Continuous Despair

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              You are welcome.

              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