Delphi获取系统打印机设置
在dialog->prints->printersetupdialog1增加这个控件
在程序中printersetupdialog1.execute就可以看到打印机设置的信息了
Delphi枚举出系统所有的打印机
在uses中包含prints
procedure Tprintsetform.FormShow(Sender: TObject); //获取系统所有打印机
begin
if (printer.Printers.Count=0) then
未安装打印机
else
安装了Printers.printers.count台打印机;
当系统打印机数不为0时,必定有一台是被默认的; 反过来说,如果 printer.Printers.Count=0//系统没有安装任何打印机 则没有默认打印机
显示默认打印机
ComboBox1.Text:=printer.Printers[printer.printerindex]
Tprinter.printers.count可以得到打印机数
Tprinter.printers可以得到打印机列表
Tprinter.printerINDEX可以设当前打印机
Tprinter.printerINDEX:=-1可以恢复默认打印机
跳过预览,直接打印
frxReport1.PrintOptions.ShowDialog := False;
frxReport1.Print;
Delphi 打印---打印机选择
var
mdevice : array[0..255] of char;
mdriver : array[0..255] of char;
mport : array[0..255] of char;
mhdmode : thandle;
mpdmode : pdevmode;
begin
printer.getprinter(mdevice, mdriver, mport, mhdmode);
printer.setprinter('fineprint pdffactory pro', mdriver, mport, mhdmode); //设置打印机
printer.begindoc;
printer.canvas.moveto(0, 0);
printer.canvas.lineto(300, 300);
printer.canvas.textout(20, 20, '打印文字');
printer.enddoc;
end;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
1.取得所有安装在本机上的打印机:
procedure TForm1.GetPrinterNames;
var
buffer: TPrinterBuffer;
currPos,n: integer;
printerName: string;
begin
PrinterNames.Free;
PrinterNames := TStringList.Create;
if GetProfileString(PChar('PrinterPorts'), nil, '', buffer, MAXPRINTERBUFFER) > 0 then
begin
currPos := 0;
n := 0;
while (true) do
begin
n := n + 1;
printerName := ParseNames(buffer, currPos);
if printerName <> '' then
begin
printerName := IntToStr(n) + ': ' + PrinterName;
PrinterNames.Add(printerName);
end else
break;
end;
end;
end;
function TForm1.ParseNames(const namebuffer: TPrinterBuffer;
var startPos: integer): string;
var
i, j, NameLength: integer;
str: string;
begin
result := '';
if (startPos > High(namebuffer)) or (namebuffer[startPos] = Chr(0)) then
exit;
for i := startPos to High(namebuffer) do
begin
if namebuffer[i] = Chr(0) then
begin
nameLength := i - startPos;
SetLength(str, nameLength);
for j := 0 to nameLength - 1 do
str[j+1] := namebuffer[startPos + j];
result := str;
startPos := i + 1;
break;
end;
end;
end;
2.设置打印机为默认打印机:
function TForm1.SetDefaultPrinter(const PrinterName: String): boolean;
var
s2 : string;
dum1 : Pchar;
xx, qq : integer;
const
cs1 : pchar = 'Windows';
cs2 : pchar = 'Device';
cs3 : pchar = 'Devices';
cs4 : pchar = #0;
begin
try
xx := 254;
GetMem( dum1, xx);
Result := False;
qq := GetProfileString(cs3,pchar(printerName), #0, dum1, xx);
if (qq > 0)and(trim(strpas(dum1)) <> '')then
begin
s2 := PrinterName + ',' + strpas(dum1);
while GetProfileString(cs1, cs2, cs4, dum1, xx) > 0 do
WriteProfileString( cs1, cs2, #0);
WriteProfileString( cs1, cs2, pchar(s2));
Result := True;
end;
finally
FreeMem(dum1);
end;
end;