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

📄 myprint.cpp

📁 该程序利用在vc环境下
💻 CPP
字号:
// MyPrint.cpp: implementation of the MyPrint class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MyPrint.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

MyPrint::MyPrint()
{
	m_minpage = 1;
	m_maxpage = 1;
	m_curpage = 1;
	pos_x = 200;
	pos_y = 400;
	cur_x = 200;
	cur_y = 400;
}

MyPrint::~MyPrint()
{
	m_font.Detach();
	printdc.Detach(); 
	::DeleteDC(printdc); 
}

void MyPrint::SetFont(int height,int weight,bool bItalic,bool bUnderline,CString font)
{
	m_font.CreateFont(height,0,0,0,weight,bItalic,bUnderline,0,DEFAULT_CHARSET,0,0,0,0,font);
}

bool MyPrint::InitPrint(int itemy)
{
	m_itemy = itemy;
	CPrintDialog printDlg(FALSE);
	//利用CPrintDialog 生成打印机设备环境
	if (printDlg.DoModal() == IDCANCEL)	return false;
	// 连接到dc上
	printdc.Attach(printDlg.CreatePrinterDC());
	printdc.m_bPrinting = TRUE;
	printdc.SetMapMode(MM_LOMETRIC);
	// 设置字体
	oldfont = printdc.SelectObject(&m_font);
	// 计算字体
	nHorRes = printdc.GetDeviceCaps(HORZRES);	
	nVerRes = printdc.GetDeviceCaps(VERTRES);
	TEXTMETRIC tm;
	printdc.GetTextMetrics(&tm);
	nCharHeight = tm.tmHeight;
	nCharWidth = tm.tmAveCharWidth;
	pageend_y = nVerRes - 150;
	// 设置打印作业
	CString strTitle;
	strTitle.LoadString(AFX_IDS_APP_TITLE);
	// 打印作业的定义信息
	::ZeroMemory (&di, sizeof (DOCINFO));
	di.cbSize = sizeof (DOCINFO);
	di.lpszDocName = strTitle;

	return true;
}

BOOL MyPrint::StratPrint(void)
{
	BOOL bPrintingOK = printdc.StartDoc(&di);
	bPrintingOK &= printdc.StartPage(); 
	return bPrintingOK;
}

void MyPrint::EndPrint(void)
{
	CString str;

	str.Format("-- 完 --",m_curpage);
	int startx = 40 * str.GetLength();

	printdc.SelectObject(oldfont);
	printdc.TextOut(nHorRes - startx,nVerRes - 70 ,str);

	printdc.EndPage(); 
	printdc.EndDoc(); 
}

void MyPrint::PrintStr(CString str)
{
	printdc.SelectObject(&m_font);
	printdc.TextOut(cur_x,cur_y,str);
	cur_y += nCharHeight + m_itemy;
	if(cur_y >= pageend_y)
	{
		Nextpage();
	}
}

void MyPrint::PrintHead(CString str)
{
	printdc.SelectObject(&m_font);
	printdc.TextOut(100,50,str);
}

void MyPrint::Nextpage(void)
{
	CString str;

	str.Format("-- 第 %d 页 --",m_curpage);
	int startx = 40 * str.GetLength();

	printdc.SelectObject(oldfont);
	printdc.TextOut(nHorRes - startx,nVerRes - 70 ,str);
	printdc.EndPage();
	printdc.StartPage();
	cur_x = pos_x;
	cur_y = pos_y;
	m_curpage++;
}

void MyPrint::SetPosX(int x)
{
	pos_x = x;
}

void MyPrint::SetPosY(int y)
{
	pos_y = y;
}

void MyPrint::SetCurX(int x)
{
	cur_x = x;
}

void MyPrint::SetCurY(int y)
{
	cur_y = y;
}

⌨️ 快捷键说明

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