锐英源软件
第一信赖

精通

英语

开源

擅长

开发

培训

胸怀四海 

第一信赖

当前位置:锐英源 / 软件开发培训 / 学员作品 / MFC横向打印预览
导航
源代码下载
服务方向
软件开发
办公财务MIS
股票
设备监控
网页信息采集及控制
多媒体
软件开发培训
Java 安卓移动开发
Java Web开发
HTML5培训
iOS培训
网站前端开发
VC++
C++游戏开发培训
C#软件
C语言(Linux)
ASP.NET网站开发(C#)
C#软件+ASP.NET网站
SOCKET网络通信开发
COMOLE和ActiveX开发
C++(Linux)
汇编和破解
驱动开发
SkinMagicVC++换肤
流媒体开发
MicroStation二次开发
计算机英语翻译
联系方式
固话:0371-63888850
手机:138-0381-0136
Q Q:396806883
微信:ryysoft

MFC横向打印预览

本例子用MFC相关类:子框架类和CView类实现了打印。
1、原例子把ListCtrl里内容竖向预览及打印,新例子支持横向预览及打印
2、CView派生类的OnPreparePrinting函数负责预览及打印前的准备,包括设备状态准备,在这里通过修改设备状态对应的成员变量实现了功能。涉及关键字:GetPrinterDeviceDefaults,CPrintInfo,dmOrientation,DMORIENT_LANDSCAPE

核心代码如下:

BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo)
{
// TODO: call DoPreparePrinting to invoke the Print dialog box
//设置横向打印
LPDEVMODE lpDevMode;
if(AfxGetApp()->GetPrinterDeviceDefaults(&pInfo->m_pPD->m_pd))
{
lpDevMode=(LPDEVMODE)GlobalLock(pInfo->m_pPD->m_pd.hDevMode);
if(lpDevMode)
{
lpDevMode->dmPaperSize=DMPAPER_A4;
lpDevMode->dmOrientation=DMORIENT_LANDSCAPE;
}
GlobalUnlock(pInfo->m_pPD->m_pd.hDevMode);
}
if (DoPreparePrinting(pInfo))
return TRUE;
else
{
GetParent()->DestroyWindow();
return FALSE;
}
}

LRESULT CMyPrintDlg::OnMyPrint(WPARAM wParam,LPARAM lParam)
{
UINT gl_uNumOfPoints = m_List.GetItemCount();
CDC* pDC = (CDC*)wParam;
CPrintInfo* pInfo = (CPrintInfo *)lParam;
int nPageNumber = pInfo->m_nCurPage;
TRACE("myprint get page %d\r\n",pInfo->m_nCurPage);
int i,j,columns = m_List.GetColumnCounts();
CFont *pOldFont;
CFont DataFont;
DataFont.CreatePointFont(120,"宋体",pDC);
CHeaderCtrl *header = m_List.GetHeaderCtrl();
//columns = header->GetItemCount();
int nItems = m_List.GetItemCount();

nPageNumber = nPageNumber % pInfo->GetMaxPage();
if (0 == nPageNumber)
{
nPageNumber = 1;
m_CurrenItem = 0;
pInfo->m_nCurPage = 1;
}
int m_templines = m_nLinesPerPage * (nPageNumber) ;
CString temp;
pOldFont=pDC->SelectObject(&DataFont);
temp.Format("总共%d页,第%d页,从第%d项到第%d项", pInfo->GetMaxPage(),nPageNumber,m_CurrenItem,m_templines);
pDC->TextOut(0, 0, temp);
if(nPageNumber > 0)
{
pDC->Rectangle(m_cxOffset,(int)((2.5 + PRINTMARGIN)*m_cyPrinter),m_cxOffset+m_cxWidth,(m_nLinesPerPage+3 + PRINTMARGIN)*m_cyPrinter+20);
for(i=1;i<7;i++)
{
pDC->MoveTo(m_cxOffset+i*m_cxWidth/7,(int)((2.5 + PRINTMARGIN)*m_cyPrinter));
pDC->LineTo(m_cxOffset+i*m_cxWidth/7,(3 +m_nLinesPerPage+PRINTMARGIN)*m_cyPrinter+20);
}
pDC->MoveTo(m_cxOffset,(4 + PRINTMARGIN)*m_cyPrinter);
pDC->LineTo(m_cxOffset+m_cxWidth,(4 + PRINTMARGIN)*m_cyPrinter);

for (i = 0; i < columns; i++)
{
CString temp;
TCHAR buffer[120];
HDITEM *hdItem = new HDITEM;
hdItem->mask = HDI_TEXT;
hdItem->pszText = buffer;
hdItem->cchTextMax = sizeof(buffer);
if (header->GetItem(i, hdItem))
{
temp = CString(buffer);
pDC->TextOut(m_cxOffset+50+i*m_cxWidth/7,(int)((2.8 + PRINTMARGIN)*m_cyPrinter),temp);
}
}
m_CurrenItem = (nPageNumber-1) * m_nLinesPerPage;
for(i=m_CurrenItem;i < m_templines && i < nItems;i++)
{

int nIndex = i - m_CurrenItem;
// if(nIndex<(int)gl_uNumOfPoints)
//{
for(j=0;j<columns;j++)
{
CString str;
str = m_List.GetItemText(i,j);//新改
pDC->TextOut(m_cxOffset+50+j*m_cxWidth/7,(4 + PRINTMARGIN+nIndex)*m_cyPrinter+5,str);
}
//}

}
// m_CurrenItem = i;
pDC->SelectObject (pOldFont);
// DeleteObject(DataFont);

}
/*if(nPageNumber==1)
{
HBITMAP hBitmap=::LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP1));
BITMAP bitmap;
::GetObject(hBitmap,sizeof(BITMAP),&bitmap);
double dScale=(double)m_cxWidth/bitmap.bmWidth;
int nScaledWidth=m_cxWidth;
int nScaledHeight=(int)(bitmap.bmHeight*dScale);

HDC dcMem;
dcMem=::CreateCompatibleDC(pDC->m_hDC);
HBITMAP hOldBmp=(HBITMAP)::SelectObject(dcMem,hBitmap);

int nVertCenterPos = pDC->GetDeviceCaps (VERTRES) / 2;
::StretchBlt(pDC->m_hDC,m_cxOffset,(4 + PRINTMARGIN)*m_cyPrinter,nScaledWidth,nScaledHeight,dcMem,0,0,bitmap.bmWidth,bitmap.bmHeight,SRCCOPY);


::SelectObject(dcMem,hOldBmp);
::DeleteDC(dcMem);
::DeleteObject(hBitmap);
}*/
// if(m_bPrintHead)
PrintPageHeader(pDC,pInfo->m_nCurPage, pInfo);
return TRUE;
}
LRESULT CMyPrintDlg::OnBeginPrinting(WPARAM wParam,LPARAM lParam)
{
CDC* pDC = (CDC*)wParam;
CPrintInfo* pInfo = (CPrintInfo*)lParam;
if(NULL == m_fontPrinter.m_hObject)
m_fontPrinter.CreatePointFont(120, "FixedSys", pDC);
TEXTMETRIC tm;
CFont *pOldFont = pDC->SelectObject(&m_fontPrinter);
pDC->GetTextMetrics(&tm);
m_cyPrinter = tm.tmHeight + tm.tmExternalLeading;
CSize size = pDC->GetTextExtent(_T("---------1---------2---------" \
"3---------4---------5---------6---------7---------8---"),81);
pDC->SelectObject(pOldFont);
m_cxWidth = size.cx;

m_nLinesPerPage = (pDC->GetDeviceCaps(VERTRES)-
(m_cyPrinter * (3 + (2* PRINTMARGIN))))/ m_cyPrinter;
int m_MaxPage = m_List.GetItemCount();
m_MaxPage = m_MaxPage / m_nLinesPerPage + (m_MaxPage % m_nLinesPerPage > 0 ? 1:0);
pInfo->SetMaxPage(m_MaxPage);
pInfo->m_nCurPage = 1; // start printing at page# 1
m_cxOffset = (pDC ->GetDeviceCaps(HORZRES) - size.cx) / 2;

return TRUE;
}

LRESULT CMyPrintDlg::OnEndPrinting(WPARAM wParam,LPARAM lParam)
{
if ( NULL != m_fontPrinter.m_hObject)
m_fontPrinter.DeleteObject();
return TRUE;
}

