当前位置:C++技术网 > 资讯 > MFC打印学习之打印输出方向控制实例

MFC打印学习之打印输出方向控制实例

更新时间:2016-01-26 21:50:16浏览次数:1+次

在开发具有打印功能得应用程序时,有时因为打印的数据太大导致正常打印时无法打印出完整的信息,要解决这个问题,可以在打印时设置横向打印,这样就可以增加水平方向的空间。
在对话框上添加两个按钮控件-横向控件,纵向控件,并建立消息响应,ID号分别为:IDC_BUTHORZRES,IDC_BUTVERTERS。

在对话框类的实现文件中,写代码:

void COrientationDlg::SetPrint(BOOL isway)
{
	DWORD dwflags=PD_ALLPAGES | PD_NOPAGENUMS | PD_USEDEVMODECOPIES
 		| PD_SELECTION | PD_HIDEPRINTTOFILE | PD_RETURNDEFAULT; 
 	CPrintDialog dlg(false,dwflags,NULL);
	if (dlg.DoModal() == IDOK)
 	{
 		LPDEVMODE dv = dlg.GetDevMode();
		//设置打印方向
		if(isway)
			dv->dmOrientation = DMORIENT_LANDSCAPE;   	//横向打印
		else
			dv->dmOrientation = DMORIENT_PORTRAIT;	 	//纵向打印
		CDC dc;
		dc.Attach(dlg.GetPrinterDC());///得到打印机的DC
		dc.ResetDC(dv);
 		Draw(&dc,TRUE);
 	}
}

void COrientationDlg::Draw(CDC *pDC, BOOL isprinted)
{
	m_Font.CreatePointFont(150,"宋体",pDC);
	if (!isprinted) //预览
	{
		ratex = ratey = 1;
	}
	else //打印
	{
		printx = pDC->GetDeviceCaps(LOGPIXELSX);
		printy = pDC->GetDeviceCaps(LOGPIXELSY);
		ratex  = (double)(printx)/screenx;
		ratey  = (double)(printy)/screeny;
		pDC->StartDoc("print");
	}
	pDC->SelectObject(&m_Font);
	for (int i=0;i<6;i++)
	{
		pDC->TextOut(int(100*ratex),int((30+i*40)*ratey),str[i]);
	}
	if (isprinted)
	{
		pDC->EndDoc();
	}
	m_Font.DeleteObject();
}

void COrientationDlg::OnButhorzres() 
{
	// TODO: Add your control notification handler code here
	SetPrint(TRUE);
}

void COrientationDlg::OnButverters() 
{
	// TODO: Add your control notification handler code here
	SetPrint(FALSE);
}

HBRUSH COrientationDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
	CBrush m_brush (RGB(255,255,255));	
	CRect m_rect;
	GetClientRect(m_rect);
	pDC->FillRect(m_rect,&m_brush);
	// TODO: Return a different brush if the default is not desired
	return m_brush;
}
对话框类的头文件中:
CString str[6];									//打印的字符串数组
	int 	screenx,screeny;							//窗口每英寸像素数
	int 	printx,printy;								//打印机每英寸像素数
	double 	ratex,ratey;							//打印机与屏幕的像素比率
	CFont	m_Font;									//打印文本的字体
对于代码的实现理解,我们可以在《打印程序代码常用的函数或结构体粗讲》里面看到。