0/5 (5 avis)
Snippet vu 19 382 fois - Téléchargée 21 fois
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); }
16 sept. 2010 à 11:48
Je ne sais pas si quelqu'un y répondra mais je tente le coup.
J'ai voulu utiliser ce code malheureusement après avoir modifier les options, lorsque je les lance via les imprimantes les changements n'ont pas été prix en compte.
Quelqu'un saurait il pourquoi?
14 juin 2007 à 14:16
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
14 juin 2007 à 13:55
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
14 juin 2007 à 13:08
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....
13 avril 2007 à 10:08
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.