delphi 获取打印机默认纸张_Delphi 设置系统默认打印机

获取默认打印机名:function TForm1.GetDefaultPrinterName: string;

var

iSize: Integer;

sIniFile, sSection, sKeyName: PChar;

begin

iSize := 256;

sIniFile := 'win.ini';

sSection := 'windows';

sKeyName := 'device';

SetLength(Result, iSize);

GetPrivateProfileString(sSection, sKeyName, nil, PChar(Result), iSize, sIniFile);

Result := Copy(Result, 0, Pos(',', Result) - 1);

end;

更改默认打印机:需要Uses WinSpoolprocedure ChangeDefaultPrinter(const Name: string);

var

W2KSDP: function(pszPrinter: PChar): Boolean; stdcall;

H: THandle;

Size, Dummy: Cardinal;

PI: PPrinterInfo2;

begin

if (Win32Platform = VER_PLATFORM_WIN32_NT) and (Win32MajorVersion >= 5) then

begin

@W2KSDP := GetProcAddress(GetModuleHandle(winspl), 'SetDefaultPrinterA');

if @W2KSDP = nil then RaiseLastOSError;

if not W2KSDP(PChar(Name)) then RaiseLastOSError;

end

else

begin

if not OpenPrinter(PChar(Name), H, nil) then RaiseLastOSError;

try

GetPrinter(H, 2, nil, 0, @Size);

if GetLastError <> ERROR_INSUFFICIENT_BUFFER then RaiseLastOSError;

GetMem(PI, Size);

try

if not GetPrinter(H, 2, PI, Size, @Dummy) then RaiseLastOSError;

PI^.Attributes := PI^.Attributes or PRINTER_ATTRIBUTE_DEFAULT;

if not SetPrinter(H, 2, PI, PRINTER_CONTROL_SET_STATUS) then RaiseLastOSError;

finally

FreeMem(PI);

end;

finally

ClosePrinter(H);

end;

end;

end; //ChangeDefaultPrinterChangeDefaultPrinter函数 在D7下使用SetDefaultPrinterA,在Delphi2010以上版本使用SetDefaultPrinterWprocedure TDM.ChangeDefaultPrinter(const Name: string);

var

Device: array[0..255] of Char;

Driver: array[0..255] of char;

Port: array[0..255] of char;

s : array[0..255] of Char;

hDeviceMode: THandle;

I: Integer;

begin

for I := 0 to Printer.Printers.Count - 1 do

if Printer.Printers.Strings[I] = name then

begin

Printer.PrinterIndex := I;

Break;

end;

Printer.GetPrinter (Device, Driver, Port, hDeviceMode);

StrCopy (s, Device);

StrCat (s, ',');

StrCat (s, Driver);

StrCat (s, ',');

StrCat (s, Port);

WriteProfileString ('windows', 'device', s);

StrCopy (s, 'windows');

SendMessage (HWND_BROADCAST, WM_WININICHANGE, 0, LongInt(@s));

end;//这个函数也可以更改打印机,效果没有上面的好

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值