delphi编程指针(系统默认打印机的获取和设置)
delphi编程指针(系统默认打印机的获取和设置)procedure TChildSetPrinterForm.FormCreate(Sender: TObject); begin // 窗体创建时获取系统默认打印机 GetSystemDefaultPrinter; end; procedure TChildSetPrinterForm.GetSystemDefaultPrinter; begin // 将系统默认打印机的名称赋值给窗体中的 TEdit 控件中 DefaultPrinterEdit.Text:=GetDefaultPrinter.Split([' '])[0]; end; // 调用 GetProfileString 获取系统的默认打印机 function TChildSetPrinterForm.GetDefaultPrinter: String; var ResStr: arra
在 Windows 环境下,获取默认打印机可以使用 WIN32 API 中的 GetProfileString 函数来实现;设置默认打印机可以使用 WIN32 API 中的 WriteProfileString 函数来实现;
在 Delphi 中,使用 Printers 单元中的 Printer 可以获取所有的打印机及某个打印机的具体信息。
基于以上方法,在 Delphi 中对于系统默认打印机的获取和设置程序实现如下,界面如下:
默认打印机显示
单击“默认打印机设置”按钮后弹出如下对话框:
打印机列表对话框
打印机列表选项
获取系统当前默认打印机的代码如下:
procedure TChildSetPrinterForm.FormCreate(Sender: TObject);
begin
// 窗体创建时获取系统默认打印机
GetSystemDefaultPrinter;
end;
procedure TChildSetPrinterForm.GetSystemDefaultPrinter;
begin
// 将系统默认打印机的名称赋值给窗体中的 TEdit 控件中
DefaultPrinterEdit.Text:=GetDefaultPrinter.Split([' '])[0];
end;
// 调用 GetProfileString 获取系统的默认打印机
function TChildSetPrinterForm.GetDefaultPrinter: String;
var
ResStr: array[0..255] of Char;
begin
GetProfileString('Windows' 'device' '' ResStr 255);
Result := StrPas(ResStr);
end;
单击“默认打印机设置”按钮的代码如下:
procedure TChildSetPrinterForm.SetDefaultPrinterBitBtnClick(Sender: TObject);
begin
// 创建默认打印机设置对话框窗体
DefaultPrinterDialogForm:=TDefaultPrinterDialogForm.Create(Self);
// 以模态方式打开该窗体
if DefaultPrinterDialogForm.ShowModal = mrOk then
begin
// 如果用户单击“确定”按钮,则重新获取系统默认打印机
GetSystemDefaultPrinter;
end;
DefaultPrinterDialogForm.Free;
end;
单击“默认打印机设置”按钮后弹出的对话框代码如下:
unit UnitDefaultPrinterDialog;
interface
uses
Winapi.Windows Winapi.Messages System.SysUtils System.Variants System.Classes Vcl.Graphics
Vcl.Controls Vcl.Forms Vcl.Dialogs Vcl.StdCtrls Printers;
type
TDefaultPrinterDialogForm = class(TForm)
Label1: TLabel;
PrintersComboBox: TComboBox; // 打印机列表选项
CancelButton: TButton; // 取消按钮
OkButton: TButton; // 确定按钮
procedure FormCreate(Sender: TObject);
procedure CancelButtonClick(Sender: TObject); // 取消按钮单击事件
procedure OkButtonClick(Sender: TObject); // 确定按钮单击事件
private
{ Private declarations }
procedure SetDefaultPrinter; // 设置默认打印机
public
{ Public declarations }
end;
var
DefaultPrinterDialogForm: TDefaultPrinterDialogForm;
implementation
{$R *.dfm}
procedure TDefaultPrinterDialogForm.CancelButtonClick(Sender: TObject);
begin
// 取消
ModalResult:=mrCancel;
end;
procedure TDefaultPrinterDialogForm.FormCreate(Sender: TObject);
begin
// 如果 Printer 对象不存在或者其中的打印机列表数量为0,则退出;表示系统中没有打印机
if (Printer = nil) or (printer.Printers.Count = 0) then Exit;
PrintersComboBox.Items:=Printer.Printers; // 将打印机列表显示在组合框中
PrintersComboBox.ItemIndex := Printer.PrinterIndex; // 将当前的打印机索引设置组合框的当前索引
end;
procedure TDefaultPrinterDialogForm.OkButtonClick(Sender: TObject);
begin
// 单击确定按钮时,设置默认打印机
SetDefaultPrinter;
ModalResult:=mrOk;
end;
procedure TDefaultPrinterDialogForm.SetDefaultPrinter;
var
MyHandle : THandle;
MyDevice MyDriver MyPort: array [0..255] of Char;
begin
{ 根据 ComboBox 的 ItemIndex 将打印机设置为选中的打印机 }
Printer.PrinterIndex := PrintersComboBox.ItemIndex;
{ 获取选中的打印机属性 }
Printer.GetPrinter(MyDevice MyDriver MyPort MyHandle);
{ 通过分隔上述接收的每个字符数组,创建 WriteProfileString() 所需要的字符串 }
StrCat( MyDevice ' ');
StrCat( MyDevice MyDriver );
StrCat( MyDevice ' ');
StrCat( MyDevice MyPort );
{ 将新的默认打印机复制到系统的 windows ini 文件中,复制到设备下的 [windows] 部分 }
WriteProfileString('WINDOWS' 'DEVICE' MyDevice);
end;
end.
在对话框中单击“确定”按钮后,将用户选择的打印机设置为系统默认打印机,同时关闭对话框。