具体代码
- procedure SetDefaultPrinter(NewDefPrinter: string);
- var
- ResStr: array[0..255] of Char;
- begin
- StrPCopy(ResStr, NewdefPrinter);
- WriteProfileString('windows', 'device', ResStr);
- StrCopy(ResStr, 'windows');
- SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, Longint(@ResStr));
- end;
参数:NewDefPrinter是具体的打印机名称,可以到控制面板-设备和打印机下查看,例如:Microsoft XPS Document Writer
使用代码
- SetDefaultPrinter('Microsoft XPS Document Writer');
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,Printers, StdCtrls;
type
TForm1 = class(TForm)
Button3: TButton;
ComboBox1: TComboBox;
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure SetDefaultPrinter(NewDefPrinter: string);
var
ResStr: array[0..255] of Char;
begin
StrPCopy(ResStr, NewdefPrinter);
WriteProfileString('windows', 'device', ResStr);
StrCopy(ResStr, 'windows');
SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, Longint(@ResStr));
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
ComboBox1.Items:=printer.Printers;
ComboBox1.Text:=IntToStr(SizeOf(ComboBox1.Items));
SetDefaultPrinter('Microsoft XPS Document Writer');
end;
end.