vc下打印机打印的问题

jianren_ko 2014-04-11 10:55:00
整个打印源码如下:
STDMETHODIMP CPrinterInterface::OnPrint(ULONG* pData, ULONG* pParaData, ULONG lData)
{
// TODO: 在此添加实现代码
IOmRonPlcMain* pIOmRonPlcMain = (IOmRonPlcMain*)(*pParaData);
TCHAR szPrinter[100];
char *szDevice,*szDriver,*szOutput;
HDC hdcPrint = NULL;

//取得设备字符串
static DOCINFO di={sizeof(DOCINFO),_T("printer"),NULL};
DWORD strTemp = GetProfileString(_T("windows"),_T("device"),_T(",,,"),szPrinter,100);

//分解字符串
int nOutputStrLen = WideCharToMultiByte (CP_ACP, 0, szPrinter, wcslen (szPrinter), NULL, 0, 0, 0) + 2; //这里的+2是指宽字符的结束符
char* szcPrinter;
szcPrinter = new char[nOutputStrLen];
WideCharToMultiByte(CP_ACP, 0, szPrinter, wcslen (szPrinter)+1, szcPrinter, nOutputStrLen, 0, 0); //这里的+1是指结束符
if ((NULL!=(szDevice=strtok(szcPrinter,",")))&&(NULL!=(szDriver=strtok(NULL,",")))&&(NULL!=(szOutput=strtok(NULL,","))))
{
//创建打印设备句柄
DWORD dwNum = MultiByteToWideChar (CP_ACP, 0, szOutput, -1, NULL, 0);
wchar_t *pwOutput;
pwOutput = new wchar_t[dwNum];
MultiByteToWideChar (CP_ACP, 0, szOutput, -1, pwOutput, dwNum);

dwNum = MultiByteToWideChar (CP_ACP, 0, szDriver, -1, NULL, 0);
wchar_t *pwDriver;
pwDriver = new wchar_t[dwNum];
MultiByteToWideChar (CP_ACP, 0, szDriver, -1, pwDriver, dwNum);

dwNum = MultiByteToWideChar (CP_ACP, 0, szDevice, -1, NULL, 0);
wchar_t *pwDevice;
pwDevice = new wchar_t[dwNum];
MultiByteToWideChar (CP_ACP, 0, szDevice, -1, pwDevice, dwNum);

if (/*hdcPrint!=NULL*/(hdcPrint=CreateDC(pwDriver,pwDevice,pwOutput,NULL))!=NULL)
{
//开始执行打印作业
if (StartDoc(hdcPrint,&di)>0)
{
int nTest = StartPage(hdcPrint); //开始走纸
SaveDC(hdcPrint); //保存打印机设备句柄

CRect rcClient;
GetClientRect((HWND)(*pData),&rcClient);
TextOut(hdcPrint,1,1,_T("HELLOWORD!"),16);
pIOmRonPlcMain->Draw((ULONG)hdcPrint,rcClient.left,rcClient.top,rcClient.right,rcClient.bottom);

DWORD dwNeeded = 0;
DWORD dwReturned = 0;
if(EnumJobs(hdcPrint,0,0xFFFFFFFF,NULL,0,NULL,0,&dwNeeded))
{
if(GetLastError() != ERROR_INSUFFICIENT_BUFFER)
{
ClosePrinter(hdcPrint);
return FALSE;
}
}

RestoreDC(hdcPrint,-1); //恢复打印机设备句柄
if (nTest = EndPage(hdcPrint)>0)
{
DWORD nTemp = GetLastError();
nTest = EndDoc(hdcPrint);
nTemp = GetLastError();
}


DeleteDC(hdcPrint);
}
}

if(pwDevice)
{
delete []pwDevice;
pwDevice = NULL;
}
if(pwDriver)
{
delete []pwDriver;
pwDriver = NULL;
}
if(pwOutput)
{
delete []pwOutput;
pwOutput = NULL;
}
}
else
{
//MessageBox(_T("没有默认打印机,或者没有安装打印机!"));
return S_OK;
}
return S_OK;
}

现在的问题是,调用EndPage()总是返回-1,错误,求高人指点啊
...全文
134 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
jianren_ko 2014-04-12
  • 打赏
  • 举报
回复
我知道问题在哪儿了,原因是pIOmRonPlcMain->Draw((ULONG)hdcPrint,rcClient.left,rcClient.top,rcClient.right,rcClient.bottom);调用后,hdcPrint失效了。在这里,hdcPrint他是打印设备dc,draw函数里把它转换为屏幕设备dc,所以失效。
赵4老师 2014-04-11
  • 打赏
  • 举报
回复
EndPage The EndPage function informs the device that the application has finished writing to a page. This function is typically used to direct the device driver to advance to a new page. This function replaces the NEWFRAME printer escape. int EndPage( HDC hdc // handle to device context ); Parameters hdc Handle to the device context for the print job. Return Values If the function succeeds, the return value is greater than zero. If the function fails, the return value is less than or equal to zero. Windows NT: To get extended error information, callGetLastError. Remarks Use the ResetDC function to change the device mode, if necessary, after calling the EndPage function. Note that a call to ResetDC resets all device context attributes back to default values. Windows 3.x: EndPage resets the device context attributes back to default values. You must re-select objects and set up the mapping mode again before printing the next page. Windows 95: EndPage does not reset the device context attributes. However, the next StartPage call does reset the device context attributes to default values. At that time, you must re-select objects and set up the mapping mode again before printing the next page. Windows NT: Beginning with Windows NT Version 3.5, neither EndPage or StartPage resets the device context attributes. Device context attributes remain constant across subsequent pages. You do not need to re-select objects and set up the mapping mode again before printing the next page; however, doing so will produce the same results and reduce code differences between Windows 95 and Windows NT. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Requires version 2.0 or later. Header: Declared in wingdi.h. Import Library: Use gdi32.lib. See Also Printing and Print Spooler Overview, Printing and Print Spooler Functions, ResetDC, StartPage

3,882

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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