操作打印机任务,200分求代码!

dingyacan 2009-09-09 09:21:50
rt
...全文
127 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
A阿_Qiang 2009-09-10
  • 打赏
  • 举报
回复
学习了,顶!
joeweng 2009-09-10
  • 打赏
  • 举报
回复
学习
dingyacan 2009-09-09
  • 打赏
  • 举报
回复
解决了,结贴,[BitCoffee]来第二贴拿分
wuyq11 2009-09-09
  • 打赏
  • 举报
回复
[DllImport("winspool.drv", CharSet = CharSet.Auto)]
public static extern bool OpenPrinter(string pPrinterName, out IntPtr phPrinter, IntPtr pDefault);
[DllImport("winspool.drv", CharSet = CharSet.Auto)]
public static extern bool ClosePrinter(IntPtr hPrinter);
[DllImport("winspool.drv", CharSet = CharSet.Auto)]
public static extern int EnumJobs(IntPtr hPrinter, int FirstJob, int NoJobs, int Level, IntPtr pInfo, int cdBuf,out int pcbNeeded, out int pcReturned);
OpenPrinter,用来获取给定打印机的句柄,通过该句柄可以实现对相应打印机的操作。
EnumJobs,用来列出所指定打印机上正在打印的作业信息
castlooo 2009-09-09
  • 打赏
  • 举报
回复
学习
gui0605 2009-09-09
  • 打赏
  • 举报
回复
gisyellow 2009-09-09
  • 打赏
  • 举报
回复
学习了。。
BitCoffee 2009-09-09
  • 打赏
  • 举报
回复

using System.Runtime.InteropServices;

unsafe struct PRINTER_DEFAULTS
{
public void* pDatatype; // LPTSTR
public void* pDevMode; // LPDEVMODE
public uint DesiredAccess; // ACCESS_MASK
};

[DllImport("kernel32", SetLastError = true)]
static extern int GetLastError();

[DllImport("WinSpool.drv", SetLastError = true)]
static extern unsafe bool OpenPrinter
(string pPrinterName, int* phPrinter, void* pDefault);

[DllImport("WinSpool.drv", SetLastError = true)]
static extern bool ClosePrinter(int hPrinter);

[DllImport("WinSpool.drv", SetLastError = true)]
static extern unsafe bool SetPrinter
(int hPrinter, uint Level, void* pPrinter, uint Command);

const uint PRINTER_ACCESS_ADMINISTER = 0x00000004;
const uint PRINTER_STATUS_OFFLINE = 0x00000080;
const uint PRINTER_CONTROL_PAUSE = 1;
const uint PRINTER_CONTROL_RESUME = 2;
const uint PRINTER_CONTROL_PURGE = 3;
const uint PRINTER_CONTROL_SET_STATUS = 4;

// Open a printer for administration operations.
static unsafe int CSOpenPrinter(string printerName)
{
bool bResult;
int hPrinter;
PRINTER_DEFAULTS pd;

pd.pDatatype = null;
pd.pDevMode = null;
pd.DesiredAccess = PRINTER_ACCESS_ADMINISTER;

bResult = OpenPrinter(printerName, &hPrinter, &pd);
if (!bResult)
{
throw new ApplicationException("Cannot open printer '" +
printerName + "' " +
Marshal.GetLastWin32Error());
}
return hPrinter;
}

// Close the printer.
static void CSClosePrinter(int hPrinter)
{
if (!ClosePrinter(hPrinter))
{
throw new ApplicationException("Cannot close printer " +
Marshal.GetLastWin32Error());
}
}

// Pause printer.
static unsafe void CSPausePrinter(int hPrinter)
{
if (!SetPrinter(hPrinter, 0, null, PRINTER_CONTROL_PAUSE))
{
throw new ApplicationException("Cannot pause printer " +
Marshal.GetLastWin32Error());
}
}

// Delete printer.
static unsafe void CSDeletePrinter(int hPrinter)
{
if (!SetPrinter(hPrinter, 0, null, PRINTER_CONTROL_PURGE))
{
throw new ApplicationException("Cannot delete printer " +
Marshal.GetLastWin32Error());
}
}

// Resume printer.
static unsafe void CSResumePrinter(int hPrinter)
{
if (!SetPrinter(hPrinter, 0, null, PRINTER_CONTROL_RESUME))
{
throw new ApplicationException("Cannot resume printer " +
Marshal.GetLastWin32Error());
}
}

// Disable printer (offline).
static unsafe void CSDisablePrinter(int hPrinter)
{
if (!SetPrinter(hPrinter, 0,
(byte*)PRINTER_STATUS_OFFLINE,
PRINTER_CONTROL_SET_STATUS))
{
throw new ApplicationException("Cannot disable printer " +
Marshal.GetLastWin32Error());
}
}

// Enable printer.
static unsafe void CSEnablePrinter(int hPrinter)
{
if (!SetPrinter(hPrinter, 0, (byte*)0, PRINTER_CONTROL_SET_STATUS))
{
throw new ApplicationException("Cannot enable printer " +
Marshal.GetLastWin32Error());
}
}

//
int hPrinter;
try
{
hPrinter = CSOpenPrinter("打印机名");
CSPausePrinter(hPrinter);
CSDeletePrinter(hPrinter);
CSClosePrinter(hPrinter);
}
catch
{

}

110,557

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