Call API to pop up printer properties dialog box

Call api to pop up the printer properties dialog box
Author:vitoriatang
From:Internet
.NET Framework encapsulates a lot of dialog boxes about printing, such as PrintDialog, PageSetupDialog.
But sometimes we also need to care about the printer properties dialog box, then we can call the API to solve this problem. There are several API functions related to
PrinterProperties
DocumentProperties
OpenPrinter
ClosePrinter
introduced one by one

printerproperties
displays the printer properties dialog box.

documentproperties
displays the printer configuration dialog box.

openprinter
opens the printer

closeprinter
close the printer

When calling printerproperties or documentproperties, you need to call openprinter first, and call closeprinter after the end.

As for the difference between printer properties and printer configuration, I understand for myself. More detailed information can be found in msdn

sample codes:
1. 声明API函数
       [System.Runtime.InteropServices.DllImportAttribute("winspool.drv", SetLastError = true)]
       public extern static int DocumentProperties(
            IntPtr hWnd,              // handle to parent window
            IntPtr hPrinter,           // handle to printer object
            string pDeviceName,   // device name
            ref IntPtr pDevModeOutput, // modified device mode
            ref IntPtr pDevModeInput,   // original device mode
            int fMode);                 // mode options

        [System.Runtime.InteropServices.DllImportAttribute("winspool.drv")]
        public static extern int PrinterProperties(
            IntPtr hwnd,  // handle to parent window
            IntPtr hPrinter); // handle to printer object

         [System.Runtime.InteropServices.DllImportAttribute("winspool.drv", SetLastError = true)]
        public extern static int OpenPrinter(
            string pPrinterName,   // printer name
            ref IntPtr hPrinter,      // handle to printer object
            ref IntPtr pDefault);    // handle to default printer object.

        [System.Runtime.InteropServices.DllImportAttribute("winspool.drv", SetLastError = true)]
        public static extern int ClosePrinter(
            IntPtr phPrinter); // handle to printer object


2.调用DocumentProperties
private void documentPropButton_Click(object sender, EventArgs e)
        {
            string printerName = _document.PrinterSettings.PrinterName;

            if (printerName != null && printerName.Length > 0)
            {
                IntPtr pPrinter = IntPtr.Zero;
                IntPtr pDevModeOutput = IntPtr.Zero;
                IntPtr pDevModeInput = IntPtr.Zero;
                IntPtr nullPointer = IntPtr.Zero;

                 OpenPrinter(printerName, ref pPrinter, ref nullPointer);

                int iNeeded = DocumentProperties(this.Handle, pPrinter, printerName, ref pDevModeOutput, ref pDevModeInput, 0);
                pDevModeOutput = System.Runtime.InteropServices.Marshal.AllocHGlobal(iNeeded);
                DocumentProperties(this.Handle, pPrinter, printerName, ref pDevModeOutput, ref pDevModeInput, DM_PROMPT);
                ClosePrinter(pPrinter);
            }
        }

3. 调用PrinterProperties
private void printPropButton_Click(object sender, EventArgs e)
        {
            string printerName = _document.PrinterSettings.PrinterName;

            if (printerName != null && printerName.Length > 0)
            {
                IntPtr pPrinter = IntPtr.Zero;
                IntPtr pDevModeOutput = IntPtr.Zero;
                IntPtr pDevModeInput = IntPtr.Zero;
                IntPtr nullPointer = IntPtr.Zero;

                 OpenPrinter(printerName, ref pPrinter, ref nullPointer);

                int iNeeded = PrinterProperties (this.Handle, pPrinter);
                ClosePrinter (pPrinter);
            }

 

 

Guess you like

Origin blog.csdn.net/jinzhengquanqq/article/details/5878329