用别人的To set the Windows default printer by code first you have to set the device entry in the windows section of the WIN.INI file. For example:[windows] device=Epson Stylus 200,EPS200,LPT1:The value lists the name of the printer, the driver and the port. If you don't know what to write there, just take a look at the devices section of your WIN.INI file:[Devices] Epson Stylus 200=EPS200,LPT1: Canon BJC-2000=CJRSTR,LPT1:After updating the WIN.INI file you should broadcast the WM_WININICHANGE message (calling the SendMessage API) to all running applications so they get aware of the change.The following source code example shows how to retrieve the available printers and display them in a ListBox, and then how to set the selected printer as the default printer:uses IniFiles, SysUtils, Messages;type TDevice = record Name, Driver, Port: string; end;var Devices: array of TDevice; DDevice: TDevice; // current default printerprocedure TForm1.FormCreate(Sender: TObject); var WinIni: TIniFile; DevList: TStringList; device: string; i, p: integer; begin WinIni := TIniFile.Create('WIN.INI'); // Get the current default printer device := WinIni.ReadString('windows', 'device', ',,'); if device = '' then device := ',,'; p := Pos(',', device); DDevice.Name := Copy(device, 1, p-1); device := Copy(device, p+1, Length(device)-p); p := Pos(',', device); DDevice.Driver := Copy(device, 1, p-1); DDevice.Port := Copy(device, p+1, Length(device)-p); // Get the printers list DevList := TStringList.Create; WinIni.ReadSectionValues('Devices', DevList); // Store the printers list in a dynamic array SetLength(Devices, DevList.Count); for i := 0 to DevList.Count - 1 do begin device := DevList[i]; p := Pos('=', device); Devices[i].Name := Copy(device, 1, p-1); device := Copy(device, p+1, Length(device)-p); p := Pos(',', device); Devices[i].Driver := Copy(device, 1, p-1); Devices[i].Port := Copy(device, p+1, Length(device)-p);// Add the printer to the ListBox ListBox1.Items.Add(Devices[i].Name + ' (' + Devices[i].Port + ')'); // Is the current default printer? if (CompareText(Devices[i].Name, DDevice.Name) = 0) and (CompareText(Devices[i].Driver, DDevice.Driver) = 0) and (CompareText(Devices[i].Port, DDevice.Port) = 0) then ListBox1.ItemIndex := i; // Make it the selected printer end; WinIni.Free; end;procedure TForm1.Button1Click(Sender: TObject); var WinIni: TIniFile; begin if ListBox1.ItemIndex = -1 then exit; DDevice := Devices[ListBox1.ItemIndex]; WinIni := TIniFile.Create('WIN.INI'); WinIni.WriteString('windows', 'device', DDevice.Name + ',' + DDevice.Driver + ',' + DDevice.Port); WinIni.Free; SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, LPARAM(pchar('windows'))); end;procedure TForm1.ListBox1DblClick(Sender: TObject); begin Button1Click(Sender); end;
device=Epson Stylus 200,EPS200,LPT1:The value lists the name of the printer, the driver and the port. If you don't know what to write there, just take a look at the devices section of your WIN.INI file:[Devices]
Epson Stylus 200=EPS200,LPT1:
Canon BJC-2000=CJRSTR,LPT1:After updating the WIN.INI file you should broadcast the WM_WININICHANGE message (calling the SendMessage API) to all running applications so they get aware of the change.The following source code example shows how to retrieve the available printers and display them in a ListBox, and then how to set the selected printer as the default printer:uses IniFiles, SysUtils, Messages;type
TDevice = record
Name, Driver, Port: string;
end;var
Devices: array of TDevice;
DDevice: TDevice; // current default printerprocedure TForm1.FormCreate(Sender: TObject);
var
WinIni: TIniFile;
DevList: TStringList;
device: string;
i, p: integer;
begin
WinIni := TIniFile.Create('WIN.INI'); // Get the current default printer
device := WinIni.ReadString('windows', 'device', ',,');
if device = '' then device := ',,';
p := Pos(',', device);
DDevice.Name := Copy(device, 1, p-1);
device := Copy(device, p+1, Length(device)-p);
p := Pos(',', device);
DDevice.Driver := Copy(device, 1, p-1);
DDevice.Port := Copy(device, p+1, Length(device)-p); // Get the printers list
DevList := TStringList.Create;
WinIni.ReadSectionValues('Devices', DevList); // Store the printers list in a dynamic array
SetLength(Devices, DevList.Count);
for i := 0 to DevList.Count - 1 do begin
device := DevList[i];
p := Pos('=', device);
Devices[i].Name := Copy(device, 1, p-1);
device := Copy(device, p+1, Length(device)-p);
p := Pos(',', device);
Devices[i].Driver := Copy(device, 1, p-1);
Devices[i].Port := Copy(device, p+1, Length(device)-p);// Add the printer to the ListBox
ListBox1.Items.Add(Devices[i].Name
+ ' (' + Devices[i].Port + ')'); // Is the current default printer?
if (CompareText(Devices[i].Name, DDevice.Name) = 0) and
(CompareText(Devices[i].Driver, DDevice.Driver) = 0) and
(CompareText(Devices[i].Port, DDevice.Port) = 0) then
ListBox1.ItemIndex := i; // Make it the selected printer
end;
WinIni.Free;
end;procedure TForm1.Button1Click(Sender: TObject);
var
WinIni: TIniFile;
begin
if ListBox1.ItemIndex = -1 then exit;
DDevice := Devices[ListBox1.ItemIndex];
WinIni := TIniFile.Create('WIN.INI');
WinIni.WriteString('windows', 'device', DDevice.Name
+ ',' + DDevice.Driver + ',' + DDevice.Port);
WinIni.Free;
SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0,
LPARAM(pchar('windows')));
end;procedure TForm1.ListBox1DblClick(Sender: TObject);
begin
Button1Click(Sender);
end;
-----------
if Printer.Printers.Count < 1 then
begin
Application.MessageBox('未安装打印机,请先安装打印机!','提示',MB_ICONWARNING + MB_OK);
Exit;
end;
Printer.Copies := 5; //打印份数
Printer.Orientation := poPortrait; //纸张方向
Printer.PageHeight := 200;//纸张高度
Printer.PageWidth := 100; //纸张宽度
Printer.BeginDoc; //准备打印
Printer.EndDoc; //开始打印
我用你的语句编译时,以下两句出现问题
Printer.PageHeight := 200;
Printer.PageWidth := 100;
错误为:
cannot assign to a read-only property