Accueil > > > LANCER LES OPTIONS D'IMPRESSION D'UNE IMPRIMANTE
LANCER LES OPTIONS D'IMPRESSION D'UNE IMPRIMANTE
Information sur la source
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-IMPRIMAN TE_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/winsp ool.DocumentProperties
Le même code existe sur CodeProject et est plus complet que le miens (trouvé par Bidou) : http://www.codeproject.com/useritems/PrinterProp ertiesWindow.asp
Historique
- 13 avril 2007 09:53:10 :
- Commentaires
- 13 avril 2007 10:10:57 :
- Modification avec une présentation plus complète.
Sources du même auteur
Sources de la même categorie
Commentaires et avis
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
|
Derniers Blogs
TECHDAYS PARIS 2012 : COMMENT SHAREPOINT A SAUVé MES TECHDAYSTECHDAYS PARIS 2012 : COMMENT SHAREPOINT A SAUVé MES TECHDAYS par ROMELARD Fabrice
Speakers : Lionel Limozin et Alain Marty La session commence par une découverte de SharePoint à travers la mise en place d'un environnement SharePoint pour la gestion des Sessions animées par BeWise. Le besoin est très ba...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice PERSPECTIVE 3.0 POUR SILVERLIGHT 5.0PERSPECTIVE 3.0 POUR SILVERLIGHT 5.0 par odewit
Je viens de publier la version 3.0 de Perspective pour Silverlight, qui regroupe un portage sous Silverlight 5.0 des fonctionnalités de Perspective 2.0, le framework 3D de haut-niveau introduit récemment et de nouveaux exemples de code. En voici la li...
Cliquez pour lire la suite de l'article par odewit TECHDAYS PARIS 2012 : TOP 10 DES BEST PRACTICES POUR SQL SERVERTECHDAYS PARIS 2012 : TOP 10 DES BEST PRACTICES POUR SQL SERVER par ROMELARD Fabrice
Speaker : Nadia Ben El Kadi Configuration machine La session commence par la toute première question à se poser lors de la mise en place d'environnement SQL Server, la configuration des machines : Type de mac...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : KINECT + OFFICE 365 UN BON GESTE POUR VOTRE SITECHDAYS PARIS 2012 : KINECT + OFFICE 365 UN BON GESTE POUR VOTRE SI par ROMELARD Fabrice
Speakers : Fabrice Barbin, Samuel Blanchard, Julien Lo Presti Titre Prometteur et attractif invitant à voir comment lier le composant ludique Kinect dans le cadre d'une structure IT classique, notamment au travers de la plat...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : PLEINIèRE DU PREMIER JOURTECHDAYS PARIS 2012 : PLEINIèRE DU PREMIER JOUR par ROMELARD Fabrice
KeyNotes du premier jour pour les développeurs. La session est principalement axée sur une des principales directions prise par Microsoft à travers tous ses nouveaux produits : Cloud privé ou public (Solution Azure) ...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|