温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

打印机的管理

发布时间:2020-07-30 11:20:31 来源:网络 阅读:553 作者:Chinayu2014 栏目:编程语言
  1. 枚举计算机的所有打印机

#include <winspool.h>
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;
}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI