Lancer les options d'impression d'une imprimante

Contenu du snippet

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 / Exemple :


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

A voir également

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.