Actually i'm working in a project which need print a test page to a particular printer. the test page must be the same which of prints Windows in the option printer properties -> print test page.
How i can do this in delphi?
This code will print the test page for the default printer:
uses ShellAPI, printers;
{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject);
var
Device, Driver, Port: Array [0..255] of Char;
hDevMode: THandle;
begin
Printer.GetPrinter(Device, Driver, Port, hDevmode);
ShInvokePrinterCommand(handle, PRINTACTION_TESTPAGE, Device, nil, true );
end;
Result:
Enjoy :)
You can use the PrintTestPage
method the from the Win32_Printer
wmi class
check this sample
{$APPTYPE CONSOLE}
uses
SysUtils,
ActiveX,
ComObj;
procedure PrintTestPage(const PrinterName:string);
var
FSWbemLocator : OLEVariant;
FWMIService : OLEVariant;
FWbemObjectSet: OLEVariant;
FWbemObject : OLEVariant;
oEnum : IEnumvariant;
iValue : LongWord;
PrintResult : Integer;
begin;
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
FWMIService := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', '');
FWbemObjectSet:= FWMIService.ExecQuery(Format('SELECT * FROM Win32_Printer Where Name="%s"',[PrinterName]),'WQL',0);
oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
if oEnum.Next(1, FWbemObject, iValue) = 0 then
begin
PrintResult:=FWbemObject.PrintTestPage;
if PrintResult=0 then
Writeln('Success')
else
Writeln('An error occurred');
end;
end;
begin
try
CoInitialize(nil);
try
PrintTestPage('MyPrinter');
finally
CoUninitialize;
end;
except
on E:Exception do
Writeln(E.Classname, ':', E.Message);
end;
Readln;
end.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With