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
UNE JOLIE-HORLOGE ET PAS QU'UN PEU !UNE JOLIE-HORLOGE ET PAS QU'UN PEU ! par neodante
Pour les possesseurs d'iPhone, ça y est Bijin Tokei - qui se traduit littéralement en Français par " Jolie Horloge " - est arrivé et GRATUITEMENT s'il vous plaît ! Après la version Tokyo, Hokkaido, night club, racing, Gal, "pour les mademoiselles'", . voi...
Cliquez pour lire la suite de l'article par neodante TECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICESTECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICES par ROMELARD Fabrice
Animé par: Gaetan Bouveret et Julien Chomarat Business Connectivity Services (BCS) est dans SharePoint 2010 la version 2 de Business Data Catalog (BDC dans SharePoint 2007). Il s'agit de la solution permettant de visualiser des données provenan...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice [DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE[DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE par orion
Comme de nombreux geek, je suis un grand amateur de série TV et je rate régulièrement des épisodes de mes séries préférés. Une solution s'offre à vous avec ce merveilleux site : Tv Gorge - www.tvgorge.com Moteur de recherche à l'appui, vous pouvez ...
Cliquez pour lire la suite de l'article par orion TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010 par ROMELARD Fabrice
Animé par: Vincent Bellet et Baptiste Giraudier La BI dans SharePoint 2010, Les nouveaux services d'application dans SP2010 et SQL Server Reporting services 2008 R2. La BI dans SharePoint est généralisée pour tous afin de permettre à tous les coll...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
DB-MAIN (9.1.0)DB-MAIN (9.1.0)DB-MAIN is a data-modeling and data-architecture tool. It is designed to help developers and anal... Cliquez pour télécharger DB-MAIN Xilisoft DPG Convertisseur (5.1.37.0120)XILISOFT DPG CONVERTISSEUR (5.1.37.0120)Xilisoft DPG Convertisseur offre aux fans de Nintendo DS une bonne solution leur permettant de dé... Cliquez pour télécharger Xilisoft DPG Convertisseur GraphicsGale (2.01.01)GRAPHICSGALE (2.01.01)GraphicsGale est un logiciel de PixelArt avec de nombreuse fonctionnalités permettant de réalisé ... Cliquez pour télécharger GraphicsGale Architecte 3D (Platinum 2010)ARCHITECTE 3D (PLATINUM 2010)Architecte 3D Platinium vous permet de concevoir facilement les plans votre future maison, de l'é... Cliquez pour télécharger Architecte 3D TeamViewer 5 (TeamViewer 5)TEAMVIEWER 5 (TEAMVIEWER 5)Dépanner un ami,expliquer une manipulation devient un jeu d'enfant.
Prise en main d'un autre ord... Cliquez pour télécharger TeamViewer 5
|