How to set paper size for the default printer through a program?

This is a discussion on How to set paper size for the default printer through a program? within the Windows Programming forums, part of the Platform Specific Boards category; Can anyone tell me how to set the paper size and other printer properties without bringing up the printer setup ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    3

    Post How to set paper size for the default printer through a program?

    Can anyone tell me how to set the paper size and other printer properties without bringing up the printer setup dialog box? I tried AddForm and SetForm API function. However, the currently selected printer form doesn't seem to change.

    Thanks

    Sugunan

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Try,
    PageSetUp->lStructSize=sizeof(PAGESETUPDLG) ;
    PageSetUp->hwndOwner= hWnd ;
    PageSetUp->hDevMode = NULL ;
    PageSetUp->hDevNames= NULL;
    PageSetUp->Flags=PSD_RETURNDEFAULT;
    //stack the structure with data for the default printer
    PageSetupDlg(PageSetUp);

    This should call the Print Dlg without showing it, returning the default printers settings. The DevMode struct will contain the paper size.

    Also you can use the EnumPrinters() function, with the PRINTER_ENUM_DEFAULT flag. You will need to know what OS is running, to tell what PRINT_INFO struct to use, and watch for truncated printer names under NT/2000.
    Then create a DC and use GetDeviceCaps() on the printer DC.

    iWidth =GetDeviceCaps(hdcTemp,HORZRES);
    iHeight =GetDeviceCaps(hdcTemp,VERTRES);


    I find, under NT/2000
    GetProfileString("windows", "device", (LPCTSTR) &szBuffer,(LPTSTR) &szOutputString, 255);

    will get the printer name without the truncation. (If the printer has a long driver name it won't fit in the string, the last characters are lost and so you can't getDC with it)

    Isn't this easy!

    /////////
    I should read these questions more carefully. This is how to GET the paper size.
    Last edited by novacain; 08-30-2001 at 10:46 PM.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    3

    Thanks for the fast response

    Dear Novacain,

    thank you for the fast response and the tip to execute the Print Setup Dialog without actually showing it. May be there is a way on similar lines to set the property also. I shall read the MSDN documantation in detail.


    regards

    Sugunan

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    A way round the problem may be to save the data, DEVNAME and DEVMODE from the PAGESETUP struct, to disk and load them up when you need a new print.

    All you need are the settings to save in the first place.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    3

    Just one more question

    Thanks for the tip.

    Is it necessary to free the HGLOBAL returned in the hDevMode parameter? MSDN doesn't say anything about this.


    thanks

    Sugunan

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    As far as I know you do not have to free the HGLOBAL returned DEVMODE or DEVNAME structure.

    You may have to use GlobalLock on them to 'type cast' them to the structures to use the data returned in them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issues and questions about queues
    By Nazgulled in forum C Programming
    Replies: 36
    Last Post: 12-13-2007, 02:03 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  3. Trouble with DMA Segmentation Faults
    By firestorm717 in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 10:20 PM
  4. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 11:14 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 10:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21