IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

API, COM et SDKs Delphi Discussion :

[Imprimante] Enregistrer tous les paramètres d'une imprimante


Sujet :

API, COM et SDKs Delphi

  1. #1
    Membre éclairé

    Profil pro
    Inscrit en
    Décembre 2002
    Messages
    1 085
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : Belgique

    Informations forums :
    Inscription : Décembre 2002
    Messages : 1 085
    Points : 886
    Points
    886
    Par défaut [Imprimante] Enregistrer tous les paramètres d'une imprimante
    Bonjour à tous,

    Je souhaiterai enregistrer tous les paramètres (fichier ou blob) d'une imprimante qui a été configurée
    par l'utilisateur à l'aide d'un TPrinterSetupDialog.

    J'ai bien entendu essayé les solutions les plus courantes comme par exemple enregistrer
    les données se trouvant dans PDeviceModeW ou via PPrinterInfo2

    Ces deux solutions fonctionnent bien mais n'enregistrent malheureusement que les propriétés
    de base à savoir (Orientation, format papier, bac).

    Si je modifie une valeur (autres que les précédentes) dans le module "Propriété", celles-ci ne sont pas enregistrer sans le record PDeviceModeW ou PPrinterInfo2.
    Par exemple :
    - couleur ou monochrome
    - recto-verso

    Nom : Config.png
