16,472
社区成员
发帖
与我相关
我的任务
分享
void CPrint_TestDlg::OnButton1()
{
int ret;
// 增加如下代码
char szprinter[80];
char *szDevice,*szDriver,*szOutput;
HDC hdcprint; // 定义一个设备环境句柄
//定义一个打印作业
static DOCINFO di={sizeof(DOCINFO),"printer",NULL};
// 得到设备字符串存入数组szprinter中
GetProfileString("windows","device",",,,",szprinter,80);
// 将设备字符串分解
if(NULL!=(szDevice=strtok(szprinter,","))&&NULL!=(szDriver=strtok(NULL,","))&&NULL!=(szOutput=strtok(NULL,",")))
// 创建一个打印机设备句柄
{
if((hdcprint=CreateDC(szDriver,szDevice,szOutput,NULL))!=0)
{
if(StartDoc(hdcprint,&di)>0) //开始执行一个打印作业
{
ret=StartPage(hdcprint); //打印机走纸,开始打印
/*DWORD dwError = GetLastError();
CString str;
str.Format(_T("Error Code :[%d]"), dwError);
AfxMessageBox(str);
SaveDC(hdcprint); //保存打印机设备句柄
// 输出一行文字
TextOut(hdcprint,1,1,"这是一个例子",12);
// 输出一条直线
MoveToEx(hdcprint,20,20,NULL);
LineTo(hdcprint,120,120);*/
RestoreDC(hdcprint,-1); //恢复打印机设备句柄
EndPage(hdcprint); //打印机停纸,停止打印
EndDoc(hdcprint); //结束一个打印作业
}
}
// 用API函数DeleteDC销毁一个打印机设备句柄
DeleteDC(hdcprint);
}
else
{
MessageBox("没有默认打印机,或者没有安装打印机!");
return;
}