不使用mfc框架和文档视图支持的情况下如何进行打印?我是第一次做打印。很多问题不是太明白。
如何获得打印机的dc?用CPrintDialog类的成员函数可否实现?CreatePrinterDC()和GetPrinterDC()可以么?在那种情况下使用?
CChildView类有没有支持打印的相关函数?
请各位高手指点.分数继续加.
如何获得打印机的dc?用CPrintDialog类的成员函数可否实现?CreatePrinterDC()和GetPrinterDC()可以么?在那种情况下使用?
CChildView类有没有支持打印的相关函数?
请各位高手指点.分数继续加.
解决方案 »
- SetWindowPlacement失败
- 请问 : 如何枚举任务栏上的窗口句柄,而且顺序是从左至右的顺序
- 两个坐标转换公式根据啥原理得出的?
- 在VS.NET中用VC编程时怎么将资源里面的一个对话框自动生成一个对应的类?在VC6.0里面用ClassWizard可以。
- 请问win2000server可以支持多少的套接连接!~
- 怎样从文件中读取数据?
- 2个简单的问题 online
- 使用"变量名=0"的方式却不能使编辑框中的值变为0,为什么?
- 有谁在WINDOWSxp中用过add-in Visual Assist,我打开一段时间,他好象就不起作用那,最后退出VC时,还出现BUG.
- StartService 加载驱动提示 “拒绝访问” 该如何解决
- 小问题,在线等。有关消息问题
- 请教大家,如何在CFormView添加滚动条,设置滚动
PrintDlg(),StartDoc(),EndDoc()...
{
CDC *pDC; //pDC
//打印机的初始化设置
CPrintDialog dlg(FALSE,PD_ALLPAGES | PD_USEDEVMODECOPIES | PD_NOPAGENUMS | PD_HIDEPRINTTOFILE | PD_NOSELECTION,this);
CRuixingZJApp* pApp = static_cast<CRuixingZJApp*>( ::AfxGetApp() );
long Len;
WORD nWord = 0;
HANDLE hPrinter;
DEVMODE* pDevMode;
DEVNAMES* lpDevNames;
LPTSTR lpszDeviceName;
///////////////////////////////////////////////////////////////////
if(pApp->GetPrinterDeviceDefaults(&dlg.m_pd) == 0)
{
::AfxMessageBox("错误!当前没有连接打印机!",MB_OK);
return;
}
pDevMode = (DEVMODE*)GlobalLock(dlg.m_pd.hDevMode);
lpDevNames = (LPDEVNAMES)GlobalLock(dlg.m_pd.hDevNames);
lpszDeviceName = (LPTSTR )lpDevNames + lpDevNames->wDeviceOffset; if (::OpenPrinter((char *) pDevMode->dmDeviceName, &hPrinter, NULL) == FALSE)
return;
Len = DocumentProperties(NULL, NULL, lpszDeviceName, pDevMode, NULL, 0);
pDevMode->dmPaperSize = DMPAPER_A4;
lstrcpy((char *)pDevMode->dmFormName,"A4 210 *297 mm"); Len = DocumentProperties(NULL, hPrinter, lpszDeviceName, NULL, pDevMode, DM_IN_BUFFER);
GlobalUnlock(dlg.m_pd.hDevMode);
GlobalUnlock(dlg.m_pd.hDevNames); ////////////////////////////////////////////////////////////////////////////////
if (dlg.CreatePrinterDC() == NULL)
return ;
if (dlg.DoModal() == IDOK)
{
m_pPrintDC.Attach(dlg.m_pd.hDC);
if(m_pPrintDC.GetSafeHdc() == NULL)
{
::AfxMessageBox("当前没有连接打印机!", MB_OK);
return;
} pDC = &m_pPrintDC;
ASSERT_VALID( pDC ); DOCINFO di;
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = "微机监测实时数据表格";
di.lpszOutput = NULL;
di.lpszDatatype = NULL;
di.fwType = 0;
//取打印机的横方向和纵方向的分辨率
//即每英寸点数
cxInch = pDC->GetDeviceCaps(LOGPIXELSX);
cyInch = pDC->GetDeviceCaps(LOGPIXELSY);
//取打印纸张高度和宽度
nPageHeight = pDC->GetDeviceCaps(VERTRES);
nPageWidth = pDC->GetDeviceCaps(HORZRES); m_CharWidth = (long)((float)(nPageWidth*37/40)/(float)80);
m_LineHeight = (long)((float)(nPageHeight*19/20)/(float)80); //==========================================
CFont font,*oldFont,titleFont; //设定新字体
m_lf.lfWidth = m_CharWidth;
m_lf.lfHeight = m_LineHeight;
m_lf.lfCharSet = GB2312_CHARSET;
m_lf.lfOutPrecision = 3;
m_lf.lfClipPrecision = 2;
m_lf.lfQuality = PROOF_QUALITY;
m_lf.lfPitchAndFamily = 49;
m_lf.lfEscapement = 0;
m_lf.lfOrientation = 0;
m_lf.lfUnderline = 0;
m_lf.lfPitchAndFamily = DEFAULT_PITCH|FF_ROMAN;
font.CreateFont(m_lf.lfHeight,
m_lf.lfWidth,
m_lf.lfEscapement,
m_lf.lfOrientation,
FW_NORMAL,
FALSE,
m_lf.lfUnderline,
0,
m_lf.lfCharSet,
m_lf.lfOutPrecision,
m_lf.lfClipPrecision,
m_lf.lfQuality,
m_lf.lfPitchAndFamily,
"宋体");
LOGFONT curLogFont, newLogFont;
//取当前字体
font.GetLogFont( &curLogFont );
newLogFont = curLogFont; //保存旧字体
if (!titleFont.CreateFontIndirect(&newLogFont))
return;
oldFont = pDC->SelectObject(&titleFont);
int Height = 400;
int ye = 1;//第一页
if (pDC->StartDoc(&di) > 0)
{
//打印整页
while (ye < m_ye)
{
pDC->StartPage();
pDC->MoveTo(nPageWidth/20, m_LineHeight);
DrawTable(pDC, Height, ye, 35);
pDC->EndPage();
ye = ye + 1;
}
//没有数据
if ((m_ye == 1)&&(m_other == 0))
{return;}
//打印剩下的数据或不为一页的数据
if (((m_ye == 1)&&(m_other > 0))||(ye == m_ye))
{
pDC->StartPage();
pDC->MoveTo(nPageWidth/20, m_LineHeight);
DrawTable(pDC, Height, ye, m_other);
pDC->EndPage();
}
pDC->EndDoc();
}
pDC->SelectObject(oldFont);
m_pPrintDC.Detach();
}
}一个例子,看看吧
希望对你有所帮助
//////////////////////////////////////////////////////////////////////////
void CDlgDlg::OnOK()
{
// TODO: Add extra validation here
CPrintDialog dlgPrint(false,PD_ALLPAGES,this); //
if(dlgPrint.DoModal()==IDOK)
{
CDC dcPrint;
dcPrint.Attach(dlgPrint.GetPrinterDC()); //creat and fill DOCINFO struct
DOCINFO myPrintJob;
myPrintJob.cbSize=sizeof(myPrintJob);
myPrintJob.lpszDocName="myPrintJob";
myPrintJob.lpszOutput=NULL;
myPrintJob.lpszDatatype=NULL;
myPrintJob.fwType=NULL;
//start printing the document
if(dcPrint.StartDoc(&myPrintJob)>=0)
{
//**start page
dcPrint.StartPage(); //start drawing
dcPrint.TextOut(0,0,"我要打印,靠.Fuck"); //throw the page
dcPrint.EndPage(); //close the document
dcPrint.EndDoc();
}
//delete the printer device context
dcPrint.DeleteDC();
}
CDialog::OnOK();
}