Affichages : 543
Taille : 10,5 Ko

    Ma question est la suivante :

    Etant donné que chaque imprimante dispose des ses propres options avancées, fonctionnalités, y-a-t-il un moyen de récupérer toutes ces valeurs et ce peu importe la marque de l'imprimante ?

    Merci d'avance,

  2. #2
    Rédacteur/Modérateur
    Avatar de Andnotor
    Inscrit en
    Septembre 2008
    Messages
    5 685
    Détails du profil
    Informations personnelles :
    Localisation : Autre

    Informations forums :
    Inscription : Septembre 2008
    Messages : 5 685
    Points : 13 102
    Points
    13 102
    Par défaut
    Regarde ici.
    Ca date un peu Peut-être quelques adaptations à apporter pour les Delphi récents.

  3. #3
    Expert éminent sénior
    Avatar de ShaiLeTroll
    Homme Profil pro
    Développeur C++\Delphi
    Inscrit en
    Juillet 2006
    Messages
    13 447
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Développeur C++\Delphi
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2006
    Messages : 13 447
    Points : 24 849
    Points
    24 849
    Par défaut
    Je n'utilise pas TPrinterSetupDialog mais je défini très souvent les paramétrages de l'imprimante par code

    Si j'ai décidé une présentation, je la mémorise dans un fichier config comme par exemple la taille d'un papier particulier
    PageFormat est une objet issu de la DB et\ou Ini
    Voici un code de mes prédécesseurs qui fonctionne assez bien avec des formats de papier tout à fait non standard

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
     
    procedure Txxx.InitPrinter;
    var
      Device : array[0..255] of char;
      Driver : array[0..255] of char;
      Port   : array[0..255] of char;
      hDMode : THandle;
      PDMode : PDEVMODE;
    begin
      Printer.PrinterIndex := Printer.Printers.IndexOf(PageFormat.PrinterName);
     
      Printer.GetPrinter(Device, Driver, Port, hDMode);
      if hDMode <> 0 then
      begin
        pDMode := GlobalLock(hDMode);
        if pDMode <> nil then
        begin
          try
            if PageFormat.PaperSize = DMPAPER_USER then
            begin
              pDMode^.dmFields := pDMode^.dmFields or
                                DM_PAPERSIZE or
                                DM_PAPERWIDTH or
                                DM_PAPERLENGTH;
              pDMode^.dmPaperSize := DMPAPER_USER;
            end
            else
              pDMode^.dmPaperSize := PageFormat.PaperSize;
     
            if PageFormat.PaperWidth <> 0 then
              pDMode^.dmPaperWidth := PageFormat.PaperWidth;
     
            if PageFormat.PaperLength <> 0 then
              pDMode^.dmPaperLength := PageFormat.PaperLength;
     
            if PageFormat.Orientation <> 0 then
              pDMode^.dmOrientation := PageFormat.Orientation;
     
            pDMode^.dmCopies := 1;
     
            {Set the bin to use}
            // pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL;
            // pDMode^.dmDefaultSource := DMBIN_MANUAL;
          finally
            GlobalUnlock(hDMode);
          end;
     
          Printer. (Device, Driver, Port, hDMode);
        end;
      end;
    end;
    Pour le RectoVerso, comme toutes les imprimantes ne le supporte pas, je vérifie cela avant via PrinterSupportsDuplex

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
     
      TPrinterSliteHelper = class helper for TPrinter
      public
        class function SelectPrinter(const Msg: string; var PrinterName: string): Boolean;
        class function PrinterSupportsDuplex(const APrinterName: string): Boolean;
      end;
     
     
    //------------------------------------------------------------------------------
    class function TPrinterSliteHelper.PrinterSupportsDuplex(const APrinterName: string): Boolean;
    var
      idxPrinter, idxPrinterOld: Integer;
      Device, Driver, Port: array[0..255] of Char;
      hDevMode: THandle;
    begin
      Result := False;
      if APrinterName <> '' then
      begin
        idxPrinterOld := Printer.PrinterIndex;
        try
          idxPrinter := Printer.Printers.IndexOf(APrinterName);
          if idxPrinter >= 0 then
          begin
            Printer.PrinterIndex := idxPrinter;
            Printer.GetPrinter(Device, Driver, Port, hDevmode);
     
            // DC_DUPLEX - If the printer supports duplex printing, the return value is 1; otherwise, the return value is zero. The pOutput parameter is not used.
            Result := WinAPI.WinSpool.DeviceCapabilities(Device, Port, DC_DUPLEX, nil, nil) = 1;
          end;
        finally
          Printer.PrinterIndex := idxPrinterOld;
        end;
      end;
    end;
     
    class function TPrinterSliteHelper.SelectPrinter(const Msg: string; var PrinterName: string): Boolean;
    var
      PrinterIndex: Integer;
    begin
      PrinterIndex := Printer.Printers.IndexOf(PrinterName);
      Result := TSLTMessageDlg.InputCombo(Msg, Printer.Printers, PrinterIndex) and (PrinterIndex >= 0);
      if Result then
        PrinterName := Printer.Printers.Strings[PrinterIndex];
    end;
    Aide via F1 - FAQ - Guide du développeur Delphi devant un problème - Pensez-y !
    Attention Troll Méchant !
    "Quand un homme a faim, mieux vaut lui apprendre à pêcher que de lui donner un poisson" Confucius
    Mieux vaut se taire et paraître idiot, Que l'ouvrir et de le confirmer !
    L'ignorance n'excuse pas la médiocrité !

    L'expérience, c'est le nom que chacun donne à ses erreurs. (Oscar Wilde)
    Il faut avoir le courage de se tromper et d'apprendre de ses erreurs

  4. #4
    Membre éclairé

    Profil pro
    Inscrit en
    Décembre 2002
    Messages
    1 085
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : Belgique

    Informations forums :
    Inscription : Décembre 2002
    Messages : 1 085
    Points : 886
    Points
    886
    Par défaut
    Merci à vous tous pour vos réponses.

    Le fait de passer par la structure PDEVMode ne permet pas de récupérer entièrement tous les paramètres de l'imprimante.
    J'ai cependant trouvé une solution (Basé sur un exemple C#) qui fonctionne du feu de dieu

    En gros, il suffit de :
    - récupérer le pointeur PDEVMOD
    - déterminer la taille mémoire de ce pointeur
    - enregistrer ce pointeur dans un stream

    Pour la lecture, il faut juste faire la même chose... à l'inverse bien sûr.
    Attention, il faut s'assurer que la taille du TStream correspond bien à la taille de la zone mémoire du pointeur PDEVMODE... sinon boum.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
     
    procedure TfrxSRPrinterSettings.SaveMemorySettings(PrinterName : string ;DevMode : THandle;  ADestStream: TStream);
    var
      hDevMode : Pointer;
      aSize : Integer;
    begin
      hDevMode := GlobalLock(DevMode);
      try
        aSize := DocumentProperties(0, 0, PChar(PrinterName), 0, hDevMode, 0);
        ADestStream.Write(PChar(hDevMode)[0],aSize);
        ADestStream.Position := 0;
      finally
        GlobalUnlock(DevMode);
      end;
    end;
     
     
    procedure TfrxSRPrinterSettings.LoadMemorySettingsToPrinter( APrinterName : string;ASourceStream: TStream);
    var
      hDevMode : Pointer;
      DevMode : THandle;
      aSize : Integer;
     
     
      lpPrinter, lpDriver, lpPort : PChar;
    begin
     
     
      lpPrinter := StrAlloc(255);
      lpDriver  := StrAlloc(255);
      lpPort    := StrAlloc(255);
     
     
      StrCopy(lpPrinter, PChar(APrinterName));
     
     
      Printer.GetPrinter(lpPrinter, lpDriver, lpPort, DevMode);
      hDevMode := GlobalLock(DevMode);
      try
        aSize := DocumentProperties(0, 0, PChar(APrinterName), 0, hDevMode, 0);
     
     
        if aSize = ASourceStream.Size then
        begin
          ASourceStream.Position := 0;
          ASourceStream.Read(PChar(hDevMode)[0],aSize);
     
     
          Printer.SetPrinter(lpPrinter, lpDriver, lpPort, DevMode);
        end;
     
     
      finally
        GlobalUnlock(DevMode);
      end;
    end;

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 0
    Dernier message: 24/09/2014, 16h05
  2. Réponses: 1
    Dernier message: 01/06/2014, 13h15
  3. Enregistrer tous les requetes sur une base
    Par darknesshappy dans le forum MS SQL Server
    Réponses: 5
    Dernier message: 20/12/2013, 11h21
  4. Réponses: 4
    Dernier message: 30/11/2011, 22h31
  5. Installation d'une imprimante pour tous les utilisateurs
    Par chobol dans le forum Windows XP
    Réponses: 4
    Dernier message: 04/09/2007, 22h28

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo