📄 datawin.cpp
字号:
// SRCCOPY);//保存
nWidth = rtAll.Width()-xScroll; //画背景
nHeight= rtAll.Height()-yScroll;
if(rtClient.Width()>nWidth)
{//因为移动方法不同,所以横向不会有多余部份
rtFree = rtClient;
rtFree.left = rtFree.right - (rtClient.Width()-nWidth);
dc.FillRect(&rtFree, &m_bkBrush);
}
if(rtClient.Height()>nHeight)
{
rtFree = rtClient;
rtFree.top = rtFree.bottom - (rtClient.Height()-nHeight);
CBrush Brush;
Brush.CreateSolidBrush(m_shadowColor);
dc.FillRect(&rtFree, &Brush);
Brush.DeleteObject();
}
return 2;
}//end switch;
return 0;
}
///////////////////////////////////////////////////////////
//绘页尾
void CDataWin::DrawFoot(CDC &dc, int *nRow, BOOL bIsPrint)
{
//画页尾
CRect rect, rect2;
CDataCell data;
int i, row;
int xP = dc.GetDeviceCaps(LOGPIXELSX);
int yP = dc.GetDeviceCaps(LOGPIXELSY);
DOUBLE xPix = (DOUBLE)xP*10/254; //每 mm 宽度的像素
DOUBLE yPix = (DOUBLE)yP*10/254; //每 mm 高度的像素
DOUBLE xPixChange = xPix/m_xPix; //相对象数转换
DOUBLE yPixChange = yPix/m_yPix; //相对象数转换
/* DOUBLE xPixChange = 1; //相对象数转换
DOUBLE yPixChange = 1; //相对象数转换
DOUBLE xPix = m_xPix;
DOUBLE yPix = m_yPix;
*/
int nHeight = m_nCellHeight; //表体行高
if(bIsPrint)
rect.SetRect(0,0,m_RectPrint.Width()*xPix, m_RectPrint.Height()*yPix);
else
rect = m_RectPage;
// rect.SetRect(0,0,m_RectPage.Width()*xPix, m_RectPage.Height()*yPix);
row = (*nRow);
for (i = 0; i < m_dataFoot.size(); i++)
{
data = m_dataFoot.at(i);
data.pDC = &dc;
//////////////////////////////////
// CalMulRect(&data.Rect, xPix, yPix);
// int nHeight = data.Rect.Height();
// data.Rect.bottom = rect.bottom-data.Rect.bottom;
// data.Rect.top = data.Rect.bottom-nHeight;
// data.bIsPrint = TRUE;
// data.xPix = xPixChange; data.yPix=yPixChange;
// OnInitCell(&data, TRUE);
////////////////////////////////
if(bIsPrint || m_bIsPrintView)
{
CalMulRect(&data.Rect, xPix, yPix);
int nHeight = data.Rect.Height();
data.Rect.bottom = rect.bottom-data.Rect.bottom;
data.Rect.top = data.Rect.bottom-nHeight;
}
else
{//非模拟显示
rect2 = data.Rect;
int nHeightMax = m_nHead + row * nHeight + m_nEnd + m_nFoot ;
data.Rect.bottom = nHeightMax-rect2.bottom;
data.Rect.top = data.Rect.bottom-(rect2.bottom-rect2.top);
CalMulRect(&data.Rect,xPix,yPix);
}
data.xPix = xPixChange; data.yPix=yPixChange;
data.bIsPrint = bIsPrint;
OnInitCell(&data);
if(OnCellStart(&data))
m_pGraphic->DrawItem((void*)&data);
OnCellEnd(&data);
}
}
///////////////////////////////////////////////////////////
//绘表尾
void CDataWin::DrawEnd(CDC &dc, int *nRow,BOOL bIsPrint)
{
//坐标转换, 取当前页面象数面积
CDataCell data;
int i, row;
int xP = dc.GetDeviceCaps(LOGPIXELSX);
int yP = dc.GetDeviceCaps(LOGPIXELSY);
DOUBLE xPix = (DOUBLE)xP*10/254; //每 mm 宽度的像素
DOUBLE yPix = (DOUBLE)yP*10/254; //每 mm 高度的像素
DOUBLE xPixChange = xPix/m_xPix; //相对象数转换
DOUBLE yPixChange = yPix/m_yPix; //相对象数转换
/* DOUBLE xPixChange = 1; //相对象数转换
DOUBLE yPixChange = 1; //相对象数转换
DOUBLE xPix = m_xPix;
DOUBLE yPix = m_yPix;
*/
int nHeight = m_nCellHeight; //表体行高
//////////////////////////////////////////////////////////
//画表尾
row = (*nRow);
if(m_nPage == m_nPageMax-1)
{//最后一页
row++;
for (i = 0; i < m_dataEnd.size() ; i++)
{
data = m_dataEnd.at(i);
data.pDC = &dc;
data.nRow = -1;
data.Rect.top += row * nHeight;
data.Rect.bottom += row * nHeight;
CalMulRect(&data.Rect,xPix,yPix);
data.xPix = xPixChange; data.yPix=yPixChange;
data.bIsPrint = bIsPrint;
OnInitCell(&data);
if(OnCellStart(&data))
m_pGraphic->DrawItem((void*)&data);
OnCellEnd(&data);
}//end for
}//end if
*nRow = row;
}
///////////////////////////////////////////////////////////
//绘表体
void CDataWin::DrawBody(CDC &dc, int *nRow, BOOL bIsPrint)
{
//坐标转换, 取当前页面象数面积
CDataCell data;
int i, j, row;
int xP = dc.GetDeviceCaps(LOGPIXELSX);
int yP = dc.GetDeviceCaps(LOGPIXELSY);
DOUBLE xPix = (DOUBLE)xP*10/254; //每 mm 宽度的像素
DOUBLE yPix = (DOUBLE)yP*10/254; //每 mm 高度的像素
DOUBLE xPixChange = xPix/m_xPix; //相对象数转换
DOUBLE yPixChange = yPix/m_yPix; //相对象数转换
/* DOUBLE xPixChange = 1; //相对象数转换
DOUBLE yPixChange = 1; //相对象数转换
DOUBLE xPix = m_xPix;
DOUBLE yPix = m_yPix;
*/
int nHeight = m_nCellHeight; //表体行高
//画表体
row = -1;
// {//i=总项目、j=单页总项目
// for (i =m_nPage*m_nCellMax*m_nCell, j = 0; i < m_data.size() && (j < m_nCellMax*m_nCell || !GetPrintView()); i++, j++)
for (i =0, j = 0; i < m_data.size() && (j < m_nCellMax*m_nCell || !GetPrintView()); i++, j++)
{//列举数据库项目
if(!(j % m_nCell)) //新行
{//输出非数据库项目
row++;
if(row > m_nCellMax) break; //大于最大行数
for (int k = 0; k < m_dataBody.size() ; k++)
{
data = m_dataBody.at(k);
if(m_dataBody.at(k).dwCommand != PNS_DATABASE)
{
data.pDC = &dc;
data.nRow = row;
data.Rect.top += (row) * nHeight;
data.Rect.bottom += (row) * nHeight;
CalMulRect(&data.Rect,xPix,yPix);
data.xPix = xPixChange; data.yPix=yPixChange;
data.bIsPrint = bIsPrint;
OnInitCell(&data);
if(OnCellStart(&data))
m_pGraphic->DrawItem((void*)&data);
OnCellEnd(&data);
}//end if
}//end for
}//end if新行
//输出数据库项目
data = m_data.at(i);
data.pDC = &dc;
data.nRow = row;
data.Rect.top += (row) * nHeight;
data.Rect.bottom += (row) * nHeight;
CalMulRect(&data.Rect,xPix,yPix);
data.xPix = xPixChange; data.yPix=yPixChange;
data.bIsPrint = bIsPrint;
OnInitCell(&data);
if(OnCellStart(&data))
m_pGraphic->DrawItem((void*)&data);
OnCellEnd(&data);
}//end for
*nRow = row;
}
///////////////////////////////////////////////////////////
//绘表体
void CDataWin::DrawBodyPn(CDC &dc, int *nRow, BOOL bIsPrint)
{
//坐标转换, 取当前页面象数面积
CDataCell data;
int i, j, row;
int xP = dc.GetDeviceCaps(LOGPIXELSX);
int yP = dc.GetDeviceCaps(LOGPIXELSY);
DOUBLE xPix = (DOUBLE)xP*10/254; //每 mm 宽度的像素
DOUBLE yPix = (DOUBLE)yP*10/254; //每 mm 高度的像素
DOUBLE xPixChange = xPix/m_xPix; //相对象数转换
DOUBLE yPixChange = yPix/m_yPix; //相对象数转换
/* DOUBLE xPixChange = 1; //相对象数转换
DOUBLE yPixChange = 1; //相对象数转换
DOUBLE xPix = m_xPix;
DOUBLE yPix = m_yPix;
*/
int nHeight = m_nCellHeight; //表体行高
//画表体
row = -1;
// {//i=总项目、j=单页总项目
// for (i =m_nPage*m_nCellMax*m_nCell, j = 0; i < m_data.size() && (j < m_nCellMax*m_nCell || !GetPrintView()); i++, j++)
for (i =0, j = 0; i < m_dataPn.size() && (j < m_nCellMax*m_nCell || !GetPrintView()); i++, j++)
{//列举数据库项目
if(!(j % m_nCell)) //新行
{//输出非数据库项目
row++;
if(row > m_nCellMax) break; //大于最大行数
for (int k = 0; k < m_dataBody.size() ; k++)
{
data = m_dataBody.at(k);
if(m_dataBody.at(k).dwCommand != PNS_DATABASE)
{
data.pDC = &dc;
data.nRow = row;
data.Rect.top += (row) * nHeight;
data.Rect.bottom += (row) * nHeight;
CalMulRect(&data.Rect,xPix,yPix);
data.xPix = xPixChange; data.yPix=yPixChange;
data.bIsPrint = bIsPrint;
OnInitCell(&data);
if(OnCellStart(&data))
m_pGraphic->DrawItem((void*)&data);
OnCellEnd(&data);
}//end if
}//end for
}//end if新行
//输出数据库项目
data = m_data.at(i);
data.pDC = &dc;
data.nRow = row;
data.Rect.top += (row) * nHeight;
data.Rect.bottom += (row) * nHeight;
CalMulRect(&data.Rect,xPix,yPix);
data.xPix = xPixChange; data.yPix=yPixChange;
data.bIsPrint = bIsPrint;
OnInitCell(&data);
if(OnCellStart(&data))
m_pGraphic->DrawItem((void*)&data);
OnCellEnd(&data);
}//end for
*nRow = row;
}
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
//绘表头
void CDataWin::DrawHead(CDC &dc, BOOL bIsPrint)
{
CDataCell data;
//坐标转换, 取当前页面象数面积
int xP = dc.GetDeviceCaps(LOGPIXELSX);
int yP = dc.GetDeviceCaps(LOGPIXELSY);
DOUBLE xPix = (DOUBLE)xP*10/254; //每 mm 宽度的像素
DOUBLE yPix = (DOUBLE)yP*10/254; //每 mm 高度的像素
DOUBLE xPixChange = xPix/m_xPix; //相对象数转换
DOUBLE yPixChange = yPix/m_yPix; //相对象数转换
/*
DOUBLE xPixChange = 1; //相对象数转换
DOUBLE yPixChange = 1; //相对象数转换
DOUBLE xPix = m_xPix;
DOUBLE yPix = m_yPix;
*/
for (int i = 0; i < m_dataHead.size(); i++)
{
data = m_dataHead.at(i);
data.pDC = &dc;
CalMulRect(&data.Rect,xPix,yPix);
data.xPix = xPixChange; data.yPix=yPixChange;
data.bIsPrint = bIsPrint;
OnInitCell(&data);
if(OnCellStart(&data))
m_pGraphic->DrawItem((void*)&data);
OnCellEnd(&data);
}
}
/////////////////////////////////////////////////////////
//画背景
void CDataWin::DrawBack(CDC &dc)
{
CRect rect2, rtClient, rtAll;
CBrush Brush;
//计算滚动条相对位移
int xPos = ::GetScrollPos(GetSafeHwnd(), SB_HORZ);
int yPos = ::GetScrollPos(GetSafeHwnd(), SB_VERT);
int xScroll, yScroll;
GetClientRect(&rtClient);
if(m_bIsPrintView) //模似显示
{
//坐标转换
xScroll = GetScrollH(xPos);
yScroll = GetScrollV(yPos);
dc.SetWindowOrg(xScroll, yScroll);
//画背景
GetPageRect(rtAll);
if(m_nDoubleRam>0) //创建双缓存时,填充整个前景区
{
dc.FillRect(&rtAll, &m_bkBrush); //画背景
}
else
dc.FillRect(&rtClient, &m_bkBrush); //画背景
//画阴影
Brush.CreateSolidBrush(m_shadowColor);
rect2 = rtAll; rect2.bottom=m_nFrameH/2; //上
dc.FillRect(&rect2, &Brush);
rect2 = rtAll; rect2.top = rect2.bottom-m_nFrameH/2; //下
dc.FillRect(&rect2, &Brush);
rect2 = rtAll; rect2.right=m_nFrameW/2; //左
dc.FillRect(&rect2, &Brush);
rect2 = rtAll; rect2.left=rect2.right-m_nFrameW/2; //右
dc.FillRect(&rect2, &Brush);
Brush.DeleteObject();
//画外框
rect2 = m_RectPage;
Brush.CreateSolidBrush(RGB(255,0,0));
rect2.InflateRect(1,1);
rect2.OffsetRect(m_nFrameW-2,m_nFrameH-2);
dc.FrameRect(&rect2, &Brush);
Brush.DeleteObject();
//画内框
rect2.DeflateRect(1,1);
dc.Draw3dRect(&rect2,GetSysColor(COLOR_3DDKSHADOW),
GetSysColor(COLOR_3DHILIGHT));
//位移至框内
dc.SetWindowOrg(-m_nFrameW+xScroll,-m_nFrameH+yScroll);
}
else
{
//画背景
GetPageRect(rtAll);
if(m_nDoubleRam>0) //创建双缓存时,填充整个前景区
dc.FillRect(&rtAll, &m_bkBrush); //画背景
else
dc.FillRect(&rtClient, &m_bkBrush); //画背景
xScroll = GetScrollH(xPos);
yScroll = GetScrollV(yPos);
dc.SetWindowOrg(xScroll, yScroll);
}
}
////////////////////////////////////////////////////
//一点效果都没有:(
void CDataWin::OnPaintBase(CDC &dc)
{
int row;
if(m_RectPage.Width()==0 && m_RectPage.Height()==0)
return; //没有初始化
DrawBack(dc); // 画背景
DrawHead(dc); //画页头
DrawBody(dc, &row); //表体
DrawEnd(dc, &row); //表尾
DrawFoot(dc, &row); //页尾
dc.SetWindowOrg(0,0);
}
////////////////////////////////////////////////////////
//显示
void CDataWin::OnPaint()
{
CPaintDC dc(this); // device context for painting
if(m_pGraphic==NULL || m_bRedraw == false)return;
if(!OnPageStart()) return;
//双缓存绘制
int nReturn = OnPaintDoubleRam(dc);
switch(nReturn)
{
case 0: OnPaintBase(dc);
break;
case 1: OnPaintBase(m_MemDC);
OnPaintDoubleRam(dc);
break;
}
OnPageEnd();
return ;
}
///////////////////////////////////////////////////////
//打印
//sTitle 打印标题 什么都可以
//nPrintDlg 0直接打印 1显示打印窗口 其它:提示是否打印
BOOL CDataWin::OnPrint(char * sTitle,UINT nPrintDlg)
{
CDC MemDC;
int i;
CRect rect;
BOOL bReturn = TRUE;
HDC hdcPrint = NULL;
int xP, yP;
DOUBLE xPix, yPix;
if(nPrintDlg == 0)
{ //直接打印
if(m_strPrinterName.IsEmpty())
hdcPrint = m_Print.GetDefaultPrinterDC();
else
hdcPrint = m_Print.GetPrinterDC((LPTSTR)(LPCSTR)m_strPrinterName);
}
else if(nPrintDlg == 1)
{ //显示选择窗口
CPrintDialog PrintDialog(TRUE,PD_ALLPAGES|PD_NOPAGENUMS, NULL);
if(PrintDialog.DoModal() != IDOK) return FALSE;
hdcPrint = PrintDialog.CreatePrinterDC();
}
else
{ //打开确认提示
if(MessageBox("决定打印当前报表吗?", "打印提示", MB_ICONQUESTION | MB_YESNO | MB_DEFBUTTON2) !=IDYES)
return FALSE;
if(m_strPrinterName.IsEmpty())
hdcPrint = m_Print.GetDefaultPrinterDC();
else
hdcPrint = m_Print.GetPrinterDC((LPTSTR)(LPCSTR)m_strPrinterName);
}
//创建打印句柄失败
if(hdcPrint==NULL)return FALSE;
MemDC.Attach(hdcPrint);
// m_bIsPrint = TRUE;
static DOCINFO di = {sizeof (DOCINFO), sTitle} ;
////////////////真正打印开始//////////////////////
//m_RecordSet.SetBookmark(); //记录当前位置
//开始文档打印
if(MemDC.StartDoc(&di)<0)
{
::MessageBox(GetSafeHwnd(), "连接到打印机化败!", "错误", MB_ICONSTOP);
bReturn = FALSE;
goto err01;
}
//坐标转换
xP = GetDeviceCaps(MemDC.m_hDC, LOGPIXELSX); //x方向每英寸像素点数
yP = GetDeviceCaps(MemDC.m_hDC, LOGPIXELSY); //y方向每英寸像素点数
xPix = (DOUBLE)xP*10/254; //每 mm 宽度的像素
yPix = (DOUBLE)yP*10/254; //每 mm 高度的像素
rect.SetRect(0,0,m_RectPrint.Width()*xPix, m_RectPrint.Height()*yPix);
m_RecordSet.MoveFirst(); //移至首行
m_nPagePn=m_nPageMaxPn=0;
CalSize(rect,xPix, yPix, TRUE); //重新计算每页项目数、总页数
if(!OnPrintStart()) //开始打印
{
bReturn = FALSE;
goto err01;
}
for (i = 0; i < m_nPageMaxPn; i++)
{
m_nPagePn = i;
UpdateRecord(true); //装入一页
if(MemDC.StartPage() < 0)
{//开始页
::MessageBox(GetSafeHwnd(), _T("打印失败!"), "错误", MB_ICONSTOP);
MemDC.AbortDoc();
bReturn = FALSE;
goto err01;
}
if(!OnPageStart(TRUE)) //新一页
{
bReturn = FALSE;
break;
}
OnPrintBase(MemDC); //打印输出
OnPageEnd(TRUE); //结束一页
MemDC.EndPage();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -