scaleprintview.cpp
来自「Visual C++ 的学习资料一」· C++ 代码 · 共 221 行
CPP
221 行
// scaleprintView.cpp : implementation of the CScaleprintView class
//
#include "stdafx.h"
#include "scaleprint.h"
#include "scaleprintDoc.h"
#include "scaleprintView.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CScaleprintView
IMPLEMENT_DYNCREATE(CScaleprintView, CView)
BEGIN_MESSAGE_MAP(CScaleprintView, CView)
//{{AFX_MSG_MAP(CScaleprintView)
// 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, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CScaleprintView construction/destruction
CScaleprintView::CScaleprintView()
{
// TODO: add construction code here
}
CScaleprintView::~CScaleprintView()
{
}
BOOL CScaleprintView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CScaleprintView drawing
void CScaleprintView::OnDraw(CDC* pDC)
{
CScaleprintDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
HANSHU();
titlefont.CreatePointFont(200,"宋体",pDC);
bodyfont.CreatePointFont(120,"宋体",pDC);
screenx =pDC->GetDeviceCaps(LOGPIXELSX);
screeny =pDC->GetDeviceCaps(LOGPIXELSY);
CRect rect;
GetClientRect(rect);
rect.DeflateRect(0,20,0,0);
pDC->SelectObject(&titlefont);
pDC->DrawText("商品销售排行",rect,DT_CENTER);
pDC->SelectObject(&bodyfont);
CRect m_rect(rect);
CRect temprect(m_rect.left+30,m_rect.top+100,10+(m_rect.Width())/4,m_rect.bottom);
CRect itemrect;
int width = temprect.Width();
for (int i = 0;i<4;i++)
{
pDC->DrawText(merchandise[i][0],temprect,DT_LEFT);
itemrect.CopyRect(temprect);
for (int y = 1; y< 5;y++)
{
itemrect.DeflateRect(0,50);
pDC->DrawText(merchandise[i][y],itemrect,DT_LEFT);
}
temprect.DeflateRect(width,0,0,0);
temprect.InflateRect(0,0,width,0);
}
titlefont.DeleteObject();
bodyfont.DeleteObject();
}
/////////////////////////////////////////////////////////////////////////////
// CScaleprintView printing
BOOL CScaleprintView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CScaleprintView::OnBeginPrinting(CDC* pDC, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
printx =pDC->GetDeviceCaps(LOGPIXELSX);
printy =pDC->GetDeviceCaps(LOGPIXELSY);
ratex = (double)printx /screenx; //确定打印机与屏幕的比率
ratey = (double)printy /screeny;
pageheight =pDC->GetDeviceCaps(VERTRES);
pagewidth =pDC->GetDeviceCaps(HORZRES);
//获取打印机的左右边距
leftmargin = pDC->GetDeviceCaps(PHYSICALOFFSETX);//获取左边距
int phywidth= pDC->GetDeviceCaps(PHYSICALWIDTH);
rightmargin = phywidth-pagewidth-leftmargin;
}
void CScaleprintView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CScaleprintView diagnostics
#ifdef _DEBUG
void CScaleprintView::AssertValid() const
{
CView::AssertValid();
}
void CScaleprintView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CScaleprintDoc* CScaleprintView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CScaleprintDoc)));
return (CScaleprintDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CScaleprintView message handlers
void CScaleprintView::HANSHU()
{
merchandise[0][0]= "商品编号";
merchandise[1][0]= "商品名称";
merchandise[2][0]= "数量";
merchandise[3][0]= "单价";
merchandise[0][1]= "S001";
merchandise[0][2]= "S002";
merchandise[0][3]= "S003";
merchandise[0][4]= "S004";
merchandise[1][1]= "手机";
merchandise[1][2]= "电视";
merchandise[1][3]= "空调";
merchandise[1][4]= "电脑";
merchandise[2][1]= "200";
merchandise[2][2]= "300";
merchandise[2][3]= "400";
merchandise[2][4]= "500";
merchandise[3][1]= "1200";
merchandise[3][2]= "2500";
merchandise[3][3]= "3200";
merchandise[3][4]= "5000";
}
void CScaleprintView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: Add your specialized code here and/or call the base class
CMainFrame *p=(CMainFrame*)AfxGetMainWnd();
CString str;
p->edit.GetWindowText(str);
int n;
n = atoi(str);
HANSHU();
CRect rect(-leftmargin,0,pagewidth+rightmargin,pageheight);
titlefont.CreatePointFont(200*n,"宋体",pDC);
bodyfont.CreatePointFont(120*n,"宋体",pDC);
rect.DeflateRect(0,(int)(ratey*20),0,0);
pDC->SelectObject(&titlefont);
pDC->StartDoc("printinformation");
pDC->DrawText("商品销售排行",rect,DT_CENTER);
pDC->SelectObject(&bodyfont);
CRect m_rect(rect);
CRect temprect(m_rect.left+(int)(30*ratex),m_rect.top+(int)(100*ratey),(int)(ratey*10)+(m_rect.Width())/4,m_rect.bottom);
CRect itemrect;
int width = temprect.Width();
for (int i = 0;i<4;i++)
{
pDC->DrawText(merchandise[i][0],temprect,DT_LEFT);
itemrect.CopyRect(temprect);
for (int y = 1; y< 5;y++)
{
itemrect.DeflateRect(0,(int)(ratey*50));
pDC->DrawText(merchandise[i][y],itemrect,DT_LEFT);
}
temprect.DeflateRect(width,0,0,0);
temprect.InflateRect(0,0,width,0);
}
pDC->EndDoc();
titlefont.DeleteObject();
bodyfont.DeleteObject();
//CView::OnPrint(pDC, pInfo);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?