C Board  

Go Back   C Board > Platform Specific Boards > Windows Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-03-2005, 03:28 AM   #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;
}
leojose is offline   Reply With Quote
Old 12-03-2005, 12:47 PM   #2
carry on
 
JaWiB's Avatar
 
Join Date: Feb 2003
Location: Seattle, WA
Posts: 1,971
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
__________________
"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

Last edited by JaWiB; 12-03-2005 at 12:50 PM.
JaWiB is offline   Reply With Quote
Old 12-03-2005, 01:50 PM   #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.
anonytmouse is offline   Reply With Quote
Old 12-05-2005, 12:15 AM   #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...
leojose is offline   Reply With Quote
Old 12-05-2005, 06:08 AM   #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.
anonytmouse is offline   Reply With Quote
Old 12-05-2005, 07:16 AM   #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
leojose is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 10:29 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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