2,640
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include <windows.h>
#include <winspool.h>
class ScopeHDC {
HDC hdc;
public:
explicit ScopeHDC(HDC hdc) : hdc(hdc) { }
~ScopeHDC() { DeleteDC(hdc); }
operator HDC() const { return hdc; }
};
int _tmain(int argc, _TCHAR* argv[])
{
LPTSTR pPrinterName = _T("Kyocera TASKalfa 400ci KX");
LPTSTR pDriverName = _T("Kyocera TASKalfa 400ci KX");
LPTSTR pPortName = _T("192.168.100.100");
HANDLE hPrinter;
PRINTER_INFO_2 *pi2 = NULL;
BOOL bFlag;
DWORD dwNeeded = 0;
PRINTER_DEFAULTS pd;
ZeroMemory(&pd, sizeof(pd));
pd.DesiredAccess = PRINTER_ALL_ACCESS;
BOOL res = OpenPrinter(pPrinterName, &hPrinter, &pd);
if (!res) {
return false;
}
LPDEVMODE devMode = NULL;
GetPrinter(hPrinter, 2, NULL, 0, &dwNeeded);
pi2 = (PRINTER_INFO_2 *)GlobalAlloc(GPTR, dwNeeded);
if (pi2 == NULL)
{
ClosePrinter(hPrinter);
return FALSE;
}
bFlag = GetPrinter(hPrinter, 2, (LPBYTE)pi2, dwNeeded, &dwNeeded);
if (!bFlag)
{
GlobalFree(pi2);
ClosePrinter(hPrinter);
return FALSE;
}
LONG structSize = DocumentProperties(NULL,
hPrinter,
pPrinterName,
NULL, /* Asking for size, so */
NULL, /* not used. */
0); /* Zero returns buffer size. */
if (structSize < sizeof(DEVMODE)) {
}
devMode = (LPDEVMODE)malloc(structSize);
if (!devMode)
return 0;
DocumentProperties(NULL,
hPrinter,
pPrinterName,
devMode,
NULL,
DM_OUT_BUFFER);
pi2->pDevMode = devMode;
DeleteForm(hPrinter, _T("Test Form"));
FORM_INFO_1 * pF1;
pF1=new(FORM_INFO_1);
pF1->Flags=FORM_USER;
pF1->pName=_T("Test Form");
pF1->Size.cx=2000*1000;
pF1->Size.cy=4000*1000;
pF1->ImageableArea.left=0;
pF1->ImageableArea.top=0;
pF1->ImageableArea.right=pF1->Size.cx;
pF1->ImageableArea.bottom=pF1->Size.cy;
devMode->dmFields = DM_FORMNAME;
wcscpy(devMode->dmFormName,pF1->pName);
LONG returnCode = DocumentProperties(NULL,
hPrinter,
pPrinterName,
devMode, /* The address of the buffer to fill. */
devMode, /* Not using the input buffer. */
DM_IN_BUFFER|DM_OUT_BUFFER); /* Have the output buffer filled. */
if (IDOK != returnCode) {
return 0;
}
if (!AddForm(hPrinter,1,(LPBYTE)pF1))
{
DWORD dwError = GetLastError();
delete pF1;
ClosePrinter(hPrinter);
return 0;
}
ScopeHDC hdc(CreateDC(pDriverName, pPrinterName, pPortName, devMode));
if (!hdc)
return false;
DOCINFO di = { 0 };
di.cbSize = sizeof (DOCINFO);
di.lpszDocName = (LPCTSTR)TEXT("Test Print");
di.lpszOutput = (LPTSTR) NULL;
di.lpszDatatype = (LPTSTR) NULL;
di.fwType = 0;
int nJobId = StartDoc(hdc, &di);
if (nJobId <= 0)
{
DWORD dwError = GetLastError();
return false;
}
StartPage(hdc);
SaveDC(hdc);
TextOut(
hdc,
100,
100,
_T("Test Test"),
wcslen(_T("Test Test")));
RestoreDC(hdc,-1);
EndPage(hdc);
EndDoc(hdc);
delete pF1;
ClosePrinter(hPrinter);
return 0;
}