void CMyPrintDlg::LoadmList()
{
m_List.InsertColumn(0, "编号", LVCFMT_CENTER);
m_List.InsertColumn(1,"名称", LVCFMT_CENTER);
m_List.InsertColumn(2,"ID", LVCFMT_CENTER);
m_List.InsertColumn(3, "检验类别", LVCFMT_CENTER);
m_List.InsertColumn(4, "shh", LVCFMT_CENTER);
m_List.InsertColumn(5, "ywsx", LVCFMT_CENTER);
m_List.InsertColumn(6, "cdff", LVCFMT_CENTER);
m_List.AutoAdjustWidth();

for(int i=0;i<1500;i++)
{
m_List.InsertItem(i,"aaa");
}
}

void CMyPrintDlg::ReadEXCEL()
{
CDatabase database;
CString sSql;
CString sDriver = GetEXCELDriver();
if (sDriver.IsEmpty())
return;
CString sFile;

CFileDialog mFile(TRUE,"xls", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"Excel文件(*.xls)|*.xls|", this);

if (IDOK != mFile.DoModal())
return ;
sFile = mFile.GetPathName();

if (0 == mFile.m_ofn.nFileExtension)
sFile += ".xls";
AfxMessageBox(sFile);

try{
//sSql.Format("ODBC;DRIVER = {%s}; DSN = ''''; DBQ = %s",sDriver,sFile);
sSql.Format("ODBC;DRIVER={%s};DSN='''';DBQ=%s", sDriver, sFile);
database.Open(NULL, false, false, sSql);
CRecordset recordset(&database);
sSql.Format("select distinct bh, mc,id,jylbbh, shh, ywsx, cdff from [3785$] order by bh, mc");
recordset.Open(CRecordset::forwardOnly, sSql, CRecordset::readOnly);
int i = 0;

while(!recordset.IsEOF())
{
CString m_bh, m_mc, m_id, m_jylbbh, m_shh, m_ywsx, m_cdff;
recordset.GetFieldValue("bh",m_bh);m_bh.TrimLeft(); m_bh.TrimRight();
recordset.GetFieldValue("mc", m_mc);m_mc.TrimLeft(); m_mc.TrimRight();
recordset.GetFieldValue("id", m_id);m_id.TrimLeft(); m_id.TrimRight();
recordset.GetFieldValue("jylbbh", m_jylbbh);m_jylbbh.TrimLeft(); m_jylbbh.TrimRight();
recordset.GetFieldValue("shh", m_shh);m_shh.TrimLeft(); m_shh.TrimRight();
recordset.GetFieldValue("ywsx", m_ywsx);m_ywsx.TrimLeft(); m_shh.TrimRight();
recordset.GetFieldValue("cdff", m_cdff);m_cdff.TrimLeft(); m_cdff.TrimRight();
m_List.InsertItem(i,"");
m_List.SetItemText(i, 0, m_bh);
m_List.SetItemText(i, 1, m_mc);
m_List.SetItemText(i, 2, m_id);
m_List.SetItemText(i, 3, m_jylbbh);
m_List.SetItemText(i, 4, m_shh);
m_List.SetItemText(i, 5, m_ywsx);
m_List.SetItemText(i, 6, m_cdff);

i++;
recordset.MoveNext();
}
recordset.Close();
database.Close();
}catch(CDBException *e)
{
AfxMessageBox(e->m_strError);
}

}

CString CMyPrintDlg::GetEXCELDriver()
{
char szBuf[2001];
WORD cbBufMax = 2000;
WORD cbBufOut = 0;
char *pszBuf = szBuf;
CString sDriver;

if (!SQLGetInstalledDrivers(szBuf, cbBufMax,&cbBufOut))
return "";

do
{
if (0 != strstr(pszBuf, "Excel"))
{
sDriver = CString(pszBuf);
break;
}
pszBuf = strchr(pszBuf, '\0')+1;
} while ('\0' != pszBuf[1]);

return sDriver;
}

友情链接
版权所有 Copyright(c)2004-2015 锐英源软件
公司注册号:410105000449586 豫ICP备08007559号 最佳分辨率 1024*768
地址:郑州市文化路47号院1号楼4层(47-1楼位于文化路和红专路十字路口东北角,郑州大学工学院招待所南边,工学院科技报告厅西边。)