The following is a complete example from Borland. Pay attention to the two judgment statements, which means that sometimes the information is not correct only by getprinters, and the help of WIN. INI is also required.
Uses printers;
{$ IFNDEF WIN32}
Const max_path= 144;
{ENDIF}
Procedure TForm1.Button1Click (Sender: TObject );
Var
PDevice: pChar;
PDriver: pChar;
PPort: pChar;
HDMode: THandle;
Begin
If PrintDialog1.Execute then begin
GetMem (pDevice, cchDeviceName );
GetMem (pDriver, MAX_PATH );
GetMem (pPort, MAX_PATH );
Printer. GetPrinter (pDevice, pDriver, pPort, hDMode );
If lStrLen (pDriver) = 0 then begin
GetProfileString ('devices', pDevice, '', pDriver, MAX_PATH );
PDriver [pos (',', pDriver)-1]: = #0;
End;
If lStrLen (pPort) = 0 then begin
GetProfileString ('devices', pDevice, '', pPort, MAX_PATH );
LStrCpy (pPort, @ pPort [lStrLen (pPort) + 2]);
End;
FreeMem (pDevice, cchDeviceName );
FreeMem (pDriver, MAX_PATH );
FreeMem (pPort, MAX_PATH );
End;
End;
· Get the status information of the printer queue:
In some cases, you need to obtain the status information of the printer queue. You can use the Windows API function ENumJobs to obtain the status information of the printer queue.
The following program snippet describes how to obtain the status information of the printer queue.
(1) create a new project;
(2) Add a Button component and a Memo component to form Form1 to set relevant attributes;
(3) Add the OnClick event of Button1.
Procedure TForm1.Button1Click (Sender: TObject );
Var
LPHandle: THandle; // printer handle
LPStrBuf: Array [0 .. 256] of Char; // printer name
NoJobs: Word; // number of jobs to be obtained
S: LongWord; // number of bytes in Job_Info
Job_Info: Array [0 .. 10] of JOB_INFO_1; // print queue Information
CbNeeded: Cardinal;
CReturned: Cardinal; // number of returned jobs
Ret: LongBool;
I: integer;
Begin
LPStrBuf: = 'maid; // specify the printer name
// Enable the printer
If OpenPrinter () then
Begin
MessageBox (Form1.Handle, 'failed to open the printer. ', 'Info', MB_ OK );
Exit;
End;
S: = SizeOf (Job_Info );
CbNeeded: = 0;
CReturned: = 0;
NoJobs: = 10;
Ret: = ENumJobs (LPHandle, 0, NoJobs, 1, @ Job_Info, s, cbNeeded, cReturn );
// Display the printed queue Information
Memo1.Lines. Add ('total print tasks: '+ InttoStr (cReturned) +. ');
For I: = 0 to cReturned-1 do
Memo1.Lines. Add ('quarter '+ IntToStr (I + 1) + 'print tasks' + IntToStr (Job_Info [I]. TotalPages) + 'page. ');
End;
During the test, you should set the LPStrBuf value based on the printer installed in the system. You can open the printer manager in the control panel of the Windows system to verify the running result.
· Get the default printer information:
The Windows. ini file contains the following configuration information:
[Windows]
Load =
Run =
NullPort = None
Device = maid, HPPCL5MS, LPT1
.............
In Windows, the device key specifies the default printer information in the current system. You can use the GetProfileString function of WindowsAPI to obtain this information. The original form of the GetProfileString function is as follows:
DWORDGetProfileString (
LPCTSTRlpAppName, // string of the specified node name
LPCTSTRlpKeyName, // string that specifies the key name
LPCTSTRlpDefault, // The string returned when the key name is not found
LPTSTRlpReturnedString, // string returned when the key name is found
DWORDnSize // The number of bytes of lpReturnedString
);
Example:
(1) create a new project;
(2) Add a Button component and an Edit component on the form Form1 to set relevant properties;
(3) Add the OnClick event of Button1.
Procedure TForm1.Button1Click (Sender: TObject );
Var
AppName: Array [0 .. 256] of Char; // segment name string
KeyName: Array [0 .. 256] of Char; // key name string
DefaultString: Array [0 .. 256] of Char; // The string returned by default when the key name is not found
ReturnedString: Array [0 .. 256] of Char; // string returned when the key name is found
Begin
AppName: = 'windows'; // node name
KeyName: = 'device'; // key name
GetProfileString (AppName, KeyName, DefaultString, ReturnedString, Sizeof (ReturnedString ));
Edit1.Text: = ReturnedString; // display the result
End;
In the above test, you can open win. ini for comparison.