C#调用API弹出打印机属性对话框
調(diào)用api彈出打印機(jī)屬性對(duì)話框?
Author:vitoriatang
From:Internet
.NET?Framework封裝了很多關(guān)于打印的對(duì)話框,比如說PrintDialog,?PageSetupDialog.?
但是有的時(shí)候我們還需要關(guān)心打印機(jī)屬性對(duì)話框,那么就可以調(diào)用API來解決這個(gè)問題。有幾個(gè)API函數(shù)與之相關(guān)
PrinterProperties
DocumentProperties
OpenPrinter
ClosePrinter
逐一介紹
printerproperties
顯示打印機(jī)屬性對(duì)話框。
documentproperties
顯示打印機(jī)配置對(duì)話框。
openprinter
打開打印機(jī)
closeprinter
關(guān)閉打印機(jī)
在調(diào)用printerproperties或者documentproperties的時(shí)候,都需要先調(diào)用openprinter,并在結(jié)束后調(diào)用closeprinter。
至于打印機(jī)屬性和打印機(jī)配置有什么不同,就自己領(lǐng)會(huì)了。更為詳盡的信息可以查閱msdn
sample?codes:
1.?聲明API函數(shù)
???????[System.Runtime.InteropServices.DllImportAttribute("winspool.drv",?SetLastError?=?true)]
???????public?extern?static?int?DocumentProperties(
????????????IntPtr?hWnd,??????????????//?handle?to?parent?window?
????????????IntPtr?hPrinter,???????????//?handle?to?printer?object
????????????string?pDeviceName,???//?device?name
????????????ref?IntPtr?pDevModeOutput,?//?modified?device?mode
????????????ref?IntPtr?pDevModeInput,???//?original?device?mode
????????????int?fMode);?????????????????//?mode?options?
????????[System.Runtime.InteropServices.DllImportAttribute("winspool.drv")]
????????public?static?extern?int?PrinterProperties(
????????????IntPtr?hwnd,??//?handle?to?parent?window
????????????IntPtr?hPrinter);?//?handle?to?printer?object
?????????[System.Runtime.InteropServices.DllImportAttribute("winspool.drv",?SetLastError?=?true)]
????????public?extern?static?int?OpenPrinter(
????????????string?pPrinterName,???//?printer?name
????????????ref?IntPtr?hPrinter,??????//?handle?to?printer?object
????????????ref?IntPtr?pDefault);????//?handle?to?default?printer?object.?
????????[System.Runtime.InteropServices.DllImportAttribute("winspool.drv",?SetLastError?=?true)]
????????public?static?extern?int?ClosePrinter(
????????????IntPtr?phPrinter);?//?handle?to?printer?object
2.調(diào)用DocumentProperties
private?void?documentPropButton_Click(object?sender,?EventArgs?e)
????????{
????????????string?printerName?=?_document.PrinterSettings.PrinterName;?
????????????if?(printerName?!=?null?&&?printerName.Length?>?0)
????????????{
????????????????IntPtr?pPrinter?=?IntPtr.Zero;
????????????????IntPtr?pDevModeOutput?=?IntPtr.Zero;
????????????????IntPtr?pDevModeInput?=?IntPtr.Zero;
????????????????IntPtr?nullPointer?=?IntPtr.Zero;
?????????????????OpenPrinter(printerName,?ref?pPrinter,?ref?nullPointer);?
????????????????int?iNeeded?=?DocumentProperties(this.Handle,?pPrinter,?printerName,?ref?pDevModeOutput,?ref?pDevModeInput,?0);
????????????????pDevModeOutput?=?System.Runtime.InteropServices.Marshal.AllocHGlobal(iNeeded);
????????????????DocumentProperties(this.Handle,?pPrinter,?printerName,?ref?pDevModeOutput,?ref?pDevModeInput,?DM_PROMPT);
????????????????ClosePrinter(pPrinter);
????????????}
????????}?
3.?調(diào)用PrinterProperties
private?void?printPropButton_Click(object?sender,?EventArgs?e)
????????{
????????????string?printerName?=?_document.PrinterSettings.PrinterName;?
????????????if?(printerName?!=?null?&&?printerName.Length?>?0)
????????????{
????????????????IntPtr?pPrinter?=?IntPtr.Zero;
????????????????IntPtr?pDevModeOutput?=?IntPtr.Zero;
????????????????IntPtr?pDevModeInput?=?IntPtr.Zero;
????????????????IntPtr?nullPointer?=?IntPtr.Zero;
?????????????????OpenPrinter(printerName,?ref?pPrinter,?ref?nullPointer);?
????????????????int?iNeeded?=?PrinterProperties(this.Handle,?pPrinter);
????????????????ClosePrinter(pPrinter);
????????????}
總結(jié)
以上是生活随笔為你收集整理的C#调用API弹出打印机属性对话框的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 反射 框架_Java 反射,开
- 下一篇: kafka streams实战 pdf_