⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reptabdlg.cpp

📁 报表制作程序演示
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	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::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
	if(pScrollBar==&pVScrollbar)
	{
		switch(nSBCode)
		{
		case SB_THUMBPOSITION:
			pVScrollbar.SetScrollPos(nPos);
		    break;
		case SB_LINEUP:
			pVScrollbar.SetScrollPos(pVScrollbar.GetScrollPos()-5);
			break;
		case SB_LINEDOWN:
			pVScrollbar.SetScrollPos(pVScrollbar.GetScrollPos()+5);
			break;	
		case SB_PAGEDOWN:
			pVScrollbar.SetScrollPos(pVScrollbar.GetScrollPos()+40);
			break;
		case SB_PAGEUP:
			pVScrollbar.SetScrollPos(pVScrollbar.GetScrollPos()-40);
			break;
		}
		UpDatePage();
	}
	CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
}

void CRepTabDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
	if(pScrollBar==&pHScrollbar)
	{
		switch(nSBCode)
		{
		case SB_THUMBPOSITION:
			pHScrollbar.SetScrollPos(nPos);
		    break;
		case SB_LINEUP:
			pHScrollbar.SetScrollPos(pHScrollbar.GetScrollPos()-40);
			break;
		case SB_LINEDOWN:
			pHScrollbar.SetScrollPos(pHScrollbar.GetScrollPos()+40);
			break;
		case SB_LEFT:
			pHScrollbar.SetScrollPos(pHScrollbar.GetScrollPos()-10);
			break;
		case SB_RIGHT:
			pHScrollbar.SetScrollPos(pHScrollbar.GetScrollPos()+10);
			break;
		case SB_PAGERIGHT:
			pHScrollbar.SetScrollPos(pHScrollbar.GetScrollPos()+20);
			break;
		case SB_PAGELEFT:
			pHScrollbar.SetScrollPos(pHScrollbar.GetScrollPos()-20);
			break;
		}
		UpDatePage();
	}
	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

BOOL CRepTabDlg::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt) 
{
	// TODO: Add your message handler code here and/or call default
	pVScrollbar.SetScrollPos(pVScrollbar.GetScrollPos()-zDelta);
	UpDatePage();
	return CDialog::OnMouseWheel(nFlags, zDelta, pt);
}
//移到第一页
void CRepTabDlg::OnFirst() 
{
	// TODO: Add your command handler code here
	CRect rect;
	GetClientRect(rect);
	int nMin,nMax;
	pVScrollbar.GetScrollRange(&nMin,&nMax);
	pVScrollbar.SetScrollPos(nMax-nPageHeight-2*nTopMargin-27+rect.Height());
	UpDatePage();
}

void CRepTabDlg::OnLast() 
{
	// TODO: Add your command handler code here
	CRect rect;
	GetClientRect(rect);
	int nMin,nMax;
	pVScrollbar.GetScrollRange(&nMin,&nMax);
	pVScrollbar.SetScrollPos(nMax-nPageHeight-2*nTopMargin-27+rect.Height());
	UpDatePage();
}

void CRepTabDlg::OnNext() 
{
	// TODO: Add your command handler code here
	if(nNowPagePos>=nPageCount) return;
	CRect rect;
	GetClientRect(rect);
	pVScrollbar.SetScrollPos(nNowPagePos*(nPageHeight+nGap)+27+nTopMargin);
	nNowPagePos++;
	UpDatePage();
}
//上一页
void CRepTabDlg::OnPre() 
{
	// TODO: Add your command handler code here
	CRect rect;
	GetClientRect(rect);
	nNowPagePos--;
	pVScrollbar.SetScrollPos(nNowPagePos*(nPageHeight+nGap)+27+nTopMargin-(nPageHeight+nGap));
	UpDatePage();
}

void CRepTabDlg::OnPrint() 
{
	// TODO: Add your command handler code here
	CPrintDialog dlg(FALSE, PD_SELECTION | PD_USEDEVMODECOPIES);
	if(defdeviceset!=NULL)
		dlg.m_pd.hDevMode=defdeviceset;
	else
	{
		PRINTDLG pd;
		ZeroMemory(&pd, sizeof(PRINTDLG));
		AfxGetApp()->GetPrinterDeviceDefaults(&pd);

		DEVMODE* pDV = (LPDEVMODE) ::GlobalLock(pd.hDevMode);
		pDV->dmPaperSize=DMPAPER_B5;
		GlobalUnlock(pd.hDevMode);
		dlg.m_pd.hDevMode=pDV;
	}
	dlg.m_pd.nCopies=1;
	dlg.m_pd.nMaxPage=nPageCount;
	dlg.m_pd.nMinPage=1;
	dlg.m_pd.nFromPage=1;
	dlg.m_pd.nToPage=nPageCount;
	if(dlg.DoModal()==IDOK)
	{
		int nBeginPage,nEndPage;
		if (dlg.PrintAll())              // print all pages in the document
		{
			nBeginPage = dlg.m_pd.nMinPage;
			nEndPage = dlg.m_pd.nMaxPage;
		}
		else if (dlg.PrintRange())       // print only a range of pages 
		{                                // in the document 
			nBeginPage = dlg.GetFromPage();
			nEndPage = dlg.GetToPage();
		}
		else if (dlg.PrintSelection())   // print only the currently selected 
		{
			nBeginPage = nEndPage = 1;     // -1 to denote unknown yet
		}


		HDC hdc = dlg.GetPrinterDC();
		CDC* pDC=new CDC();
		pDC->Attach(hdc);

		PreParePrinter(pDC);
		PrintTabPage(pDC,nBeginPage,nEndPage);
		
		pDC->Detach();

		delete pDC;
	}
}

void CRepTabDlg::OnSetpage() 
{
	// TODO: Add your command handler code here
	CPageSetupDialog dlg(PSD_MARGINS);
	if(defdeviceset!=NULL)
		dlg.m_psd.hDevMode=defdeviceset;
	else
	{
		PRINTDLG pd;
		ZeroMemory(&pd, sizeof(PRINTDLG));
		AfxGetApp()->GetPrinterDeviceDefaults(&pd);

		DEVMODE* pDV = (LPDEVMODE) ::GlobalLock(pd.hDevMode);
		pDV->dmPaperSize=DMPAPER_B5;
		GlobalUnlock(pd.hDevMode);
		dlg.m_psd.hDevMode=pDV;
	}

	if(dlg.DoModal()==IDOK)
	{
		CSize size = dlg.GetPaperSize();
		nPageWidth=size.cx*37.7928949357521/1000;
		nPageHeight=size.cy*37.7928949357521/1000;

		SetLeftMargin();
		CalPageCount();
		SetHVScrollRange();
		CalBeginAndEndIndex();
		UpDatePage();

		defdeviceset=dlg.GetDevMode();
	}
}
//
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::OnRepexit() 
{
	// TODO: Add your command handler code here
	CWnd* pWnd=AfxGetApp()->GetMainWnd()->GetDesktopWindow()->GetActiveWindow();
	pWnd->PostMessage(WM_CLOSE);
}
//准备打印机上下文
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 + -