|
Trouver une ressource
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
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.
Sources du même auteur
Sources de la même categorie
Sources en rapport avec celle ci
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
|
Téléchargements
Logiciels à télécharger sur le même thème :
Comparez les prix Nouvelle version
|