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

📄 reptabdlg.cpp

📁 报表制作程序演示
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// RepTabDlg.cpp : implementation file
//

#include "stdafx.h"
#include "test.h"
#include "RepTabDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CRepTabDlg dialog

static UINT indicators[] =
{
	ID_SEPARATOR,
	ID_SEPARATOR,
	ID_SEPARATOR,
};

CRepTabDlg::CRepTabDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CRepTabDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CRepTabDlg)
	//}}AFX_DATA_INIT
	m_bCreated=false;
	nPageWidth=687;
	nPageHeight=971;
	nPageInTop=40;
	nPageInLeft=40;
	nLeftMargin=40;
	nTopMargin=20;
	nGap=20;

	nRowHeight=20;
	nPageCount=1;
//	nRowCount=3;
//	nColCount=5;

	defdeviceset=NULL;
}
CRepTabDlg::~CRepTabDlg()
{
	arrTxt.RemoveAll();
	arrPageEndIndex.RemoveAll();
}


void CRepTabDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRepTabDlg)
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CRepTabDlg, CDialog)
	//{{AFX_MSG_MAP(CRepTabDlg)
	ON_WM_SIZE()
	ON_WM_TIMER()
	ON_WM_PAINT()
	ON_WM_VSCROLL()
	ON_WM_HSCROLL()
	ON_WM_MOUSEWHEEL()
	ON_COMMAND(ID_FIRST, OnFirst)
	ON_COMMAND(ID_LAST, OnLast)
	ON_COMMAND(ID_NEXT, OnNext)
	ON_COMMAND(ID_PRE, OnPre)
	ON_COMMAND(ID_PRINT, OnPrint)
	ON_COMMAND(ID_SETPAGE, OnSetpage)
	ON_COMMAND(ID_REPEXIT, OnRepexit)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRepTabDlg message handlers

void CRepTabDlg::OnSize(UINT nType, int cx, int cy) 
{
	if(!m_bCreated)
		return;
	CDialog::OnSize(nType, cx, cy);
	CRect rect;
	GetClientRect(rect);
	int nWidth=rect.Width();
	int nHeight=rect.Height();
	m_wndStatusBar.MoveWindow(0,nHeight-16,nWidth,16,TRUE);
	pHScrollbar.MoveWindow(0,nHeight-32,nWidth-16,16,TRUE);
	pVScrollbar.MoveWindow(nWidth-16,20,16,nHeight-52,TRUE);

	SetHVScrollRange();
}

BOOL CRepTabDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
	| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
	!m_wndToolBar.LoadToolBar(IDR_TOOLBARDETAILREP))
	{
		TRACE0("Failed to create toolbar\n");
		return false;      // fail to create
	}
	m_wndToolBar.MoveWindow(0,0,410,27,TRUE);
	m_wndToolBar.ShowWindow(SW_SHOW);

	pHScrollbar.Create(SBS_HORZ | SBS_BOTTOMALIGN | WS_CHILD, CRect(5, 5, 100, 30), this, 100);
    pHScrollbar.ShowScrollBar();

	pVScrollbar.Create(SBS_VERT | SBS_RIGHTALIGN | WS_CHILD, CRect(100, 0, 120, 100), this, 200);
    pVScrollbar.ShowScrollBar();

	if (!m_wndStatusBar.Create(this) ||
	!m_wndStatusBar.SetIndicators(indicators,
	  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return FALSE;      // fail to create
	}
	m_wndStatusBar.ShowWindow(SW_SHOW);
	m_wndStatusBar.SetPaneInfo(0,ID_SEPARATOR,2,0);
	m_wndStatusBar.SetPaneInfo(1,ID_SEPARATOR,1,300);

	m_bCreated=true;

	ShowWindow(SW_MAXIMIZE);
	CalPageCount();
	SetLeftMargin();
	SetHVScrollRange();
	UpdateStatusBar();
	CalBeginAndEndIndex();

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CRepTabDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	
		// TODO: Add extra initialization here
	CDialog::OnTimer(nIDEvent);
}

BOOL CRepTabDlg::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
	strTitle="";
	arrTxt.RemoveAll();
	nColCount=0;
	nRowCount=0;
	return CDialog::DestroyWindow();
}

//画背影
void CRepTabDlg::DrawBK(CDC* pDC)
{
	CRect rect;
	GetClientRect(&rect);
	rect.top=27;
	rect.right-=16;
	rect.bottom-=32;
	pDC->SelectStockObject(2);
	pDC->Rectangle(rect);
	pDC->SelectStockObject(0);
}
//更新页
void CRepTabDlg::UpDatePage()
{
	CDC* pDC=GetDC();
//	CalPageCount();
//	CalBeginAndEndIndex();
	UpdateStatusBar();
	DrawBK(pDC);
	DrawPage(pDC);
	DrawCell(pDC);
	
	ReleaseDC(pDC);
}

void CRepTabDlg::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	UpDatePage();
	// Do not call CDialog::OnPaint() for painting messages
}
//计算页数
void CRepTabDlg::CalPageCount()
{
	nPageCount=((nRowCount+1)*nRowHeight%(nPageHeight-2*nPageInTop))>0?(nRowCount+1)*nRowHeight/(nPageHeight-2*nPageInTop)+1:(nRowCount+1)*nRowHeight/(nPageHeight-2*nPageInTop);
//	nPageCount=1+(nRowHeight*nRowCount+nRowHeight)/(nPageHeight-2*nPageInTop);
}
//计算各页结束的行数
void CRepTabDlg::CalBeginAndEndIndex()
{
	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;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -