1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
//---------------------------------------------------------------------------
bool Test_Print_Duplex(int IndexPrinter)
{
char Device[255],Driver[255],Port[255];
THandle hDevMode;
TPrinter *MyPrinter = Printer();
MyPrinter->PrinterIndex=IndexPrinter;
MyPrinter->GetPrinter(Device,Driver,Port,hDevMode);
if(DeviceCapabilities(Device,Port,DC_DUPLEX,NULL,NULL)!=0)
return true;
else return false;
}
bool SetPrinterDuplex(String sPrinterName , long nDuplexSettings)
{
HANDLE hPrinter;
PRINTER_DEFAULTS pd;
LPPRINTER_INFO_2 pinfo;
//DEVMODE dm;
LPDEVMODE dm;
//LPDEVMODE *yDevModeData;
LPDEVMODE yDevModeData;
LPBYTE yPInfoMemory;
LPDWORD nBytesNeeded;
long nRet;
LPDWORD nJunk;
try
{
if((nDuplexSettings<1)||(nDuplexSettings>3))
{
MessageDlg("Erreur:\r\nle paramètre Recto Verso est incorrect: "+IntToStr(nDuplexSettings),mtError, TMsgDlgButtons() << mbCancel, 0);
return false;
}
pd.DesiredAccess=PRINTER_ALL_ACCESS;
nRet=OpenPrinter(sPrinterName.c_str(),&hPrinter,&pd);
if((nRet==0)||(hPrinter==0))
{
if(GetLastError()==5)
MessageDlg("Erreur:\r\nAccès refusé à l'imprimante!",mtError, TMsgDlgButtons() << mbCancel, 0);
else MessageDlg("Erreur:\r\nL'accès imprimante à échoué, vérifier:\r\n\t*L'imprimante est allumée et en ligne.\r\n\t*En cas d'imprimante partagée, que le PC correspondant soit allumé.",mtError, TMsgDlgButtons() << mbCancel, 0);
return false;
}
nRet = DocumentProperties (0, hPrinter , sPrinterName.c_str(),0,0,0);
if(nRet<0)
{
MessageDlg("Erreur:\r\nImpossible d'obtenir la taille des paramètres imprimante (DEVMODE)!",mtError, TMsgDlgButtons() << mbCancel, 0);
if(hPrinter!=0)
ClosePrinter(hPrinter);
return false;
}
// yDevModeData = new LPDEVMODE [nRet+100];
nRet=DocumentProperties(0,hPrinter,sPrinterName.c_str(),yDevModeData/*[0]*/,0 , DM_OUT_BUFFER);
if(nRet<0)
{
MessageDlg("Erreur:\r\nImpossible d'obtenir les paramètres imprimante (DEVMODE)!",mtError, TMsgDlgButtons() << mbCancel, 0);
if(hPrinter!=0)
ClosePrinter(hPrinter);
return false;
}
/*Call */CopyMemory ( dm, yDevModeData/*[0]*/,sizeof(dm));
if((dm->dmFields&DM_DUPLEX)!=DM_DUPLEX)
{
MessageDlg("Erreur:\r\nImpossible d'affecter le mode recto Verso! 2 cas possibles\r\n\t*L'imprimante ne supporte pas le recto Verso\r\n\t*Le driver de l'imprimante ne supporte pas son affectation par programmation.",mtError, TMsgDlgButtons() << mbCancel, 0);
if(hPrinter!=0)
ClosePrinter(hPrinter);
return false;
}
dm->dmDuplex=nDuplexSettings;
/*Call */CopyMemory (yDevModeData/*[0]*/ , dm , sizeof(dm));
nRet = DocumentProperties (0, hPrinter, sPrinterName.c_str(), yDevModeData/*[0]*/ , yDevModeData/*[0]*/,DM_IN_BUFFER|DM_OUT_BUFFER);
if(nRet<0)
{
MessageDlg("Erreur:\r\nImpossible d'affecter le mode recto Verso sur cette imprimante!!!",mtError, TMsgDlgButtons() << mbCancel, 0);
if(hPrinter!=0)
ClosePrinter(hPrinter);
return false;
}
GetPrinter(hPrinter, 2, 0, 0, nBytesNeeded);
if(nBytesNeeded==0)
{
if(hPrinter!=0)
ClosePrinter(hPrinter);
return false;
}
// yPInfoMemory = new LPDEVMODE [nRet+100];
nRet= GetPrinter (hPrinter, 2, yPInfoMemory /*[0]*/, (DWORD) nBytesNeeded, nJunk);
if(nRet == 0)
{
MessageDlg("Erreur:\r\nImpossible d'obtenir les paramètres imprimante!!!",mtError, TMsgDlgButtons() << mbCancel, 0);
if(hPrinter!=0)
ClosePrinter(hPrinter);
return false;
}
/*Call*/ CopyMemory(pinfo, yPInfoMemory/*[0]*/, sizeof(pinfo));
pinfo->pDevMode = yDevModeData/*[0]*/;
pinfo->pSecurityDescriptor = 0;
/*Call*/ CopyMemory(yPInfoMemory/*[0]*/, pinfo, sizeof(pinfo));
nRet=SetPrinter(hPrinter, 2, yPInfoMemory/*[0]*/, 0);
if(nRet==0)
MessageDlg("Erreur:\r\nImpossible d'affecter les paramètres imprimante!!!",mtError, TMsgDlgButtons() << mbCancel, 0);
if(hPrinter!=0)
ClosePrinter(hPrinter);
return true;
}
catch(...)
{
if(hPrinter!=0)
ClosePrinter(hPrinter);
return false;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
ListBox2->Items->Text=Printer()->Printers->Text;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListBox2Click(TObject *Sender)
{
if(Test_Print_Duplex(ListBox2->ItemIndex))
{
Label1->Caption="Recto Verso Autorisé";
Label1->Color=clBtnFace;
}
else
{
Label1->Color=clRed;
Label1->Caption="Recto Verso Non Autorisé";
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListBox2DblClick(TObject *Sender)
{
if(Test_Print_Duplex(ListBox2->ItemIndex))
{
Label1->Caption="Recto Verso Autorisé";
Label1->Color=clBtnFace;
SetPrinterDuplex(ListBox2->Items->Strings[ListBox2->ItemIndex] , DMDUP_VERTICAL);
TPrinter *MyPrinter=Printer();
MyPrinter->PrinterIndex=ListBox2->ItemIndex;
MyPrinter->Title="Test Recto Verso";
MyPrinter->BeginDoc();
MyPrinter->Canvas->TextOutA(25,25,"Recto");
MyPrinter->NewPage();
MyPrinter->Canvas->TextOutA(25,25,"Verso");
MyPrinter->EndDoc();
}
else
{
Label1->Color=clRed;
Label1->Caption="Recto Verso Non Autorisé";
}
}
//--------------------------------------------------------------------------- |
Partager