具体代码

  1. procedure SetDefaultPrinter(NewDefPrinter: string);
  2. var
  3. ResStr: array[0..255] of Char;
  4. begin
  5. StrPCopy(ResStr, NewdefPrinter);
  6. WriteProfileString('windows', 'device', ResStr);
  7. StrCopy(ResStr, 'windows');
  8. SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, Longint(@ResStr));
  9. end;

复制代码

参数:NewDefPrinter是具体的打印机名称,可以到控制面板-设备和打印机下查看,例如:Microsoft XPS Document Writer
使用代码

  1. 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.

 
 
 
05-11 13:47