📄 reptabdlg.cpp
字号:
{
arrPageEndIndex.RemoveAll();
for(int i=1;i<=nPageCount;i++)
{
int nRealHeight=nPageHeight-2*nPageInTop;
if(i==1)
{
nRealHeight=nPageHeight-2*nPageInTop-nRowHeight;
arrPageEndIndex.Add(nRealHeight/nRowHeight-1);
}
else
{
int n=nRealHeight/nRowHeight;
arrPageEndIndex.Add(n);
}
}
arrPageEndIndex.SetAt(nPageCount-1,nRowCount-1);
}
//画各页
void CRepTabDlg::DrawPage(CDC* pDC)
{
if(nRowCount<=0) return;
//得到滚动条的位置
int NowHPos=pHScrollbar.GetScrollPos();
int NowVPos=pVScrollbar.GetScrollPos();
CRect ClipRect;
GetClientRect(ClipRect);
ClipRect.top=27;
ClipRect.right-=16;
ClipRect.bottom-=32;
pDC->IntersectClipRect(ClipRect);
int nWidth=4;
for(int i=0;i<nPageCount;i++)
{
CRect DarkRect;
CRect PageRect;
DarkRect.top=27+nTopMargin+i*(nPageHeight+nGap)+nWidth-NowVPos;
DarkRect.left=nLeftMargin+nWidth-NowHPos;
DarkRect.bottom=DarkRect.top+nPageHeight;
DarkRect.right=DarkRect.left+nPageWidth;
PageRect.top=27+nTopMargin+i*(nPageHeight+nGap)-NowVPos;
PageRect.left=nLeftMargin-NowHPos;
PageRect.bottom=PageRect.top+nPageHeight;
PageRect.right=PageRect.left+nPageWidth;
pDC->SelectStockObject(3);
pDC->Rectangle(DarkRect);
pDC->SelectStockObject(0);
pDC->Rectangle(PageRect);
//写页码
CString strPage;
strPage.Format("---%d---",i+1);
int x=PageRect.left+(PageRect.Width()-pDC->GetTextExtent(strPage).cx)/2;
int y=PageRect.bottom-nPageInTop;
CPen* pOldPen;
CPen* pNewPen;
pNewPen=new CPen;
if(pNewPen->CreatePen(PS_SOLID,1,RGB(120,120,120)))
{
pOldPen=pDC->SelectObject(pNewPen);
COLORREF oldcolor=pDC->SetTextColor(RGB(120,120,120));
pDC->TextOut(x,y,strPage);
pDC->SetTextColor(oldcolor);
}
pDC->SelectObject(pOldPen);
delete pNewPen;
}
}
//写各页的内容
void CRepTabDlg::DrawCell(CDC* pDC)
{
int nNowPage=0;//现在正在绘制的面页是第几页
// int nNowPos=nTitleHeight+nPageInTop;//现在正在绘制的页中位置
int nPageBeginY=nRowHeight;//一个页中Y开始的坐标
//得到滚动条的位置
if(nRowCount<=0) return;
int NowHPos=pHScrollbar.GetScrollPos();
int NowVPos=pVScrollbar.GetScrollPos();
////////////////////////////////////
CFont font;
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT)); // zero out structure
lf.lfHeight = 13; // request a 12-pixel-height font
lf.lfCharSet=1;
strcpy(lf.lfFaceName, "宋体"); // request a face name "Arial"
VERIFY(font.CreateFontIndirect(&lf)); // create the font
CFont* def_font = pDC->SelectObject(&font);
//////////////////////////////////////
int nNowPos=nRowHeight+nPageInTop;
//nTabBeginPosY=nTitleHeight;
int nTabBeginPosY=nRowHeight;
int nTWidth=pDC->GetTextExtent(strTitle).cx;
nTWidth=(nPageWidth-nTWidth)/2-nPageInLeft;
pDC->TextOut(nLeftMargin-NowHPos+nPageInLeft+nTWidth,27+nTopMargin-NowVPos+nPageInTop,strTitle);
for(int i=0;i<nRowCount;i++)
{
if(i==0)
{
//画表格数据项的背景
CRect bgrect;
bgrect.left=nLeftMargin-NowHPos+nPageInLeft;
bgrect.top=nNowPage*(nPageHeight+nGap)+27+nTopMargin+nNowPos-NowVPos;
bgrect.right=nLeftMargin-NowHPos-nPageInLeft+nPageWidth;
bgrect.bottom=bgrect.top+nRowHeight;
CBrush* pOldBrush;
CBrush* pNewBrush;
pNewBrush=new CBrush;
if(pNewBrush->CreateSolidBrush(RGB(200,200,200)))
pOldBrush=pDC->SelectObject(pNewBrush);
else
TRACE0("Create the brush error!");
pDC->Rectangle(bgrect);
pDC->SelectObject(pOldBrush);
delete pNewBrush;
}
//画表格的一行的上线
pDC->MoveTo(nLeftMargin-NowHPos+nPageInLeft,nNowPage*(nPageHeight+nGap)+27+nTopMargin+nNowPos-NowVPos);
pDC->LineTo(nLeftMargin-NowHPos-nPageInLeft+nPageWidth,nNowPage*(nPageHeight+nGap)+27+nTopMargin+nNowPos-NowVPos);
pDC->SetBkMode(TRANSPARENT);
for(int j=0;j<nColCount;j++)
{
CRect clipRect;
clipRect.top=nNowPage*(nPageHeight+nGap)+27+nTopMargin+nNowPos-NowVPos;
clipRect.left=nLeftMargin-NowHPos+nPageInLeft+j*(nPageWidth-2*nPageInLeft)/nColCount;
clipRect.right=nLeftMargin-NowHPos+nPageInLeft+(j+1)*(nPageWidth-2*nPageInLeft)/nColCount;
clipRect.bottom=nNowPage*(nPageHeight+nGap)+27+nTopMargin+nNowPos-NowVPos+nRowHeight;
// pDC->IntersectClipRect(clipRect);
CString str;
str=arrTxt.GetAt(i*nColCount+j);
pDC->TextOut(nLeftMargin-NowHPos+nPageInLeft+j*(nPageWidth-2*nPageInLeft)/nColCount+4,nNowPage*(nPageHeight+nGap)+27+nTopMargin+nNowPos-NowVPos+2,str);
}
nNowPos+=nRowHeight;
//当画完一页时需要换页
if(nNowPos+nRowHeight>nPageHeight-nPageInTop)
{
//绘制一页中最下面的那一条表格横线
pDC->MoveTo(nLeftMargin-NowHPos+nPageInLeft,nNowPage*(nPageHeight+nGap)+27+nTopMargin-NowVPos+nNowPos);
pDC->LineTo(nLeftMargin-NowHPos-nPageInLeft+nPageWidth,nNowPage*(nPageHeight+nGap)+27+nTopMargin-NowVPos+nNowPos);
//绘制一页中纵向表格线
for(int k=0;k<nColCount;k++)
{
pDC->MoveTo(nLeftMargin-NowHPos+nPageInLeft+k*(nPageWidth-2*nPageInLeft)/nColCount,nNowPage*(nPageHeight+nGap)+27+nTopMargin-NowVPos+nPageInTop+nPageBeginY);
pDC->LineTo(nLeftMargin-NowHPos+nPageInLeft+k*(nPageWidth-2*nPageInLeft)/nColCount,nNowPage*(nPageHeight+nGap)+27+nTopMargin-NowVPos+nNowPos);
}
pDC->MoveTo(nLeftMargin-NowHPos+nPageInLeft+nPageWidth-2*nPageInLeft,nNowPage*(nPageHeight+nGap)+27+nTopMargin-NowVPos+nPageInTop+nPageBeginY);
pDC->LineTo(nLeftMargin-NowHPos+nPageInLeft+nPageWidth-2*nPageInLeft,nNowPage*(nPageHeight+nGap)+27+nTopMargin-NowVPos+nNowPos);
nPageBeginY=0;
nNowPos=nPageInTop;
nNowPage++;
}
}
////////////////////////////////////////
//绘制最后一页的表格的纵向的线条,当只有一页时也调用些过程
for(int k=0;k<nColCount;k++)
{
pDC->MoveTo(nLeftMargin-NowHPos+nPageInLeft+k*(nPageWidth-2*nPageInLeft)/nColCount,nNowPage*(nPageHeight+nGap)+27+nTopMargin-NowVPos+nPageInTop+nPageBeginY);
pDC->LineTo(nLeftMargin-NowHPos+nPageInLeft+k*(nPageWidth-2*nPageInLeft)/nColCount,nNowPage*(nPageHeight+nGap)+27+nTopMargin-NowVPos+nNowPos);
}
pDC->MoveTo(nLeftMargin-NowHPos+nPageInLeft+nPageWidth-2*nPageInLeft,nNowPage*(nPageHeight+nGap)+27+nTopMargin-NowVPos+nPageInTop+nPageBeginY);
pDC->LineTo(nLeftMargin-NowHPos+nPageInLeft+nPageWidth-2*nPageInLeft,nNowPage*(nPageHeight+nGap)+27+nTopMargin+nNowPos-NowVPos);
//画最后一页的最下面一条表格横线
pDC->MoveTo(nLeftMargin-NowHPos+nPageInLeft,nNowPage*(nPageHeight+nGap)+27+nTopMargin-NowVPos+nNowPos);
pDC->LineTo(nLeftMargin-NowHPos-nPageInLeft+nPageWidth,nNowPage*(nPageHeight+nGap)+27+nTopMargin-NowVPos+nNowPos);
// nTabEndPosY=nNowPos;
pDC->SelectObject(def_font);
pDC->SetBkMode(OPAQUE);
////////////////////////////////////
/*
//得到滚动条的位置
int NowHPos=pHScrollbar.GetScrollPos();
int NowVPos=pVScrollbar.GetScrollPos();
int nBeginX=nLeftMargin-NowHPos;
int nBeginY;
for(int i=1;i<=nPageCount;i++)
{
nBeginY=27+nTopMargin+(i-1)*(nPageHeight+nGap)-NowVPos;
DrawPageContent(pDC,i,i,nBeginX,nBeginY);
}
*/
}
//写一页的内容
void CRepTabDlg::DrawPageContent(CDC* pDC,int nBegin,int nEnd,int nBeginX,int nBeginY)
{
int nBeginIndex;
int nEndIndex;
int nCurPosY;
//创建打印字体
CFont font;
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT)); // zero out structure
lf.lfHeight = 13; // request a 12-pixel-height font
lf.lfCharSet=1;
strcpy(lf.lfFaceName, "宋体"); // request a face name "Arial"
VERIFY(font.CreateFontIndirect(&lf)); // create the font
CFont* def_font = pDC->SelectObject(&font);
nCurPosY=nPageInTop+nBeginY;
//打印各页
// for(int i=nBegin;i<=nEnd;i++)
// {
if(nBegin==1)
{
//打印标题
pDC->TextOut(nBeginX+nPageInLeft+(nPageWidth-2*nPageInLeft-pDC->GetTextExtent(strTitle).cx)/2,nCurPosY,strTitle);
nBeginIndex=0;
nEndIndex=arrPageEndIndex.GetAt(0);
nCurPosY+=nRowHeight;
}
else
{
nBeginIndex=arrPageEndIndex.GetAt(nBegin-2)+1;
nEndIndex=arrPageEndIndex.GetAt(nBegin-1);
}
//打印表格的竖线
for(int n=0;n<=nColCount;n++)
{
pDC->MoveTo(nBeginX+nPageInLeft+n*(nPageWidth-2*nPageInLeft)/nColCount,nCurPosY);
pDC->LineTo(nBeginX+nPageInLeft+n*(nPageWidth-2*nPageInLeft)/nColCount,nCurPosY+(nEndIndex-nBeginIndex+1)*nRowHeight);
}
//画表格的第一条横线
pDC->MoveTo(nBeginX+nPageInLeft,nCurPosY);
pDC->LineTo(nBeginX+nPageWidth-nPageInLeft,nCurPosY);
//打印各行
for(int j=nBeginIndex;j<=nEndIndex;j++)
{
//打印一行中的各列的内容
for(int k=0;k<nColCount;k++)
{
CString str;
if(j*nColCount+k>=arrTxt.GetUpperBound())
str=" ";
else
str=arrTxt.GetAt(j*nColCount+k);
// str=arrTxt.GetAt(j*nColCount+k);
pDC->TextOut(nBeginX+nPageInLeft+k*(nPageWidth-2*nPageInLeft)/nColCount+4,nCurPosY+2,str);
}
nCurPosY+=nRowHeight;
pDC->MoveTo(nBeginX+nPageInLeft,nCurPosY);
pDC->LineTo(nBeginX+nPageWidth-nPageInLeft,nCurPosY);
}
pDC->SelectObject(def_font);
// }
}
//设置滚动条的范围
void CRepTabDlg::SetHVScrollRange()
{
CRect rect;
GetClientRect(&rect);
pVScrollbar.SetScrollRange(0,2*nTopMargin+3*nGap+nPageCount*(nPageHeight+nGap)-rect.Height(),TRUE);
pHScrollbar.SetScrollRange(0,nPageWidth+2*nLeftMargin-rect.Width(),TRUE);
}
void CRepTabDlg::SetLeftMargin()
{
int nScreenWidth=GetSystemMetrics(0);
int nScreenHeight=GetSystemMetrics(1);
if(2*nLeftMargin+nPageWidth<=nScreenWidth)
nLeftMargin=(nScreenWidth-nPageWidth)/2;
else
nLeftMargin=40;
}
void CRepTabDlg::PreParePrinter(CDC* pDC)
{
pDC->SetMapMode(MM_ANISOTROPIC);
//转换坐标映射方式
CSize size = CSize(nPageWidth, nPageHeight);
pDC->SetWindowExt(size);//确定窗口大小
int xExt=pDC->GetDeviceCaps(HORZRES);
int yExt=pDC->GetDeviceCaps(VERTRES);
pDC->SetViewportExt(xExt,yExt);
}
//打印报表
void CRepTabDlg::PrintTabPage(CDC* pDC,int nBegin,int nEnd)
{
int nHorRes=nPageWidth;
int nVerRes=nPageHeight;
int nBeginIndex;
int nEndIndex;
int nCurPosY;
DOCINFO di;
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = "NetReptable Data Printing";
di.lpszOutput = (LPTSTR) NULL;
di.lpszDatatype = (LPTSTR) NULL;
di.fwType = 0;
//创建打印字体
CFont font;
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT)); // zero out structure
lf.lfHeight = 14; // request a 12-pixel-height font
lf.lfCharSet=1;
strcpy(lf.lfFaceName, "宋体"); // request a face name "Arial"
VERIFY(font.CreateFontIndirect(&lf)); // create the font
CFont* def_font = pDC->SelectObject(&font);
pDC->StartDoc(&di);
nCurPosY=nPageInTop;
//打印各页
for(int i=nBegin;i<=nEnd;i++)
{
pDC->StartPage();
if(i==1)
{
//打印标题
pDC->TextOut(nPageInLeft+(nHorRes-2*nPageInLeft-pDC->GetTextExtent(strTitle).cx)/2,nCurPosY,strTitle);
nBeginIndex=0;
nEndIndex=arrPageEndIndex.GetAt(0);
nCurPosY+=nRowHeight;
}
else
{
nBeginIndex=arrPageEndIndex.GetAt(i-2)+1;
nEndIndex=arrPageEndIndex.GetAt(i-1);
}
//打印表格的竖线
for(int n=0;n<=nColCount;n++)
{
pDC->MoveTo(nPageInLeft+n*(nPageWidth-2*nPageInLeft)/nColCount,nCurPosY);
pDC->LineTo(nPageInLeft+n*(nPageWidth-2*nPageInLeft)/nColCount,nCurPosY+(nEndIndex-nBeginIndex+1)*nRowHeight);
}
//画表格的第一条横线
pDC->MoveTo(nPageInLeft,nCurPosY);
pDC->LineTo(nHorRes-nPageInLeft,nCurPosY);
//打印各行
for(int j=nBeginIndex;j<=nEndIndex;j++)
{
//打印一行中的各列的内容
for(int k=0;k<nColCount;k++)
{
CString str;
if(j*nColCount+k>=arrTxt.GetUpperBound())
str=" ";
else
str=arrTxt.GetAt(j*nColCount+k);
pDC->TextOut(nPageInLeft+k*(nPageWidth-2*nPageInLeft)/nColCount+4,nCurPosY+2,str);
}
nCurPosY+=nRowHeight;
pDC->MoveTo(nPageInLeft,nCurPosY);
pDC->LineTo(nHorRes-nPageInLeft,nCurPosY);
}
//打印页码
CString str;
str.Format("--%d--",i);
CFont font1;
LOGFONT lf1;
memset(&lf1, 0, sizeof(LOGFONT)); // zero out structure
lf1.lfHeight = 12; // request a 12-pixel-height font
lf1.lfCharSet=1;
strcpy(lf1.lfFaceName, "宋体"); // request a face name "Arial"
VERIFY(font1.CreateFontIndirect(&lf1));
pDC->SelectObject(&font1);
int ntxt=pDC->GetTextExtent(str).cx;
pDC->TextOut(nPageInLeft+(nHorRes-2*nPageInLeft-ntxt)/2,nVerRes-nPageInTop+7,str);
pDC->SelectObject(def_font);
pDC->EndPage();
}
pDC->EndDoc();
}
//更新状态条显示
void CRepTabDlg::UpdateStatusBar()
{
nNowPagePos=pVScrollbar.GetScrollPos();
CRect mClientRect;
GetClientRect(mClientRect);
nNowPagePos=1+(nNowPagePos+mClientRect.Height()-nTopMargin-2*27)/(nPageHeight+nGap);
if(nNowPagePos>=nPageCount)
nNowPagePos=nPageCount;
CString str;
str.Format("页码:%d/%d,当前是第%d页",nNowPagePos,nPageCount,nNowPagePos);
m_wndStatusBar.SetPaneText(1,str,TRUE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -