网站建设资讯

NEWS

网站建设资讯

打印机的管理

  1. 枚举计算机的所有打印机

    成都创新互联公司专业为企业提供罗甸网站建设、罗甸做网站、罗甸网站设计、罗甸网站制作等企业网站建设、网页设计与制作、罗甸企业网站模板建站服务,十余年罗甸做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

#include 
PRINTER_INFO_5* prninfo = NULL;
void CMFCApplication1Dlg::OnBnClickedButton8()
{
	DWORD dwNeeded=0, dwReturned=0;
	EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 5, NULL, 0, &dwNeeded, &dwReturned);
	prninfo = (PRINTER_INFO_5*)GlobalAlloc(GPTR, dwNeeded);

	if (EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 5, (LPBYTE)prninfo, dwNeeded,
	 &dwNeeded, &dwReturned)) 
	 {
		for (DWORD i = 0; i < dwReturned; i++, prninfo++) 
		{
			TRACE(_T("x = %s\n"), prninfo->pPrinterName);
			TRACE(_T("x = %s\n"), prninfo->pPortName);
		}
	}
}

2.设置默认打印机

TCHAR szPrinterName[1024] = { 0 };
GetDlgItemText(IDC_EDIT1, szPrinterName, 1024);
BOOL ret = FALSE;
ret = SetDefaultPrinter(szPrinterName);
if (ret == FALSE)
	AfxMessageBox(_T("设置默认打印机失败"));
else
	AfxMessageBox(_T("设置默认打印机成功"));

3.启动打印机服务

SHELLEXECUTEINFO ShExecInfo = { 0 };
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask  = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd   = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = _T("cmd");

ShExecInfo.lpParameters = _T("/C net start spooler");

ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow       = SW_HIDE;
ShExecInfo.hInstApp    = NULL;

ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess, INFINITE);

4.获取指定打印机的所有纸张

TCHAR szPaperName[30000] = {0};
TCHAR str[650] = {0};
void CMFCApplication1Dlg::Printerdo1()
{
	BYTE* pBuffer = NULL;
	DWORD dwSize = 0;
	DWORD dwNum = 0;

	//获得缓冲区大小
	::EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 5, pBuffer, dwSize, &dwSize, &dwNum);
	pBuffer = new BYTE[dwSize];

	//枚举打印机
	::EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 5, pBuffer, dwSize, &dwSize, &dwNum);
	if (dwNum != 0)
	{
		//获得打印机信息
		PRINTER_INFO_5* pPrinterInfo = (PRINTER_INFO_5*)pBuffer;

		LPHANDLE phandle;
		HANDLE handle;
		handle = malloc(sizeof(HANDLE));
		phandle = &handle;
		PRINTER_DEFAULTS *printer_defaults;
		printer_defaults = (PRINTER_DEFAULTS *)malloc(sizeof(PRINTER_DEFAULTS));
		LPWSTR str_printername, str_portname, papername2;

		//添加打印机名及端口
		str_printername = pPrinterInfo->pPrinterName;
		str_portname = pPrinterInfo->pPortName;
		int nNeeded = 0;
		nNeeded = DeviceCapabilities(str_printername, str_portname, DC_PAPERNAMES, 
		                             NULL, NULL);
		DeviceCapabilities(str_printername, str_portname, DC_PAPERNAMES, 
		                             szPaperName,NULL);

		for (int i = 0, j=0; i < nNeeded && szPaperName[j] != 0;)
		{
			
			wmemcpy_s(str, 64, (LPCWSTR)(&szPaperName)+j,64);
			m_ListPrinter.AddString(str);
			TRACE(_T("%s\n"), str);
               j += 64;
		}
	}
}

5.获取所有纸张的尺寸

CPoint  pt[107];
DeviceCapabilities(str_printername, str_printername, DC_PAPERSIZE, (LPWSTR)&pt, NULL);

6.获取默认打印机的默认纸型和大小

void CMFCApplication1Dlg::OnBnClickedButton13()
{
	// TODO: 在此添加控件通知处理程序代码
	PRINTDLG FAR * pPrintDlg = new PRINTDLG;
	//获取当前打印机的设置
	if (AfxGetApp()->GetPrinterDeviceDefaults(pPrintDlg))
	{
	      //获取打印机的两个设置信息结构
	      DEVNAMES FAR*lpDevNames=(DEVNAMES FAR *)::GlobalLock(pPrintDlg->hDevNames);
	      DEVMODE FAR*lpDevMode =(DEVMODE FAR *)::GlobalLock(pPrintDlg->hDevMode);

	      //获取相关信息:驱动服务名,打印机名,端口号
	      CString szDriver((LPTSTR)lpDevNames +lpDevNames->wDriverOffset);
	      CString szDevice((LPTSTR)lpDevNames +lpDevNames->wDeviceOffset);
	      CString szOutput((LPTSTR)lpDevNames +lpDevNames->wOutputOffset);

	      HANDLE hPrinter;
	      ::OpenPrinter((LPWSTR)(LPCWSTR)szDevice, &hPrinter, NULL);
	      ::DocumentProperties(NULL,hPrinter,(LPWSTR)(LPCWSTR)szDevice,lpDevMode,
			lpDevMode, DM_IN_BUFFER | DM_OUT_BUFFER);

	      // 结构解锁
	      ::GlobalUnlock(pPrintDlg->hDevNames);
	      ::GlobalUnlock(pPrintDlg->hDevMode);
	}
	delete pPrintDlg;
}

文章题目:打印机的管理
网页链接:http://njwzjz.com/article/pdchjh.html