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

📄 groupreportview.cpp

📁 < Visual C++数据库经典开发实例精解>>的实例源码
💻 CPP
字号:
// GroupReportView.cpp : implementation of the CGroupReportView class
//

#include "stdafx.h"
#include "GroupReport.h"

#include "GroupReportDoc.h"
#include "GroupReportView.h"


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

/////////////////////////////////////////////////////////////////////////////
// CGroupReportView

IMPLEMENT_DYNCREATE(CGroupReportView, CScrollView)

BEGIN_MESSAGE_MAP(CGroupReportView, CScrollView)
	//{{AFX_MSG_MAP(CGroupReportView)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGroupReportView construction/destruction

CGroupReportView::CGroupReportView()
{
	// TODO: add construction code here
	m_pSet=NULL;
	classname="";
}

CGroupReportView::~CGroupReportView()
{
	if(m_pSet)
		delete m_pSet;
}

BOOL CGroupReportView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CScrollView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CGroupReportView drawing

void CGroupReportView::OnDraw(CDC* pDC)
{
	PrintReport(pDC);
}

void CGroupReportView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();
	m_pSet=new CStudentSet(NULL);
	m_pSet->m_strSort = "classid";
	m_pSet->Open();
	CSize sizeTotal(29000,21000);
	SetScrollSizes(MM_LOMETRIC,sizeTotal);//设置滚动屏幕大小
}

/////////////////////////////////////////////////////////////////////////////
// CGroupReportView printing

BOOL CGroupReportView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CGroupReportView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CGroupReportView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CGroupReportView diagnostics

#ifdef _DEBUG
void CGroupReportView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CGroupReportView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

CGroupReportDoc* CGroupReportView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGroupReportDoc)));
	return (CGroupReportDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CGroupReportView message handlers

void CGroupReportView::OnPrint(CDC* pDC, CPrintInfo* pInfo) 
{
	PrintReport(pDC, pInfo);
}
void CGroupReportView::PrintReport(CDC* pDC, CPrintInfo* pInfo) 
{
	CString str;//用于将数值数据转换为字符串,以便使用TextOut函数输出
	CTime time = CTime::GetCurrentTime();//在打印报表时用于在页脚输出日期
	int nLineHeight=0;//用于计算每行数据占用高度
	int y=0;//用于控制TextOut函数中输出位置的y轴坐标

	CFont ftitle/*报表标题字体*/,fdetail/*数据字体*/;
	CFont fheader/*字段标题字体*/,ffooter/*报表页脚字体*/;
	//创建报表各部分使用的字体
	ftitle.CreateFont(100,0,0,0,FW_BOLD,0,0,0,
					DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
					DEFAULT_QUALITY,DEFAULT_PITCH | FF_ROMAN,"楷体_GB2312");		
	fheader.CreateFont(60,0,0,0,FW_SEMIBOLD,0,1,0,
					DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
					DEFAULT_QUALITY,DEFAULT_PITCH | FF_ROMAN,"楷体_GB2312");		
	fdetail.CreateFont(50,0,0,0,FW_NORMAL,0,0,0,
					DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
					DEFAULT_QUALITY,DEFAULT_PITCH | FF_ROMAN,"楷体_GB2312");
	ffooter.CreateFont(40,0,0,0,FW_NORMAL,0,0,0,
					DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
					DEFAULT_QUALITY,DEFAULT_PITCH | FF_ROMAN,"楷体_GB2312");
	
    if (m_pSet->IsBOF()){
		return;//若记录集没有数据,则直接返回
	}
	//如果是打印第一页或在窗体显示数据,则刷新记录集
	//这样可从第一条记录开始输出,否则继续接着记录集当前记录输出
	if (!pInfo || pInfo->m_nCurPage==1){		
		m_pSet->Requery();//刷新记录集,第一条记录自动成为当前记录输出
	}	

	pDC->SelectObject(&ftitle);//设置报表标题字体
	pDC->TextOut(250,0,"班级学生名单报表");//输出报表标题	
	/*
	TEXTMETRIC tm;
	//输出数据库表StudentInfo数据
    while (!m_pSet->IsEOF()) 
	{
		if (pInfo && abs(y)>2700){
			//如果是打印或打印预览,不是在窗体中显示数据
			//则在一页打满时增加页数,跳出记录集搜索循环
			pInfo->SetMaxPage(pInfo->m_nCurPage + 1) ;
			break;//记录集搜索循环,停止打印记录数据
		}
		if(classname!=m_pSet->m_classname)
		{//输出新的班级名称
			pDC->GetTextMetrics(&tm);
			nLineHeight=tm.tmHeight+tm.tmExternalLeading;//获取行高
			y = -nLineHeight-20;//计算下一行记录输出的y轴坐标
			pDC->SelectObject(&fheader);//设置字段标题字体
			pDC->TextOut(100,y,m_pSet->m_classname);//输出班级名称
			classname=m_pSet->m_classname;
			y-=nLineHeight;//计算下一行数据输出的y轴坐标
		}
		//pDC->GetTextMetrics(&tm);
		//nLineHeight=tm.tmHeight+tm.tmExternalLeading;//获取新字体下的行高
		//y-=nLineHeight+20;//计算第一行记录输出的y轴坐标

		pDC->SelectObject(&fdetail);//设置记录数据显示字体
		pDC->GetTextMetrics(&tm);
		nLineHeight=tm.tmHeight+tm.tmExternalLeading;//获取新字体下的行高

		pDC->TextOut(300,y, m_pSet->m_stuid);//输出学号
		pDC->TextOut(450,y, m_pSet->m_stuname);//输出姓名
		
		y-=nLineHeight;//计算下一行数据输出的y轴坐标
		m_pSet->MoveNext();//使下一条记录成为当前记录		
    }
	if(pInfo){
		//打印报表页脚
		pDC->SelectObject(&ffooter);				
		str.Format("学生信息报表 第 %d 页 %d年%d月%d日",
			pInfo->m_nCurPage,			
			time.GetYear(),time.GetMonth(),time.GetDay());
		pDC->TextOut(650,-2809,str);
	}	*/
}

⌨️ 快捷键说明

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