獲取默認打印機名:
創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務領(lǐng)域包括:網(wǎng)站設(shè)計、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務,滿足客戶于互聯(lián)網(wǎng)時代的雙牌網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡建設(shè)合作伙伴!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 WinSpool
procedure 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; //ChangeDefaultPrinter
ChangeDefaultPrinter函數(shù) 在D7下使用SetDefaultPrinterA,在Delphi2010以上版本使用SetDefaultPrinterW
procedure 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;//這個函數(shù)也可以更改打印機,效果沒有上面的好
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。