Mega Code Archive

 
Categories / Delphi / Examples
 

Printerdevicesettings

Code to bring up a form with the printer device settings- (same form that you see if on the desktop you click Settings/Printers/select a printer/right mouse/Properties/ last tab on the right (Device Settings)... the code below supposedly works under Delphi 4.0 BUT BUT BUT under Delphi 5.0 an exception is raised as you leave the function... (ie it's 'close' but not quite there)... ************************************************************** ///////////////////////////////////////////////////////////// procedure TMainForm.MenuFilePrnDeviceSettingsClick(Sender: TObject); //Here, with just a few lines of code, we use the WIN32 API //to open up a printer properties dialog box. This code will //activate the same printer dialog box as the TPrinterSetupDialog //in Delphi4. We will be using the Delphi function //Printer.GetPrinter(), and the WIN32 API calls OpenPrinter(), //PrinterProperties(), and ClosePrinter() along with the DEVMODE //structure. First we will need to make sure we add Printers and //WinSpool to our Units uses clause. In our TButton.OnClick event //we will first set our TPrinter.PrinterIndex to default printer //by setting this value to -1. The comments in the code are //pretty straight foward, they should explain the rest as best //as possible. Printer.GetPrinter() gets the properties of the //current printer in the TPrinter.PrinterIndex. The first //parameter is our printer device. The second parameter is our //printer driver. The third parameter is our printer port. The //last parameter is a handle to our printer device mode. pDevMode //is a pointer to a DEVMODE structure which contains information //about the device initialization and environment of a printer. //The pDevMode structure is explained in detail in an earlier //printing tip. OpenPrinter() retrieves a handle identifying the //specified printer. The first parameter is the printer name. //The second parameter is a variable THandle returned for access //to the printer. The last parameter is a PRINTER_DEFAULTS WIN32 //API structure that specifies the default data type, environment, //initialization data, and access rights for a printer. //ClosePrinter() closes the specified printer. The only parameter //is the THandle received form OpenPrinter() as described above. var MyPrinter, MyHandle : THandle; MyDevModeOut,MyDevModeIn: pDevmode; MyDriver, MyPort : array [0..255] of Char; MyDevice : PChar; begin //set Printer index to the default printer... Printer.PrinterIndex := -1; //get our printer properties... Printer.GetPrinter(MyDevice, MyDriver, MyPort, MyHandle); //OpenPrinter WIN32 API call returns hPrinter to us... OpenPrinter(MyDevice, MyPrinter, nil); try //open printer properties dialog box... PrinterProperties(Handle, MyPrinter); finally //close our printer handle... if (MyPrinter <> 0) then ClosePrinter(MyPrinter); end; end;