天天看點

列印系統開發(46)——API(4)——打開列印機屬性視窗

public partial class Form1 : Form
    {
        private PrintDocument printDocument = null;
        private PrinterSettings printSettings = null;

        public Form1()
        {
            InitializeComponent();

            printDocument = new PrintDocument();
            printSettings = printDocument.PrinterSettings;
            printSettings.PrinterName = "ZEBRA R110Xi4 300DPI";
        }

       
        private void btnProperties_Click(object sender, EventArgs e)
        {
            OpenPrinterPropertiesDialog(printSettings);
        }

        [DllImport("winspool.Drv", EntryPoint = "DocumentPropertiesW", SetLastError = true,
        ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        private static extern int DocumentProperties(IntPtr hwnd, IntPtr hPrinter,
        [MarshalAs(UnmanagedType.LPWStr)] string pDeviceNameg,
        IntPtr pDevModeOutput, IntPtr pDevModeInput, int fMode);

        [DllImport("kernel32.dll", ExactSpelling = true)]
        public static extern IntPtr GlobalFree(IntPtr handle);

        [DllImport("kernel32.dll", ExactSpelling = true)]
        public static extern IntPtr GlobalLock(IntPtr handle);

        [DllImport("kernel32.dll", ExactSpelling = true)]
        public static extern IntPtr GlobalUnlock(IntPtr handle);

        private void OpenPrinterPropertiesDialog(PrinterSettings printerSettings)//Shows the printer settings dialogue that comes with the printer driver
        {            
            IntPtr hDevMode = IntPtr.Zero;
            IntPtr devModeData = IntPtr.Zero;
            IntPtr hPrinter = IntPtr.Zero;
            String pName = printerSettings.PrinterName;
            try
            {
                //建立與列印機設定相對應的 DEVMODE 結構的句柄。 
                hDevMode = printerSettings.GetHdevmode(printerSettings.DefaultPageSettings);
                //鎖定記憶體對象并傳回一個指針 
                IntPtr pDevMode = GlobalLock(hDevMode);
                get needed size 
                int sizeNeeded = DocumentProperties(this.Handle, IntPtr.Zero, pName, IntPtr.Zero, IntPtr.Zero, 0); 

                if (sizeNeeded < 0)
                {
                   throw new Exception();
                }

                //allocate memory
                devModeData = Marshal.AllocHGlobal(sizeNeeded);
                //show the native dialog 
                int returncode = DocumentProperties(this.Handle, IntPtr.Zero, pName, devModeData, pDevMode, 14);

                //Failure to display native dialog
                if (returncode < 0) 
                {
                    throw new Exception();
                }

                //unlocks the memory
                GlobalUnlock(hDevMode);

                if (devModeData != IntPtr.Zero)
                {
                    printerSettings.SetHdevmode(devModeData);
                    //printerSettings.DefaultPageSettings.SetHdevmode(devModeData);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                if (hDevMode != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(hDevMode);
                    hDevMode = IntPtr.Zero;
                }
                if (devModeData != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(devModeData);
                    devModeData = IntPtr.Zero;
                }
            }
        }
    }
           

繼續閱讀