Board index » delphi » Changing DEFAULT PRINTER

Changing DEFAULT PRINTER

hi all,

Does anyone know how to change de default printer through delphi
Application??

thanx for any help....

Curumim Tech

 

Re:Changing DEFAULT PRINTER


Quote
Curumim Tech wrote:
> Does anyone know how to change de default printer through delphi
> Application??

Try this:

procedure ChangeDefaultPrinter;
var
  Device : array[0..255] of char;
  Driver : array[0..255] of char;
  Port   : array[0..255] of char;
  hDeviceMode: THandle;
begin
  Printer.PrinterIndex := ....; // select printer to make default
  Printer.GetPrinter(Device, Driver, Port, hDeviceMode);
  StrCat( Device, ',');
  StrCat( Device, Driver );
  StrCat( Device, ',');
  StrCat( Device, Port );
  WriteProfileString( 'windows', 'device', Device );
  StrCopy( Device, 'windows' );
  SendMessage( HWND_BROADCAST, WM_WININICHANGE, 0, longint(@Device));
end;

Bye, Udo
--

Please reply only to this newsgroup. Do not send any unrequested mails.

Re:Changing DEFAULT PRINTER


Quote
Curumim Tech <mmiy...@zaz.com.br> wrote in message

news:8865cr$jj4$1@srv4-poa.zaz.com.br...

Quote
> Does anyone know how to change de default printer through delphi
> Application??

The code below (tested with D2) sets the currently selected printer as the
persistent default. Tested under Win9x with local printers only, so I can't
say whether it would work with NT or network printers.

Note the calls to the two different GetPrinter functions - one in the
Printers unit and the other in Winspool.

uses Printers, Winspool;

procedure TForm1.ChangePrinter;
var
  ADevice, ADriver, APort: array[0..100] of Char;
  hDeviceMode, hPrinter: THandle;
  PrinterInfo2: PPrinterInfo2;
  Size: DWORD;
begin
  ZeroMemory(@ADevice, SizeOf(ADevice));
  // Get the device name of the current printer
  Printer.GetPrinter(ADevice, ADriver, APort, hDeviceMode);
  // Initialise some memory for the TPrinterInfo2 structure
  Size := 0;
  GetMem(PrinterInfo2, Size);
  try
   // Get a handle to the current printer
   if not OpenPrinter(ADevice, hPrinter, nil) then
   begin
     ShowMessage('Error getting handle to current printer.' + #13 +
       SysErrorMessage(GetLastError));
     Exit;
   end;
    // Get size of printer info structure
    Winspool.GetPrinter(hPrinter, 2, PrinterInfo2, Size, @Size);
    // Allocate memory for printer info structure
    ReAllocMem(PrinterInfo2, Size);
    // Get printer info for current printer
    if not Winspool.GetPrinter(hPrinter, 2, PrinterInfo2, Size, @Size) then
    begin
      ShowMessage('Error getting current printer info.' + #13 +
        SysErrorMessage(GetLastError));
      Exit;
    end;
    // Modify the printer info to include the default attribute
    with PrinterInfo2^ do
      Attributes := Attributes + PRINTER_ATTRIBUTE_DEFAULT;
    // Apply the modified printer info to the current printer
    if not Winspool.SetPrinter(hPrinter, 2, PrinterInfo2 , 0) then
    begin
      ShowMessage('Error setting default printer.' + #13 +
        SysErrorMessage(GetLastError));
     Exit;
    end;
  finally
    FreeMem(PrinterInfo2);
    if hPrinter <> 0 then ClosePrinter(hPrinter);
  end
end;

--
Stephen Brown

Re:Changing DEFAULT PRINTER


there is a proc in delphi:  printersetup
you can use it.
On Sun, 13 Feb 2000 07:37:45 -0300, "Curumim Tech"
Quote
<mmiy...@zaz.com.br> wrote:
>hi all,

>Does anyone know how to change de default printer through delphi
>Application??

>thanx for any help....

>Curumim Tech

Other Threads