打印系统开发(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;
                }
            }
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值