Re: Print Properties

From: Peter Below (TeamB) (100113.1101_at_compuXXserve.com)
Date: 01/23/04


Date: Fri, 23 Jan 2004 19:43:08 +0100

In article <40104a0b@newsgroups.borland.com>, Camiel Wijffels wrote:
> The conjunction of GetPrinter and SetPrinter works just fine. Except for one
> point - the printerproperties gets lost.
>
> ie, when I have my app and select printer properties with (for example) the
> default printer, I can change the settings. These will indeed be used while
> printing. However when I select another printer in my listbox (applying once
> the Get/Set pair) and I go back to my default printer in my listbox
> (applying the Get/Set pair once more), the settings just made are lost. Only
> the printer default remains.
>
> How to solve?

The easiest way may be to make the changed settings the default, see below.
The alternative would be to save the complete device context for each printer
(beware: they will have different sizes for each printer, and the size is
*not* Sizeof(TDeviceMode)!), so you can restore those settings instead of
using ResetPrinter to restore the defaults. It may be enough, though, to save
only the public part of the device mode record (the part represented by a
TDeviceMode).

Uses WinSpool;

Procedure MakePrintersettingsPermanent;
var
  hPrinter: THandle;
  Device : array[0..255] of char;
  Driver : array[0..255] of char;
  Port : array[0..255] of char;
  hDeviceMode: THandle;
  pDevMode: PDeviceMode;
  bytesNeeded: Cardinal;
  pPI: PPrinterInfo2;
  Defaults: TPrinterDefaults;
  retval: BOOL;
begin
  Assert( Printer.PrinterIndex >= 0 );
  Printer.GetPrinter(Device, Driver, Port, hDeviceMode);
  FillChar( Defaults, Sizeof(Defaults), 0 );
  Defaults.DesiredAccess:=
    PRINTER_ACCESS_ADMINISTER or PRINTER_ACCESS_USE;
  if not WinSpool.OpenPrinter(@Device, hPrinter, @Defaults ) then
        RaiseLastWin32Error;
  try
    retval := WinSpool.GetPrinter(
      hPrinter,
      2,
      Nil, 0, @bytesNeeded );
    GetMem( pPI, bytesNeeded );
    try
      retval := WinSpool.GetPrinter(
        hPrinter, 2,
        pPI, bytesNeeded, @bytesNeeded );
      If not retval Then
        RaiseLastWin32Error;

      pDevMode := GlobalLock( hDeviceMode );
      Assert( Assigned( pdevmode ));
      try
        Move( pdevmode^, pPI^.pDevMode^, Sizeof( pdevmode^ ));
      finally
        GlobalUnlock( hDevicemode );
      end;
      If not WinSpool.SetPrinter(
        hPrinter, 2,
        pPI,
        0 )
      Then
        RaiseLastWin32error;
    finally
      FreeMem( pPI );
    end;

  finally
    WinSpool.ClosePrinter( hPrinter );
  end;
end;

--
Peter Below (TeamB)  
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be


Relevant Pages

  • Re: Persistent Storage
    ... are not lost ... To persist the settings you should have a permanent registry using ... (l'indirizzo di reply di questo messaggio non è ...
    (microsoft.public.windowsce.platbuilder)
  • Re: Trying to install XP
    ... WinXP disk you have will not want to install, unless you do it from scratch, ... The solution is to save all of the files and settings you need to keep ... All records of UserNames/passwords will be lost. ... Does this just mean that I will have to download SP2 again or will I ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: Please Help Lost Files! URGENT
    ... I have Lost all my pictures ... I have 2 drives a c and a d ... > can go to Performance Settings User Profiles Settings, ... I went to User Profiles, ...
    (microsoft.public.windowsxp.perform_maintain)
  • Re: Trying to install XP
    ... Basically, since you have SP2 already, any ... WinXP disk you have will not want to install, unless you do it from scratch, ... The solution is to save all of the files and settings you need to keep ... All records of UserNames/passwords will be lost. ...
    (microsoft.public.windowsxp.help_and_support)
  • Change Printer settings
    ... Acrobat profitional 8 the printer has a settings such as (view pdf ... result, pdf output folder) I need to change them from my program, I ... HPrinter: THandle; ... pDatatype:= nil; ...
    (borland.public.delphi.language.objectpascal)