打开打印机属性窗口

 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     }

 

你可能感兴趣的:(属性)