📄 odbcreportview.cpp
字号:
// ODBCReportView.cpp : implementation of the CODBCReportView class
//
#include "stdafx.h"
#include "ODBCReport.h"
#include "StudentSet.h"
#include "ODBCReportDoc.h"
#include "ODBCReportView.h"
#include <afx.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CODBCReportView
IMPLEMENT_DYNCREATE(CODBCReportView, CScrollView)
BEGIN_MESSAGE_MAP(CODBCReportView, CScrollView)
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()
/////////////////////////////////////////////////////////////////////////////
// CODBCReportView construction/destruction
CODBCReportView::CODBCReportView()
{
// TODO: add construction code here
}
CODBCReportView::~CODBCReportView()
{
}
BOOL CODBCReportView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CScrollView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CODBCReportView drawing
void CODBCReportView::OnDraw(CDC* pDC)
{
PrintReport(pDC);
}
void CODBCReportView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
CSize sizeTotal(29000,21000);
SetScrollSizes(MM_LOMETRIC,sizeTotal);//设置滚动屏幕大小
m_pSet = &GetDocument()->m_studentset; //指向记录集
m_pSet->m_strFilter="";//设置筛选条件
if (m_pSet->IsOpen()) {
m_pSet->Close();//若记录集已打开,则将其关闭
}
m_pSet->Open();//打开记录集
}
/////////////////////////////////////////////////////////////////////////////
// CODBCReportView printing
BOOL CODBCReportView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CODBCReportView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CODBCReportView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CODBCReportView diagnostics
#ifdef _DEBUG
void CODBCReportView::AssertValid() const
{
CScrollView::AssertValid();
}
void CODBCReportView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CODBCReportDoc* CODBCReportView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CODBCReportDoc)));
return (CODBCReportDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CODBCReportView message handlers
void CODBCReportView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
PrintReport(pDC, pInfo);
}
void CODBCReportView::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(650,0,"学生信息报表");//输出报表标题
TEXTMETRIC tm;
pDC->GetTextMetrics(&tm);
nLineHeight=tm.tmHeight+tm.tmExternalLeading;//获取行高
y = -nLineHeight-20;//计算字段标题输出的y轴坐标
pDC->SelectObject(&fheader);//设置字段标题字体
//输出字段标题,可根据实际页面情况调整输出的x轴坐标
pDC->TextOut(100,y,"学号");
pDC->TextOut(450,y,"姓名");
pDC->TextOut(710,y,"年龄");
pDC->TextOut(900,y,"性别");
pDC->TextOut(1100,y,"电子邮件");
pDC->GetTextMetrics(&tm);
nLineHeight=tm.tmHeight+tm.tmExternalLeading;//获取新字体下的行高
y-=nLineHeight+20;//计算第一行记录输出的y轴坐标
pDC->SelectObject(&fdetail);//设置记录数据显示字体
pDC->GetTextMetrics(&tm);
nLineHeight=tm.tmHeight+tm.tmExternalLeading;//获取新字体下的行高
//输出数据库表StudentInfo数据
while (!m_pSet->IsEOF())
{
if (pInfo && abs(y)>2700){
//如果是打印或打印预览,不是在窗体中显示数据
//则在一页打满时增加页数,跳出记录集搜索循环
pInfo->SetMaxPage(pInfo->m_nCurPage + 1) ;
break;//记录集搜索循环,停止打印记录数据
}
pDC->TextOut(100,y, m_pSet->m_id);//输出学号
pDC->TextOut(450,y, m_pSet->m_name);//输出姓名
str.Format("%d",m_pSet->m_age);//将年龄数值转换为字符串
pDC->TextOut(760,y, str);//输出年龄
pDC->TextOut(950,y, m_pSet->m_sex);//输出数值
pDC->TextOut(1100,y, m_pSet->m_email);
m_pSet->MoveNext();//使下一条记录成为当前记录
y-=nLineHeight;//计算下一行数据输出的y轴坐标
}
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);
}
}
BOOL CODBCReportView::OnDrop(COleDataObject* pDataObject, DROPEFFECT dropEffect, CPoint point)
{
// TODO: Add your specialized code here and/or call the base class
return CScrollView::OnDrop(pDataObject, dropEffect, point);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -