Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum. Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

LANCER LES OPTIONS D'IMPRESSION D'UNE IMPRIMANTE


Information sur la source

Catégorie :API Source .NET ( DotNet ) Classé sous : options, imprimante, configurer, openprinterpropertiesdialog Niveau : Initié Date de création : 12/04/2007 Date de mise à jour : 13/04/2007 10:10:57 Vu : 7 404

Note :
Aucune note

Commentaire sur cette source (4)
Ajouter un commentaire et/ou une note

Description

Ce code permet de lancer la fenêtre de configuration propriétaire (HP, Canon, ...) de l'imprimante.
Cette fenêtre est habituellement obtenue en cliquant sur le bouton "Options" d'un object PrintDialog.
Ce code évite de passer par un PrintDialog pour configurer l'imprimante sélectionnée.
 

Source

  • private const int DM_IN_BUFFER = 8;
  • private const int DM_OUT_BUFFER = 2;
  • private const int DM_IN_PROMPT = 4;
  • /// <summary>
  • /// The DocumentProperties functions display pages for printer properties.
  • /// </summary>
  • /// <param name="hwnd">handle to parent window</param>
  • /// <param name="hPrinter">handle to printer object</param>
  • /// <param name="pDeviceName">device name</param>
  • /// <param name="pDevModeOutput">modified device mode</param>
  • /// <param name="pDevModeInput">original device mode</param>
  • /// <param name="fMode">mode options</param>
  • /// <returns></returns>
  • [DllImport("winspool.Drv", EntryPoint = "DocumentPropertiesW", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  • static extern int DocumentProperties(IntPtr hwnd, IntPtr hPrinter, [MarshalAs(UnmanagedType.LPWStr)] string pDeviceName, IntPtr pDevModeOutput, IntPtr pDevModeInput, int fMode);
  • /// <summary>
  • /// Locks a global memory object and returns a pointer to the first byte of the object's memory block
  • /// </summary>
  • /// <param name="hMem">A handle to the global memory object</param>
  • /// <returns>Value is the size of the buffer required to contain the printer driver initialization data</returns>
  • [DllImport("kernel32.dll")]
  • static extern IntPtr GlobalLock(IntPtr hMem);
  • /// <summary>
  • /// Decrements the lock count associated with a memory object
  • /// </summary>
  • /// <param name="hMem">A handle to the global memory object</param>
  • /// <returns></returns>
  • [DllImport("kernel32.dll")]
  • static extern bool GlobalUnlock(IntPtr hMem);
  • /// <summary>
  • /// Frees the specified global memory object and invalidates its handle
  • /// </summary>
  • /// <param name="hMem">A handle to the global memory object</param>
  • /// <returns></returns>
  • [DllImport("kernel32.dll")]
  • static extern IntPtr GlobalFree(IntPtr hMem);
  • /// <summary>
  • /// Open the Properties dialog of a pinter
  • /// </summary>
  • private void OpenPrinterPropertiesDialog()
  • {
  • PrinterSettings s = new PrinterSettings(); //PrintSettings peut aussi être obtenue par la propriété du même nom de l'objet PrintDialog
  • s.PrinterName = @"\\******\****"; //Nom de l'imprimante
  • OpenPrinterPropertiesDialog(s);
  • }
  • /// <summary>
  • /// Open the Properties dialog of the selected pinter
  • /// </summary>
  • /// <param name="printerSettings">Specifies information about how a document is printed, including the printer that prints it</param>
  • private void OpenPrinterPropertiesDialog(PrinterSettings printerSettings)
  • {
  • //Creates a handle to a DEVMODE structure that corresponds to the printer settings
  • IntPtr hDevMode = printerSettings.GetHdevmode(printerSettings.DefaultPageSettings);
  • //Locks the DEVMODE Handle
  • IntPtr pDevMode = GlobalLock(hDevMode);
  • //Retrieve size of the buffer for the new DEVMODE
  • int sizeNeeded = DocumentProperties(this.Handle, IntPtr.Zero, printerSettings.PrinterName, pDevMode, pDevMode, 0);
  • IntPtr devModeData = Marshal.AllocHGlobal(sizeNeeded);
  • //Lauching the printer property dialog
  • DocumentProperties(this.Handle, IntPtr.Zero, printerSettings.PrinterName, devModeData, pDevMode, DM_IN_BUFFER | DM_IN_PROMPT | DM_OUT_BUFFER);
  • //Unlock the DEVMODE
  • GlobalUnlock(hDevMode);
  • //Sets the new DEVMODE to the PrinterSettings object
  • printerSettings.SetHdevmode(devModeData);
  • printerSettings.DefaultPageSettings.SetHdevmode(devModeData);
  • //Free the DEVMODEs
  • GlobalFree(hDevMode);
  • Marshal.FreeHGlobal(devModeData);
  • }
private const int DM_IN_BUFFER = 8;
private const int DM_OUT_BUFFER = 2;
private const int DM_IN_PROMPT = 4;

/// <summary>
/// The DocumentProperties functions display pages for printer properties.
/// </summary>
/// <param name="hwnd">handle to parent window</param>
/// <param name="hPrinter">handle to printer object</param>
/// <param name="pDeviceName">device name</param>
/// <param name="pDevModeOutput">modified device mode</param>
/// <param name="pDevModeInput">original device mode</param>
/// <param name="fMode">mode options</param>
/// <returns></returns>
[DllImport("winspool.Drv", EntryPoint = "DocumentPropertiesW", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
static extern int DocumentProperties(IntPtr hwnd, IntPtr hPrinter, [MarshalAs(UnmanagedType.LPWStr)] string pDeviceName, IntPtr pDevModeOutput, IntPtr pDevModeInput, int fMode);

/// <summary>
/// Locks a global memory object and returns a pointer to the first byte of the object's memory block
/// </summary>
/// <param name="hMem">A handle to the global memory object</param>
/// <returns>Value is the size of the buffer required to contain the printer driver initialization data</returns>
[DllImport("kernel32.dll")]
static extern IntPtr GlobalLock(IntPtr hMem);

/// <summary>
/// Decrements the lock count associated with a memory object
/// </summary>
/// <param name="hMem">A handle to the global memory object</param>
/// <returns></returns>
[DllImport("kernel32.dll")]
static extern bool GlobalUnlock(IntPtr hMem);

/// <summary>
/// Frees the specified global memory object and invalidates its handle
/// </summary>
/// <param name="hMem">A handle to the global memory object</param>
/// <returns></returns>
[DllImport("kernel32.dll")]
static extern IntPtr GlobalFree(IntPtr hMem);


/// <summary>
/// Open the Properties dialog of a pinter
/// </summary>
private void OpenPrinterPropertiesDialog()
{
    PrinterSettings s = new PrinterSettings(); //PrintSettings peut aussi être obtenue par la propriété du même nom de l'objet PrintDialog 
    s.PrinterName = @"\\******\****"; //Nom de l'imprimante
    OpenPrinterPropertiesDialog(s);
}

/// <summary>
/// Open the Properties dialog of the selected pinter
/// </summary>
/// <param name="printerSettings">Specifies information about how a document is printed, including the printer that prints it</param>
private void OpenPrinterPropertiesDialog(PrinterSettings printerSettings)
{
    //Creates a handle to a DEVMODE structure that corresponds to the printer settings
    IntPtr hDevMode = printerSettings.GetHdevmode(printerSettings.DefaultPageSettings);
    
    //Locks the DEVMODE Handle
    IntPtr pDevMode = GlobalLock(hDevMode);

    //Retrieve size of the buffer for the new DEVMODE
    int sizeNeeded = DocumentProperties(this.Handle, IntPtr.Zero, printerSettings.PrinterName, pDevMode, pDevMode, 0);
    IntPtr devModeData = Marshal.AllocHGlobal(sizeNeeded);
    
    //Lauching the printer property dialog
    DocumentProperties(this.Handle, IntPtr.Zero, printerSettings.PrinterName, devModeData, pDevMode, DM_IN_BUFFER | DM_IN_PROMPT | DM_OUT_BUFFER);
    
    //Unlock the DEVMODE
    GlobalUnlock(hDevMode);

    //Sets the new DEVMODE to the PrinterSettings object
    printerSettings.SetHdevmode(devModeData);
    printerSettings.DefaultPageSettings.SetHdevmode(devModeData);

    //Free the DEVMODEs
    GlobalFree(hDevMode);
    Marshal.FreeHGlobal(devModeData);
}

Conclusion

Réponse à la question du forum :
http://www.csharpfr.com/infomsg_GESTION-IMPRIMANTE_920111.aspx

Ce code a été fait à partir du site pinvoke qui ressence toutes les déclaration c# pour les api windows.
http://www.pinvoke.net/default.aspx/winspool.DocumentProperties

Le même code existe sur CodeProject et est plus complet que le miens (trouvé par Bidou) :
http://www.codeproject.com/useritems/PrinterPropertiesWindow.asp
 

Historique

13 avril 2007 09:53:10 :
Commentaires
13 avril 2007 10:10:57 :
Modification avec une présentation plus complète.

Commentaires et avis

signaler à un administrateur
Commentaire de Bidou le 13/04/2007 10:08:35 administrateur CS

Bien mieux avec les commentaires, merci!

signaler à un administrateur
Commentaire de Wiyem le 14/06/2007 13:08:35

salut,,,
j'ai une imprimante Hp.... je veux tester l'etat de l'imprimante avant de lancer l'impression de ma feuille de crystalReport (je veux teste: si l'imprimante contient du papiers, si elle contient encore d'encre, si elle est en marche ou bien prête, etc, avant de l'ancer l'impresion physique)....
et je voudrais bien qu'un message se declanche si un de ces problèmes existe....

est ce que votre code repond à mon besoin?
et ce koi le this.Handle?
et e c ke DllImport  vient de la bibliothéque using System.Runtime.InteropServices;?

si tu peut me donner plus de détail sur la methode dont je peut appéler cette class: je serai vraiment contente....
merci pour votre aide
à bientôt....

signaler à un administrateur
Commentaire de zebobo5 le 14/06/2007 13:55:38

Salut,
Mon code permet juste d'affichier la fenêtre, HP dans ton cas, de configuration de l'imprimante.
Pour afficher la fenêre, il te suffit juste de lancer la méthode : OpenPrinterPropertiesDialog()

Si tu veux verifier tous ces paramètre, tu ne pourras pas, sauf en utilisant les Dlls HP.
Je ne pense pas que HP publie les Headers c++ de ses Dlls, ou des Interop. Verifie si des objets COM ne seraient pas disponible, sinon tu ne pourras pas. S'il existe tout ceci, ton programme ne marchera donc qu'avec une imprimante de même modèle et ayant la même version de drivers.

this.Handle :
- this est ton Formulaire
- Handle est comme partout un pointeur d'instance

DllImport :
vient bien de System.Runtime.InteropServices

signaler à un administrateur
Commentaire de Wiyem le 14/06/2007 14:16:12

salut,
merci bien pour votre aide....
je vais essayer de chercher une solution....
Je vous envoie la solution lorsque j’atteins mon but...
merci encore une fois...
à bientôt

Ajouter un commentaire

Discussions en rapport avec ce code source dans le forum

Propriétés de l'imprimante en c# [ par Schad ] J'aimerais pouvoir accéder directement aux propriétés de l'imprimante courante sans passer par PrintDialog.Toute suggestion serait la bienvenue.Schad Propriétés de l'imprimante courante en C# [ par Schad ] J'aimerais accéder aux propriétés de l'imprimante courante sans passer par la fenêtre intermédiaire de PrintDialog.Toute suggestion sera la bienvenue. Imprimante / Configuration de la page [ par zouzounet ] Bonjour,J'ai un petit soucis avec une impression dans un prog.J'utilise du GDI+ pour tracer un tableau, que je rempli de texte (toujours avec GDI+) et compilation [ par clebard ] Bonjour à tous suis tout nouveau...j'ai dans les mains un bouquin qui dit : "le C# en 21 jours"...doit y'avoir erreur!M'enfin, j'ai donc réussi à crée options de compil [ par clebard ] Depuis que j'ai installé le framework, je n'ai jamais pu compiler depuis la console DOS...le CSC.exe n'est pas connu et pourtant il est bien sur le HD [Concours] ???? Imprimante [ par Karlo ] Salut a tous,hier soir j'attaqué la partie impression de l'image et la j'ai eu un gros probleme, n'ayant pas d'imprimante chez moi (rigolé pas ca arri Changer d'imprimante par défaut [ par psdo ] Salut,je cherche un moyen de changer par l'intermédiaire du code, l'imprimante par défaut... j'ai rien trouvé sur ce sujet pour le C#.Merci de votre a Imprimer un document PDF en choisissant l'imprimante [ par psdo ] Salut,comment peut-on à partir du code, sélectionner l'imprimante avant de lancer l'impression d'un document PDF.Faut-il changer l'imprimante par défa Execution et gestion de process distant [ par ceoph ] Bonjour,J'aimerais (en winform ou webform) pouvoir lancer un process (executable) avec des options sur une machine distante et la meme chose avec d'au Options de comparaison de DataTable.Select() [ par ppao ] salut,ben le titre parle de lui meme, je voudrais savoir si on peut mettre des critères plus fin que "=" ou "!=" dans la chaine passé à Select().merci


Nos sponsors

Sondage...

CalendriCode

Décembre 2008
LMMJVSD
1234567
891011121314
15161718192021
22232425262728
293031    

Consulter la suite du CalendriCode

Téléchargements

Logiciels à télécharger sur le même thème :



Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel BAÏSE, Merci à Vincent pour ses précieux conseils
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés
Temps d'éxécution de la page : 0,312 sec

Google Coop CodeS-SourceS Google Coop CodeS-SourceS


Certaines images présentes sur le site (notament certains avatars) sont issues des collections IconShock, donc si vous souhaitez utiliser ces icons vous devez les acheter, ne les copiez pas et ne utilisez pas dans vos sites et applications sans les avoir commandé.