changing property (layout) of a printer

This is a discussion on changing property (layout) of a printer within the Windows Programming forums, part of the Platform Specific Boards category; I am looking for a method to change the Page Layout (Landscape or Portrait) property of a printer programmatically. I ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    39

    changing property (layout) of a printer

    I am looking for a method to change the Page Layout (Landscape or Portrait) property of a printer programmatically.

    I got this piece of code from somewhere which seems to run as expected. It sets the DEVMODE structure and exits without error. But when I check the property of the printer, it hasn't changed could somebody please verify if this code runs properly on their machines.
    If you notice any flaws or errors do let me know

    Code:
    #include "stdafx.h"
    #include <windows.h>
    
    LPDEVMODE GetLandscapeDevMode(HWND hWnd, char *pDevice)
    {
    	HANDLE      hPrinter;
    	LPDEVMODE   pDevMode;
    	DWORD       dwNeeded, dwRet;
    
    	/* Start by opening the printer */
    	if (!OpenPrinter(pDevice, &hPrinter, NULL))
    	   return NULL;
    	
    	/*
    	* Step 1:
    	* Allocate a buffer of the correct size.
    	*/
    	dwNeeded = DocumentProperties(hWnd,
    	   hPrinter,       /* Handle to our printer. */
    	   pDevice,        /* Name of the printer. */
    	   NULL,           /* Asking for size, so */
    	   NULL,           /* these are not used. */
    	   0);             /* Zero returns buffer size. */
    	pDevMode = (LPDEVMODE)malloc(dwNeeded);
    
    	/*
    	* Step 2:
    	* Get the default DevMode for the printer and
    	* modify it for your needs.
    	*/
    	dwRet = DocumentProperties(hWnd,
    	   hPrinter,
    	   pDevice,
    	   pDevMode,       /* The address of the buffer to fill. */
    	   NULL,           /* Not using the input buffer. */
    	   DM_OUT_BUFFER); /* Have the output buffer filled. */
    	if (dwRet != IDOK)
    	{
    	   /* If failure, cleanup and return failure. */
    	   free(pDevMode);
    	   ClosePrinter(hPrinter);
    	   return NULL;
    	}
    
    	/*
    	*	Make changes to the DevMode which are supported.
    	*/
    	if (pDevMode->dmFields & DM_ORIENTATION)
    	{
    	   /* If the printer supports paper orientation, set it.*/
    	   pDevMode->dmOrientation = DMORIENT_LANDSCAPE;
    	}
    	/*
    	* Step 3:
    	* Merge the new settings with the old.
    	* This gives the driver an opportunity to update any private
    	* portions of the DevMode structure.
    	*/
    	dwRet = DocumentProperties(hWnd,
    	   hPrinter,
    	   pDevice,
    	   pDevMode,       /* Reuse our buffer for output. */
    	   pDevMode,       /* Pass the driver our changes. */
    	   DM_IN_BUFFER |  /* Commands to Merge our changes and */
    	   DM_OUT_BUFFER); /* write the result. */
    
    	/* Finished with the printer */
    	ClosePrinter(hPrinter);
    
    	if (dwRet != IDOK)
    	{
    	   /* If failure, cleanup and return failure. */
    	   free(pDevMode);
    	   return NULL;
    	}
    
    	/* Return the modified DevMode structure. */
    	return pDevMode;
    
    }
    				
    int main(void)
    {
    	GetLandscapeDevMode(NULL,"Ghostscript PDF");
    	return 0;
    }

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I haven't done anything with the printing functions but it sounds likie the DocumentProperties function makes changes that are local to the application (link).

    Edit: Actually, I don't think it makes any changes, it just gives you a valid DEVMODE structure to use with CreateDC
    Last edited by JaWiB; 12-03-2005 at 12:50 PM.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    It sounds like you are trying to change the default settings for the printer.

    I think this printer orientation sample may be useful.

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    39
    Quote Originally Posted by JaWiB
    Actually, I don't think it makes any changes, it just gives you a valid DEVMODE structure to use with CreateDC
    I think you are right, the code I posted only works on a 'local' level

    Quote Originally Posted by anonytmouse
    It sounds like you are trying to change the default settings for the printer.
    Exactly...and the link you provided was really useful! It did change the printer settings as desired

    But there is a particular limitation I observed (and it is important for my design). the changes don't seem to affect 'open' applications. For e.g. If I run IE first and then run this application to change the layout of printer, IE still doesn't recognize the change and keeps printing in the previous format. Restarting the application does help, but my requirement prevents me from doing so. On the contrary, applications like MS-Word updates its database automatically.
    Is there any method by which I can force applications like IE to make such changes on the fly?

    Thanks for your responses so far...

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    This appears to be an issue with IE. Assumably, it loads the printer settings at startup and doesn't repspond to the WM_DEVMODECHANGE message.

  6. #6
    Registered User
    Join Date
    May 2005
    Posts
    39

    Lightbulb

    Thanks for verifying the source of the problem anonytmouse
    Luckily for me, I am using a flash application, and unlike IE it does update on a WM_DEVMODECHANGE. Hence it solve my problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Set Printer Prior To Loading Print Dialog Box
    By Beaner in forum Windows Programming
    Replies: 3
    Last Post: 10-10-2008, 02:02 PM
  2. Problem with a file parser.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 03-17-2005, 09:54 AM
  3. Success - Output to Printer
    By noraa in forum C Programming
    Replies: 5
    Last Post: 08-04-2002, 10:12 AM
  4. Property Sheets :: MFC
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 05-09-2002, 04:04 PM
  5. Changing printer
    By goodz13 in forum C++ Programming
    Replies: 0
    Last Post: 01-16-2002, 09:46 PM

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