arjunram
asked on
Setting printer paper trays to a network printer for a print job
How does one set the printer tray for a particular print job in Windows SDK programming ? I have tried CreateDC, ResetDC Setprinter? In dire need of this please help. This is VC++.
what do u mean by tray - paper size?
Here is the working sample (console app)
1) run it a.exe - it'll print all trays & ids
2) run a.exe tray_id - it'll print test page
#include <windows.h>
#include <iostream.h>
#include <stdio.h>
// Usage: a.exe [tray_#]
PRINTDLG pdSetup;
void GetDefPrinter() {
memset(&pdSetup,0,sizeof(P RINTDLG));
pdSetup.lStructSize=sizeof (PRINTDLG) ;
pdSetup.Flags=PD_RETURNDEF AULT;
PrintDlg(&pdSetup);
}
void main(int argc,char* argv[])
{
// Fill PRINTDLG struct
GetDefPrinter();
// Obtain printer info
LPDEVMODE dm=LPDEVMODE(GlobalLock(pd Setup.hDev Mode));
LPDEVNAMES dn=LPDEVNAMES(GlobalLock(p dSetup.hDe vNames));
if(argc<2) { // Print trays info
// Check trays
DWORD nTrays=DeviceCapabilities( LPTSTR(dn) +dn->wDevi ceOffset,
LPTSTR(dn)+dn->wOutputOffs et,DC_BINN AMES,0,0);
if(nTrays==DWORD(-1) || nTrays==0) {
cout<<"Cannot get trays count!"<<endl;
exit(1);
}
typedef char trayNameT[24];
trayNameT* trayName=new char[nTrays][24];
DeviceCapabilities(LPTSTR( dn)+dn->wD eviceOffse t,
LPTSTR(dn)+dn->wOutputOffs et,DC_BINN AMES,LPTST R(trayName ),0);
LPWORD trayID=new WORD[nTrays];
DeviceCapabilities(LPTSTR( dn)+dn->wD eviceOffse t,
LPTSTR(dn)+dn->wOutputOffs et,DC_BINS ,LPTSTR(tr ayID),0);
for(DWORD i=0;i<nTrays;i++) {
cout<<trayID[i]<<' '<<trayName[i]<<endl;
}
}
else {
// Get tray ID
DWORD id=strtoul(argv[1],0,0);
dm->dmDefaultSource=id;
HDC hdcPrinter=CreateDC(LPTSTR (dn)+dn->w DriverOffs et,
LPTSTR(dn)+dn->wDeviceOffs et,0,dm);
// Test printing
TEXTMETRIC tmPrinter; // Printer metricies
int cxPage, cyPage; // Width and Height of page
char szText[120]; // Buffer for output
cxPage=GetDeviceCaps(hdcPr inter, HORZRES);
SelectObject(hdcPrinter, CreatePen(PS_SOLID, 1, 0));
DOCINFO di={sizeof(DOCINFO),"Test" ,NULL};
int jobId=StartDoc(hdcPrinter, &di);
StartPage(hdcPrinter);
Rectangle(hdcPrinter, 0, 0, cxPage, 500);
SelectObject(hdcPrinter, CreatePen(PS_SOLID, 10, 0));
Rectangle(hdcPrinter, 2000, 2000, 2600, 2600);
SelectObject(hdcPrinter, CreatePen(PS_SOLID, 50, 0));
Rectangle(hdcPrinter, 200, 1000, 300, 1300);
wsprintf(szText, "Printer Test");
TextOut(hdcPrinter, 50, (20 * 5), szText, strlen(szText));
if (EndPage(hdcPrinter) > 0) {
EndDoc(hdcPrinter);
}
DeleteObject(SelectObject( hdcPrinter , GetStockObject(BLACK_PEN)) );
DeleteDC(hdcPrinter);
}
GlobalUnlock(pdSetup.hDevN ames);
GlobalUnlock(pdSetup.hDevM ode);
}
1) run it a.exe - it'll print all trays & ids
2) run a.exe tray_id - it'll print test page
#include <windows.h>
#include <iostream.h>
#include <stdio.h>
// Usage: a.exe [tray_#]
PRINTDLG pdSetup;
void GetDefPrinter() {
memset(&pdSetup,0,sizeof(P
pdSetup.lStructSize=sizeof
pdSetup.Flags=PD_RETURNDEF
PrintDlg(&pdSetup);
}
void main(int argc,char* argv[])
{
// Fill PRINTDLG struct
GetDefPrinter();
// Obtain printer info
LPDEVMODE dm=LPDEVMODE(GlobalLock(pd
LPDEVNAMES dn=LPDEVNAMES(GlobalLock(p
if(argc<2) { // Print trays info
// Check trays
DWORD nTrays=DeviceCapabilities(
LPTSTR(dn)+dn->wOutputOffs
if(nTrays==DWORD(-1) || nTrays==0) {
cout<<"Cannot get trays count!"<<endl;
exit(1);
}
typedef char trayNameT[24];
trayNameT* trayName=new char[nTrays][24];
DeviceCapabilities(LPTSTR(
LPTSTR(dn)+dn->wOutputOffs
LPWORD trayID=new WORD[nTrays];
DeviceCapabilities(LPTSTR(
LPTSTR(dn)+dn->wOutputOffs
for(DWORD i=0;i<nTrays;i++) {
cout<<trayID[i]<<' '<<trayName[i]<<endl;
}
}
else {
// Get tray ID
DWORD id=strtoul(argv[1],0,0);
dm->dmDefaultSource=id;
HDC hdcPrinter=CreateDC(LPTSTR
LPTSTR(dn)+dn->wDeviceOffs
// Test printing
TEXTMETRIC tmPrinter; // Printer metricies
int cxPage, cyPage; // Width and Height of page
char szText[120]; // Buffer for output
cxPage=GetDeviceCaps(hdcPr
SelectObject(hdcPrinter, CreatePen(PS_SOLID, 1, 0));
DOCINFO di={sizeof(DOCINFO),"Test"
int jobId=StartDoc(hdcPrinter,
StartPage(hdcPrinter);
Rectangle(hdcPrinter, 0, 0, cxPage, 500);
SelectObject(hdcPrinter, CreatePen(PS_SOLID, 10, 0));
Rectangle(hdcPrinter, 2000, 2000, 2600, 2600);
SelectObject(hdcPrinter, CreatePen(PS_SOLID, 50, 0));
Rectangle(hdcPrinter, 200, 1000, 300, 1300);
wsprintf(szText, "Printer Test");
TextOut(hdcPrinter, 50, (20 * 5), szText, strlen(szText));
if (EndPage(hdcPrinter) > 0) {
EndDoc(hdcPrinter);
}
DeleteObject(SelectObject(
DeleteDC(hdcPrinter);
}
GlobalUnlock(pdSetup.hDevN
GlobalUnlock(pdSetup.hDevM
}
ASKER
Crash occurs when doing a createDC. Please let me knwo on any further advances on this issue. I dire need. If you are close to the fix please let me know.
This code works fine.
At least, I've told you what to do.
Thank you for polite answer.
At least, I've told you what to do.
Thank you for polite answer.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi,
None of the windows applications work(print to a different tray) if the printer is a network printer. Looks like there is a problem in the setting. ThanX a lot for the sample code.
None of the windows applications work(print to a different tray) if the printer is a network printer. Looks like there is a problem in the setting. ThanX a lot for the sample code.
I've checked this code at network printer before post it here.
ASKER