Board index » delphi » How change Windows default printer

How change Windows default printer

HI,
I need to change the default printer in windows from my Delphi application.
It's simple enough to get the list of avaliable printer but how do I select
one of them to be the default Windows printer?
Using Delphi 1 to write a application to be run on both W3.x and W95.

Thanks in advance,

Dag Idoff

dag.id...@mailbox.swipnet.se

 

Re:How change Windows default printer


Quote
"Dag Idoff" (dag.id...@mailbox.swipnet.se) writes:
> HI,
> I need to change the default printer in windows from my Delphi application.
> It's simple enough to get the list of avaliable printer but how do I select
> one of them to be the default Windows printer?
> Using Delphi 1 to write a application to be run on both W3.x and W95.

This is a copy of a unit provided by SWAG....
I don't know if it will work in Delphi 1, but it's worth a try...
=====================================================================

unit Chg_prn;

interface

uses WinTypes, WinProcs, Classes, sysutils, printers, dialogs, messages;

procedure ChangeDefaultPrinter;

implementation

procedure ChangeDefaultPrinter;

var szPrinterName, szIniInfo, szSection: PChar ;

begin
  try
   GetMem(szPrinterName,SizeOf(Char) * 256);                          
{allocate memory}
   GetMem(szIniInfo,SizeOf(Char) * 256);
   GetMem(szSection,10) ;
   StrPCopy(szPrinterName,                                            
{get name for printer selected in printerindex}
            Copy(Printer.Printers[Printer.PrinterIndex], 1,
            Pos('on', Printer.Printers[Printer.PrinterIndex]) - 2 ));
   GetProfileString('DEVICES', szPrinterName, nil, szIniInfo, 254) ;  
{locate device info in win.ini}
   if szIniInfo^ <> #0 then
     begin                                                            
{if device found, then..}
     StrCat(szPrinterName,',') ;                                      
{prepare new device line}
     StrCat(szPrinterName,szIniInfo) ;
     WriteProfileString('Windows','DEVICE',szPrinterName) ;            
{update ini file}
     StrCopy(szSection,'Windows') ;
     PostMessage(HWND_BROADCAST,WM_WININICHANGE,0,LongInt(szSection)) ;
{notify all apps - ini has changed}
   end ;
   FreeMem(szPrinterName,SizeOf(Char) * 256) ;                        
{release memory}
   FreeMem(szIniInfo,SizeOf(Char) * 256) ;
   FreeMem(szSection,10) ;
 except
   on E: EOutOfMemory do ShowMessage(E.Message) ;                      
{handles no memory to allocate}
   on E: EInvalidPointer do ShowMessage(E.Message) ;                  
{handles bad pointer}
 end ;
end;

end.

====================================================================

HTH -- Denis
--
Denis Boucher
Hull, Qubec
CANADA

Re:How change Windows default printer


Quote
Dag Idoff wrote:
> HI,
> I need to change the default printer in windows from my Delphi application.
> It's simple enough to get the list of avaliable printer but how do I select
> one of them to be the default Windows printer?
> Using Delphi 1 to write a application to be run on both W3.x and W95.

> Thanks in advance,

> Dag Idoff

> dag.id...@mailbox.swipnet.se

  Idoff,

Try the printer Selection component by Herman.Steigstra.
It can be found at his homepage at:
http://www.skzl.nl/~hsteigstra

Good luck,

Kind regards, Sjef van der Velde

Other Threads