Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Printing in landscape with WinAPI

469 views
Skip to first unread message

ball-lightning

unread,
Oct 4, 2009, 3:46:41 AM10/4/09
to
Hi,

i use either CreateDC() or PrintDlg() to retrieve a handle to a
printer. It works fine and i can print out my stuff, but i don't
know...
1.) how to print in lanscape when i used CreateDC() to open the
default printer
2.) how to check if i have selected landscape orientation when i use
the PrintDlg()-Function to choose a printer manually

How is it possible to get/set the printer orientation with pure
WinAPI?

Thanks in advance,

ball-lightning

Christian ASTOR

unread,
Oct 4, 2009, 4:24:14 AM10/4/09
to

It's in DEVMODE structure (dmOrientation) =>

PRINTDLG pd;
ZeroMemory(&pd, sizeof(PRINTDLG));
pd.lStructSize = sizeof(PRINTDLG);
pd.Flags = PD_RETURNDEFAULT | PD_RETURNDC;
PrintDlg(&pd);
DEVMODE *dm=(DEVMODE *)GlobalLock(pd.hDevMode);
dm->dmOrientation=DMORIENT_LANDSCAPE;
ResetDC(pd.hDC,dm);
GlobalUnlock(pd.hDevMode);
// Print ....

ball-lightning

unread,
Oct 4, 2009, 7:30:59 AM10/4/09
to
On 4 Okt., 10:24, Christian ASTOR <casto...@club-internet.fr> wrote:
> It's in DEVMODE structure (dmOrientation) =>
>
> PRINTDLG pd;
> ZeroMemory(&pd, sizeof(PRINTDLG));
> pd.lStructSize = sizeof(PRINTDLG);
> pd.Flags = PD_RETURNDEFAULT | PD_RETURNDC;
> PrintDlg(&pd);
> DEVMODE *dm=(DEVMODE *)GlobalLock(pd.hDevMode);
> dm->dmOrientation=DMORIENT_LANDSCAPE;
> ResetDC(pd.hDC,dm);
> GlobalUnlock(pd.hDevMode);
> // Print ....

Thanks a lot! It works fine with PrinterDlg(). I modified your code to
use it with my default printer function CreateDC():

LPBYTE lpValue;
lpValue = (LPBYTE)malloc(256);
GetPrinter(hPrinterDC, 2, lpValue, 256, &zeichen);
if (zeichen>256)
{
free(lpValue);
lpValue = (LPBYTE)malloc(zeichen);
GetPrinter(hPrinterDC, 2, lpValue, zeichen, &zeichen);
}
DEVMODE *dm=(DEVMODE *)GlobalLock(lpValue);
dm->dmOrientation=DMORIENT_LANDSCAPE;
ResetDC(hPrinterDC,dm);
GlobalUnlock(lpValue);
free(lpValue);

I used GetPrinter with parameter 2 to get the DevMode of the default
printer. It works fine, but perhaps there is a better solution?

kind regards,
ball-lightning

ball-lightning

unread,
Oct 4, 2009, 10:01:10 AM10/4/09
to
Sorry, the code is NOT correct...i think the structure lpValue is not
filled with the needed DEVMODE-data. Setting printer to landscape only
works for one time, but it seems that i submit an invalid DEVMODE-
structure and the second time i use ResetDC the program crashes.

Greetings,
ball-lightning

ball-lightning

unread,
Oct 4, 2009, 12:49:38 PM10/4/09
to
OK, this code seems to work better for me, at least it does not crash
and forces the printer to print in the desired orientation...it's
quick and dirty, without checking failures or deallocating space...

hPrinterDC = CreateDC("WINSPOOL\0", Standarddrucker, NULL, NULL);

HANDLE hPrinter;
OpenPrinter(Standarddrucker,&hPrinter, NULL);

DWORD DevSize=0;
DevSize = DocumentProperties(hWnd, hPrinter, Standarddrucker, NULL,
NULL, 0);
LPDEVMODE pDevMode;
pDevMode = (LPDEVMODE)malloc(DevSize);

DWORD dwRet=0;
dwRet = DocumentProperties(hWnd, hPrinter, Standarddrucker, pDevMode,
NULL, DM_OUT_BUFFER);

// And then your code...

DEVMODE *dm=(DEVMODE *)GlobalLock(pDevMode);
dm->dmOrientation=DMORIENT_LANDSCAPE;
ResetDC(hPrinterDC,dm);
GlobalUnlock(dm);

Greetings,
ball-lightning

0 new messages