程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> 調用API彈出打印機屬性對話框實現代碼

調用API彈出打印機屬性對話框實現代碼

編輯:關於C#
 

Author:vitoriatang
From:Internet
.NET Framework封裝了很多關於打印的對話框,比如說PrintDialog, PageSetupDialog.
但是有的時候我們還需要關心打印機屬性對話框,那麼就可以調用API來解決這個問題。有幾個API函數與之相關
PrinterProperties
DocumentProperties
OpenPrinter
ClosePrinter
逐一介紹

printerproperties
顯示打印機屬性對話框。

documentproperties
顯示打印機配置對話框。

openprinter
打開打印機

closeprinter
關閉打印機

在調用printerproperties或者documentproperties的時候,都需要先調用openprinter,並在結束後調用closeprinter。

至於打印機屬性和打印機配置有什麼不同,就自己領會了。更為詳盡的信息可以查閱msdn

sample codes:
1. 聲明API函數
[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.調用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. 調用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);
}
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved