看几行代码
这是一个对话框按钮单击事件
我想打印出一张纸,页面的内容包括标题,结尾,都是我自己设置的,
另外将这个对话框打印在页面的中间
现在对话框页面不知道如何打印 ?? 请大家帮个忙,谢谢
void CPrintSelfDlg::OnBtnPrintself()
{
CDC pd;
CPrintDialog dlg(FALSE);
dlg.GetDefaults();
pd.Attach(dlg.GetPrinterDC());
pd.m_bPrinting = TRUE;
DOCINFO di;
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = "数据打印";
di.lpszOutput = (LPTSTR) NULL;
di.lpszDatatype = (LPTSTR) NULL;
di.fwType = 0;
StartDoc(pd.m_hAttribDC, &di);
StartPage(pd.m_hAttribDC);
//打印标题
CString strCaption;
CFont font,*oldfont;
CPoint point,pointt;
int nWidth=pd.GetDeviceCaps(HORZRES);
int nHeight=pd.GetDeviceCaps(VERTRES);
CRect DrawRect(0,0,nWidth,nHeight),rect,tempRect;
strCaption=_T("(DPU)RS232测试结果");
DrawRect.InflateRect(-100,-400,-100,-350);
pd.Rectangle(&DrawRect);
font.CreateFont(-MulDiv(35,-pd.GetDeviceCaps(LOGPIXELSY),72),
0,0,0,FW_NORMAL,0,0,0,GB2312_CHARSET,
OUT_STROKE_PRECIS,CLIP_STROKE_PRECIS,DRAFT_QUALITY,
VARIABLE_PITCH|FF_SWISS,_T("黑体"));
oldfont=pd.SelectObject(&font);
tempRect=DrawRect;
tempRect.InflateRect(0,-200,0,0);
pd.DrawText(strCaption,&tempRect,DT_CALCRECT|DT_SINGLELINE);
tempRect.right=DrawRect.right;
pd.DrawText(strCaption,&tempRect,DT_CENTER|DT_TOP|DT_SINGLELINE);
//打印对话框 无法打印出来,请大家帮忙
CRect rect1;
GetWindowRect(rect1);
CString str;
pd.StretchBlt(0,0,pd.GetDeviceCaps(VERTRES),pd.GetDeviceCaps(VERTRES)*rect1.Height()/rect1.Width()
,GetDC(),0,0,rect1.Width(),rect1.Height(),SRCCOPY);
pd.SelectObject(oldfont);
font.DeleteObject();
//打印结尾正文
CString strNum = _T("产品编号");
CString strCorp = _T("送检单位");
CString ChkPerson = _T("检测人员");
CString AudiPerson = _T("审核人员");
CString ChkDate = _T("检测日期");
font.CreateFont(-MulDiv(20,-pd.GetDeviceCaps(LOGPIXELSY),72),
0,0,0,FW_NORMAL,0,0,0,GB2312_CHARSET,
OUT_STROKE_PRECIS,CLIP_STROKE_PRECIS,DRAFT_QUALITY,
VARIABLE_PITCH|FF_SWISS,_T("黑体"));
pd.SelectObject(&font);
//打印产品编号
pd.TextOut(DrawRect.left+100,DrawRect.bottom-1100,strNum);
pd.MoveTo(DrawRect.left+800,DrawRect.bottom-920);
pd.LineTo(DrawRect.left+2000,DrawRect.bottom-920);
//打印送检单位
pd.TextOut(DrawRect.left+100,DrawRect.bottom-900,strCorp);
pd.MoveTo(DrawRect.left+800,DrawRect.bottom-720);
pd.LineTo(DrawRect.left+2000,DrawRect.bottom-720);
//打印检测人员
pd.TextOut(DrawRect.left+100,DrawRect.bottom-700,ChkPerson);
pd.MoveTo(DrawRect.left+800,DrawRect.bottom-520);
pd.LineTo(DrawRect.left+2000,DrawRect.bottom-520);
//打印审核人员
pd.TextOut(DrawRect.left+100,DrawRect.bottom-500,AudiPerson);
pd.MoveTo(DrawRect.left+800,DrawRect.bottom-320);
pd.LineTo(DrawRect.left+2000,DrawRect.bottom-320);
//打印检测日期
pd.TextOut(DrawRect.left+100,DrawRect.bottom-300,ChkDate);
pd.MoveTo(DrawRect.left+800,DrawRect.bottom-120);
pd.LineTo(DrawRect.left+2000,DrawRect.bottom-120);
pd.SelectObject(oldfont);
font.DeleteObject();
font.CreateFont(-MulDiv(15,-pd.GetDeviceCaps(LOGPIXELSY),72),
0,0,0,FW_NORMAL,0,0,0,GB2312_CHARSET,
OUT_STROKE_PRECIS,CLIP_STROKE_PRECIS,DRAFT_QUALITY,
VARIABLE_PITCH|FF_SWISS,_T("黑体"));
pd.SelectObject(&font);
CTime time;
CString strTime,strYear,strMonth,strDay,strHour,strMinute,strSecond;
time=CTime::GetCurrentTime ();
strYear.Format("%d",time.GetYear());
strMonth.Format("%d",time.GetMonth());
strDay.Format("%d",time.GetDay());
strHour.Format("%02d",time.GetHour());
strMinute.Format("%02d",time.GetMinute());
strSecond.Format("%02d",time.GetSecond());
strTime = strYear+"."+strMonth+"."+strDay+" "+""+strHour+":"+strMinute+":"+strSecond;
pd.TextOut(DrawRect.left+800,DrawRect.bottom-280,strTime);
//自动填写产品编号
CString StrID = "123456789" ;
pd.TextOut(DrawRect.left+850,DrawRect.bottom-1080,StrID);
pd.SelectObject(oldfont);
font.DeleteObject();
//打印页码
font.CreateFont(-MulDiv(10,-pd.GetDeviceCaps(LOGPIXELSY),72),
0,0,0,FW_NORMAL,0,0,0,GB2312_CHARSET,
OUT_STROKE_PRECIS,CLIP_STROKE_PRECIS,DRAFT_QUALITY,
VARIABLE_PITCH|FF_SWISS,_T("黑体"));
pd.SelectObject(&font);
pd.TextOut(DrawRect.left+4000,DrawRect.bottom-150,"第 5 页");
EndDoc(pd.m_hAttribDC);
//打印结束
DeleteDC(pd.m_hAttribDC);
}
问题点数:100、回复次数:9Top
1 楼rageliu(天气好了就去长白山看水怪去了,嘿嘿...)回复于 2005-10-18 14:53:28 得分 2
没做过打印!
顶下Top
2 楼djfu(飞龙在天)回复于 2005-10-18 15:13:57 得分 2
基本思路是得到打印机的DC,然后走纸打印。关注。Top
3 楼legendhui(秋天的叶子)回复于 2005-10-18 16:07:31 得分 5
http://www.vckbase.com/code/winview/dialog/EBCode21.zip
http://www.vckbase.com/code/winview/print/previewdemo.rarTop
4 楼yayaniuniu502(老唐)回复于 2005-10-18 16:34:53 得分 2
加一句
pd.SetBitmapBits()
在你StretchBlt()后试试,
我觉得问题就出在这里,图片的显示和文字不一样,你还应该加显示图象的语句。Top
5 楼maruchun(小马过河)回复于 2005-10-18 17:14:52 得分 0
谢谢秋天的叶子 和 老唐,但是都不是我想要的效果,我是要将对话框页面和本身一摸一样的打印出来Top
6 楼maruchun(小马过河)回复于 2005-10-18 17:31:22 得分 0
主要是这一段 //打印对话框 无法打印出来,请大家帮忙
CRect rect1;
GetWindowRect(rect1);
CString str;
pd.StretchBlt(0,0,pd.GetDeviceCaps(VERTRES),pd.GetDeviceCaps(VERTRES)*rect1.Height()/rect1.Width()
,GetDC(),0,0,rect1.Width(),rect1.Height(),SRCCOPY);
pd.SelectObject(oldfont);
font.DeleteObject();
请大家帮我改一下,谢谢Top
7 楼maruchun(小马过河)回复于 2005-10-18 17:49:44 得分 0
再问一句,打印程序和打印机类型(激光,喷墨)有关系吗? 谢谢Top
8 楼Mackz(在相互)回复于 2005-10-18 19:52:20 得分 4
GetDC()应该改成GetWindowDC()吧。Top
9 楼happyparrot(快乐鹦鹉)回复于 2005-10-20 14:09:30 得分 85
先拷贝对话框。
CRect rect1;
GetClientRect(rect1);
HBITMAP hBmp = CopyScreenToBitmap(GetDC()->m_hDC,rect1);
打印标题后,再打印。
HDC hDC = CreateCompatibleDC(pd.m_hDC);
SelectObject(hDC,hBmp);
StretchBlt(pd.m_hDC,100,tempRect.top,nWidth-200,nWidth*rect1.Height()/rect1.Width()
,hDC,0,0,rect1.Width(),rect1.Height(),SRCCOPY);Top




