zoukankan      html  css  js  c++  java
  • 打开打印机属性窗口

     1  public partial class Form1 : Form
     2     {
     3         private PrintDocument printDocument = null;
     4         private PrinterSettings printSettings = null;
     5 
     6         public Form1()
     7         {
     8             InitializeComponent();
     9 
    10             printDocument = new PrintDocument();
    11             printSettings = printDocument.PrinterSettings;
    12             printSettings.PrinterName = "ZEBRA R110Xi4 300DPI";
    13         }
    14 
    15        
    16         private void btnProperties_Click(object sender, EventArgs e)
    17         {
    18             OpenPrinterPropertiesDialog(printSettings);
    19         }
    20 
    21         [DllImport("winspool.Drv", EntryPoint = "DocumentPropertiesW", SetLastError = true,
    22         ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    23         private static extern int DocumentProperties(IntPtr hwnd, IntPtr hPrinter,
    24         [MarshalAs(UnmanagedType.LPWStr)] string pDeviceNameg,
    25         IntPtr pDevModeOutput, IntPtr pDevModeInput, int fMode);
    26 
    27         [DllImport("kernel32.dll", ExactSpelling = true)]
    28         public static extern IntPtr GlobalFree(IntPtr handle);
    29 
    30         [DllImport("kernel32.dll", ExactSpelling = true)]
    31         public static extern IntPtr GlobalLock(IntPtr handle);
    32 
    33         [DllImport("kernel32.dll", ExactSpelling = true)]
    34         public static extern IntPtr GlobalUnlock(IntPtr handle);
    35 
    36         private void OpenPrinterPropertiesDialog(PrinterSettings printerSettings)//Shows the printer settings dialogue that comes with the printer driver
    37         {            
    38             IntPtr hDevMode = IntPtr.Zero;
    39             IntPtr devModeData = IntPtr.Zero;
    40             IntPtr hPrinter = IntPtr.Zero;
    41             String pName = printerSettings.PrinterName;
    42             try
    43             {
    44                 //创建与打印机设置相对应的 DEVMODE 结构的句柄。 
    45                 hDevMode = printerSettings.GetHdevmode(printerSettings.DefaultPageSettings);
    46                 //锁定内存对象并返回一个指针 
    47                 IntPtr pDevMode = GlobalLock(hDevMode);
    48                 ////get needed size 
    49                 int sizeNeeded = DocumentProperties(this.Handle, IntPtr.Zero, pName, IntPtr.Zero, IntPtr.Zero, 0); 
    50 
    51                 if (sizeNeeded < 0)
    52                 {
    53                    throw new Exception();
    54                 }
    55 
    56                 //allocate memory
    57                 devModeData = Marshal.AllocHGlobal(sizeNeeded);
    58                 //show the native dialog 
    59                 int returncode = DocumentProperties(this.Handle, IntPtr.Zero, pName, devModeData, pDevMode, 14);
    60 
    61                 //Failure to display native dialog
    62                 if (returncode < 0) 
    63                 {
    64                     throw new Exception();
    65                 }
    66 
    67                 //unlocks the memory
    68                 GlobalUnlock(hDevMode);
    69 
    70                 if (devModeData != IntPtr.Zero)
    71                 {
    72                     printerSettings.SetHdevmode(devModeData);
    73                     //printerSettings.DefaultPageSettings.SetHdevmode(devModeData);
    74                 }
    75             }
    76             catch (Exception ex)
    77             {
    78                 throw;
    79             }
    80             finally
    81             {
    82                 if (hDevMode != IntPtr.Zero)
    83                 {
    84                     Marshal.FreeHGlobal(hDevMode);
    85                     hDevMode = IntPtr.Zero;
    86                 }
    87                 if (devModeData != IntPtr.Zero)
    88                 {
    89                     Marshal.FreeHGlobal(devModeData);
    90                     devModeData = IntPtr.Zero;
    91                 }
    92             }
    93         }
    94     }
  • 相关阅读:
    sql,linq基础再一次学习
    position与aop
    java基础常用类!
    JNI初步!
    java基础动态代理!
    java基础面向对象!
    php初步!
    java基础泛型!
    java基础对象多态性!
    java基础io流!
  • 原文地址:https://www.cnblogs.com/JustYong/p/4444510.html
Copyright © 2011-2022 走看看