printbmpview.cpp

来自「Visual C++ 的学习资料一」· C++ 代码 · 共 157 行

CPP
157
字号
// printbmpView.cpp : implementation of the CPrintbmpView class
//

#include "stdafx.h"
#include "printbmp.h"

#include "printbmpDoc.h"
#include "printbmpView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CPrintbmpView

IMPLEMENT_DYNCREATE(CPrintbmpView, CView)

BEGIN_MESSAGE_MAP(CPrintbmpView, CView)
	//{{AFX_MSG_MAP(CPrintbmpView)
		// 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()

/////////////////////////////////////////////////////////////////////////////
// CPrintbmpView construction/destruction

CPrintbmpView::CPrintbmpView()
{
	// TODO: add construction code here

}

CPrintbmpView::~CPrintbmpView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CPrintbmpView drawing

void CPrintbmpView::OnDraw(CDC* pDC)
{
	CPrintbmpDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here	
	screenx =pDC->GetDeviceCaps(LOGPIXELSX);
	screeny =pDC->GetDeviceCaps(LOGPIXELSY);
	BITMAP BitMap;
	CDC DCMem;
	if (!pDoc->GetHandle())
		return;
	//创建内存设备场景
	if (!DCMem.CreateCompatibleDC(pDC))
		TRACE0("DCMem.CreateCompatibleDC failed\n");
	pDoc->SelectBitmap(&DCMem);
	pDoc->GetBitmap(&BitMap);
	//将位图拷贝到显示设备场景中,进行显示
	if (!pDC->BitBlt(0, 0, BitMap.bmWidth, BitMap.bmHeight, &DCMem, 0, 0, SRCCOPY))
		TRACE0("BitBlt failed\n");
	pDoc->SelectOldBitmap(&DCMem);
	DCMem.DeleteDC();
}

/////////////////////////////////////////////////////////////////////////////
// CPrintbmpView printing

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

void CPrintbmpView::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 CPrintbmpView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CPrintbmpView diagnostics

#ifdef _DEBUG
void CPrintbmpView::AssertValid() const
{
	CView::AssertValid();
}

void CPrintbmpView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CPrintbmpView message handlers

void CPrintbmpView::OnPrint(CDC* pDC, CPrintInfo* pInfo) 
{
	// TODO: Add your specialized code here and/or call the base class
	CPrintbmpDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	BITMAP BitMap;
	CDC DCMem;
	if (!pDoc->GetHandle())
		return;
	//创建内存设备场景
	if (!DCMem.CreateCompatibleDC(pDC))
		TRACE0("DCMem.CreateCompatibleDC failed\n");
	pDoc->SelectBitmap(&DCMem);
	pDoc->GetBitmap(&BitMap);
	//将位图拷贝到显示设备场景中,进行显示
	if (!pDC->StretchBlt(0, 0, (int)(ratex*BitMap.bmWidth), (int)(ratey*BitMap.bmHeight), &DCMem,0,0,
			BitMap.bmWidth,BitMap.bmHeight,SRCCOPY))
			TRACE0("BitBlt failed\n");
//	if (!pDC->BitBlt(0, 0, (int)(ratex*BitMap.bmWidth), (int)(ratey*BitMap.bmHeight), &DCMem, 0, 0, SRCCOPY))
//		TRACE0("BitBlt failed\n");
	pDoc->SelectOldBitmap(&DCMem);
	DCMem.DeleteDC();
	//CView::OnPrint(pDC, pInfo);
}

⌨️ 快捷键说明

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