printerinfoex.cpp

来自「通过手机数据线连接手机」· C++ 代码 · 共 110 行

CPP
110
字号
// PrinterInfoEx.cpp: implementation of the CPrinterInfoEx class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "PrinterInfoEx.h"

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

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

CPrinterInfoEx::CPrinterInfoEx()
{
	m_pDC = NULL;
}

CPrinterInfoEx::~CPrinterInfoEx()
{
}

BOOL CPrinterInfoEx::Init( CDC * pDC )
{
	m_pDC = NULL;
	m_continueNextPage = FALSE;

	if( pDC == NULL )
	{
		return FALSE;
	}

	if( !pDC->IsPrinting() )
	{
		return FALSE;
	}

	m_pDC = pDC;

	// 1米 = 39.37008英寸
	m_xResolution = long(pDC->GetDeviceCaps( LOGPIXELSX ) * 39.37008);
	m_yResolution = long(pDC->GetDeviceCaps( LOGPIXELSY ) * 39.37008);

	if( m_xResolution <= 0 )
	{
		m_xResolution = long(300 * 39.37008);
	}
	if( m_yResolution <= 0 )
	{
		m_xResolution = m_xResolution;
	}
	
	m_paperWidth = pDC->GetDeviceCaps( HORZSIZE ) * 10;
	m_paperHeight = pDC->GetDeviceCaps( VERTSIZE ) * 10;
	
	m_widthPixel = pDC->GetDeviceCaps( HORZRES );
	m_heightPixel = pDC->GetDeviceCaps( VERTRES );

	m_printRect = CRect( 0, 0, m_widthPixel, m_heightPixel );

	return TRUE;
}

void CPrinterInfoEx::ConvertPixelToReal(long & x, long & y)
{
	ASSERT( m_pDC != NULL );

double piX = x;
double piY = y;

	//注意:resolution单位是点/米
	piX *= 10000;
	x = long(piX / m_xResolution);
	piY *= 10000;
	y = long(piY / m_yResolution);
}

void CPrinterInfoEx::ConvertRealToPixel(long & x, long & y)
{
	ASSERT( m_pDC != NULL );
double realX = x;
double realY = y;

	//注意:resolution单位是点/米
	realX /= 10000;
	x = long(realX * m_xResolution);
	realY /= 10000;
	y = long(realY * m_yResolution);
}

BOOL CPrinterInfoEx::CanPrint()
{
	if( m_pDC == NULL )
	{
		return FALSE;
	}

	if( m_printRect.top >= m_printRect.bottom
	 || m_printRect.left >= m_printRect.right )
	{//无可用打印范围
		return FALSE;
	}

	return TRUE;
}

⌨️ 快捷键说明

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