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     }
  • 相关阅读:
    移动文件夹时,千万别直接移动系统文件夹,应该直接复制或者移动这个系统文件夹内的内容
    UIView的intrinsicContentSize方法,在按钮中重写
    Reveal v4(8796) 使用
    AppCode 2016.3 注册码
    BSD process name correspondlng to current thread: knernel_task Mac OS version Not yet set
    iOS 适配HTTPS
    [Graphics] UIColor created with component values far outside the expected range, Set a breakpoint on UIColorBreakForOutOfRangeColorComponents to debug. This message will only be logged once.
    SB中设置UITextField 无边框,真机上输入汉字聚焦时,文字 下沉
    TTTAttributedLabel 富文本小记
    iOS 应用数据存储的常用方式
  • 原文地址:https://www.cnblogs.com/JustYong/p/4444510.html
Copyright © 2011-2022 走看看