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

📄 printselect.cpp

📁 VC++ 6.0开发。之中打印部分可能对您有值得参考的地方。其他的部分还不是很完善
💻 CPP
字号:
// PrintSelect.cpp : implementation file
//


#include "stdafx.h"
#include "test2.h"
#include "PrintSelect.h"
#include "test2dlg.h"
//print

#include "PrintRX.h"
//ado
#include "AdoRecordSet.h"
#include "AdoCommand.h"

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

/////////////////////////////////////////////////////////////////////////////
// CPrintSelect dialog


CPrintSelect::CPrintSelect(CWnd* pParent /*=NULL*/)
	: CDialog(CPrintSelect::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPrintSelect)
	m_iPrtSel = -1;
	//}}AFX_DATA_INIT
}


void CPrintSelect::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPrintSelect)
	DDX_Radio(pDX, IDC_RADIO1, m_iPrtSel);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPrintSelect, CDialog)
	//{{AFX_MSG_MAP(CPrintSelect)
	ON_BN_CLICKED(IDC_BUTTON_PRINT, OnButtonPrint)
	ON_BN_CLICKED(IDC_BUTTON_Cancel, OnBUTTONCancel)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPrintSelect message handlers

void CPrintSelect::OnButtonPrint() 
{
	CDialog::OnOK();
	//
	//CString strSql;
	//数据库字段变量 
	CString m_XM;
	CString m_SFZ;
	CString m_DH;
	CString m_JSR;
	double m_JE;
	CString m_cJE;
	CString m_BZ;
	//编号或序号
	long lcount=0;
	CString ccount;
	int ipageNum=1;
	CString cipageNum;
	CString dijiye;
	//
	bool isNewPag = FALSE;
	//
	CAdoRecordSet m_adoRecordSet;
	CAdoRecordSet m_padoRecordSet;
	CAdoConnection m_adoConnection;
	_ConnectionPtr m_pConnection;

	if (m_adoConnection.ConnectAccess("renshi.mdb"))
	{
		
		m_adoRecordSet.SetAdoConnection(&m_adoConnection);
		m_adoRecordSet.SetCursorLocation();
		m_adoRecordSet.Open("会员表", adCmdTable);
//		m_adoConnection.BeginTrans();
//		UpdateRecord(TRUE); 
	}
	else
	{
		MessageBox("数据库连接失败!");
	}

	
	if(m_iPrtSel==0)
	{
	
		CPrintRX m_Print;
		if(m_Print.InitToPrint(NULL,1)==-1)
			return;
		m_Print.StartPrint();
		m_Print.StartPage();
		// start printing the lines
		//二维点阵
		int x_pos[7];
		int y_pos[27];

		int i;
		//设置横坐标
		for (i=0; i<27; i++)
			y_pos[i] = 80 + (20 * i);

		//设置纵坐标
		x_pos[0] = 59;
		x_pos[1] = x_pos[0] + 32;
		x_pos[2] = x_pos[1] + 57;
		x_pos[3] = x_pos[2] + 115;
		x_pos[4] = x_pos[3] + 83;
		x_pos[5] = x_pos[4] + 57;
		x_pos[6] = x_pos[5] + 50;
		
		CPen	newPen;
		newPen.CreatePen (PS_SOLID, 3, RGB(0,0,0));
		//打印26行
		for (i=0; i<27; i++)
		{
			m_Print.DrawHLine(x_pos[0], y_pos[i], x_pos[6], y_pos[i], newPen);
		}
		//打印7列,其中0和6还有中间的要单独打
		for (i=0; i<7; i++)
		{
			m_Print.DrawVLine(x_pos[i], y_pos[0], x_pos[i], y_pos[26], newPen);

		}

		CSize Size;
		char m_str[6][10];
		CRect StrRect;              //Draw text in Rect
		strcpy(m_str[0], "编号");
		strcpy(m_str[1], "姓名");
		strcpy(m_str[2], "身份证号");
		strcpy(m_str[3], "电话");
		strcpy(m_str[4], "金额");
		strcpy(m_str[5], "备注");
		m_Print.SetFontFace(0, 0);
		for (i=0; i<6; i++)
		{
			StrRect.SetRect(x_pos[i], y_pos[0], x_pos[i+1], y_pos[1]);
			m_Print.DrawText(m_str[i], StrRect, 0, 0, FORMAT_HCENTER | FORMAT_VCENTER);
		}
		// now end the page
		CString Title;         //标题
		CString HeaderRight;   //BIOSBF NO.

		CString BLeftXSRS;//报销售人数
		CString BCenterHJJE;//月合计金额
		CString BLeftLXDH;//联系电话
		CString BCenterDJL;//店经理
		CString BRightGZQZ;//盖章签字

		CString BLine1;
		CString BLine2;
		CString BLine3;
		//标题
		Title.Format("   店    月销售汇总表");
		m_Print.SetFontFace(0, 3);
		StrRect.SetRect(x_pos[0] + 1, 20, x_pos[6], 56);//void SetRect( int x1, int y1, int x2, int y2 );
		m_Print.DrawText((LPTSTR)(LPCTSTR)Title, StrRect, 0, 0, FORMAT_HCENTER | FORMAT_VCENTER);
		//BIOSBF NO.
		m_Print.SetFontFace(0, 0);
		HeaderRight.Format("BIOSTBF NO.    ");
		StrRect.SetRect(x_pos[0] - 1, 56, x_pos[6], 80);
		m_Print.DrawText((LPTSTR)(LPCTSTR)HeaderRight, StrRect, 0, 0, FORMAT_RIGHT | FORMAT_VCENTER);
		//打印数据库内容

		m_adoRecordSet.MoveFirst();
		while(!m_adoRecordSet.IsEOF())
		{
			++lcount;			
			
			//MessageBox(ccount,NULL,MB_OK);
			
			
			if(lcount!=0 && lcount % 26 ==0)
			{
				lcount=lcount-26+1;
				m_Print.NewPage();
				ipageNum+=1;
				isNewPag = TRUE;
				//
				//打印26行
				for (i=0; i<27; i++)
				{
					m_Print.DrawHLine(x_pos[0], y_pos[i], x_pos[6], y_pos[i], newPen);
				}
				//打印7列,其中0和6还有中间的要单独打
				for (i=0; i<7; i++)
				{
					m_Print.DrawVLine(x_pos[i], y_pos[0], x_pos[i], y_pos[25], newPen);

				}
				//
				m_Print.SetFontFace(0, 0);
				for (i=0; i<6; i++)
				{
					StrRect.SetRect(x_pos[i], y_pos[0], x_pos[i+1], y_pos[1]);
					m_Print.DrawText(m_str[i], StrRect, 0, 0, FORMAT_HCENTER | FORMAT_VCENTER);
				}
				//
				Title.Format("   店    月销售汇总表");
				m_Print.SetFontFace(0, 3);
				StrRect.SetRect(x_pos[0] + 1, 20, x_pos[6], 56);//void SetRect( int x1, int y1, int x2, int y2 );
				m_Print.DrawText((LPTSTR)(LPCTSTR)Title, StrRect, 0, 0, FORMAT_HCENTER | FORMAT_VCENTER);
				//BIOSBF NO.
				m_Print.SetFontFace(0, 0);
				HeaderRight.Format("BIOSTBF NO.    ");
				StrRect.SetRect(x_pos[0] - 1, 56, x_pos[6], 80);
				m_Print.DrawText((LPTSTR)(LPCTSTR)HeaderRight, StrRect, 0, 0, FORMAT_RIGHT | FORMAT_VCENTER);
				//
				
			}
			//
			ccount.Format("%i",lcount);

			if(ipageNum ==1 || isNewPag)
			{
				cipageNum.Format("%i",ipageNum);
				dijiye="第"+cipageNum+"页";
				m_Print.SetFontFace(0, 0);
				StrRect.SetRect(x_pos[0], y_pos[26]+30, x_pos[6], y_pos[26]+30);
				m_Print.DrawText((LPTSTR)(LPCTSTR)dijiye, StrRect, 0, 0, FORMAT_HCENTER | FORMAT_VCENTER);
				
				isNewPag = FALSE;
			}

			m_adoRecordSet.GetFieldValue("姓名",m_XM);
			m_adoRecordSet.GetFieldValue("身份证号",m_SFZ);
			m_adoRecordSet.GetFieldValue("电话",m_DH);
			m_adoRecordSet.GetFieldValue("销售业绩",m_JE);
			m_adoRecordSet.GetFieldValue("备注",m_BZ);

			m_Print.SetFontFace(0, 0);
	
			for(int i=0;i<6;i++)
			{
				StrRect.SetRect(x_pos[i], y_pos[lcount]+3, x_pos[i+1], y_pos[lcount]+3);
				switch(i)
				{
				case 0: m_Print.DrawText((LPTSTR)(LPCTSTR)ccount, StrRect, 0, 0, FORMAT_HCENTER | FORMAT_VCENTER);break;
				case 1:	m_Print.DrawText((LPTSTR)(LPCTSTR)m_XM, StrRect, 0, 0, FORMAT_HCENTER | FORMAT_VCENTER);break;
				case 2:	m_Print.DrawText((LPTSTR)(LPCTSTR)m_SFZ, StrRect, 0, 0, FORMAT_HCENTER | FORMAT_VCENTER);break;
				case 3:	m_Print.DrawText((LPTSTR)(LPCTSTR)m_DH, StrRect, 0, 0, FORMAT_HCENTER | FORMAT_VCENTER);break;
				case 4:	m_cJE.Format("%.2f",m_JE);
					m_Print.DrawText((LPTSTR)(LPCTSTR)m_cJE, StrRect, 0, 0, FORMAT_HCENTER | FORMAT_VCENTER);break;
				case 5:	m_Print.DrawText((LPTSTR)(LPCTSTR)m_BZ, StrRect, 0, 0, FORMAT_HCENTER | FORMAT_VCENTER);break;
				//default: break;
				}
			}

			m_adoRecordSet.MoveNext();
			
		}


		
		//结束打印
		m_Print.EndPage();
		m_Print.EndPrint();
	}
	if(m_iPrtSel==1)
	{
		CPrintRX m_Print;
		if(m_Print.InitToPrint(NULL,1)==-1)
			return;
		m_Print.StartPrint();
		m_Print.StartPage();
		// start printing the lines
		//二维点阵
		int x_pos[7];
		int y_pos[32];

		int i;
		//设置横坐标
		for (i=0; i<32; i++)
			y_pos[i] = 80 + (20 * i);

		//设置纵坐标
		x_pos[0] = 59;
		x_pos[1] = x_pos[0] + 32;
		x_pos[2] = x_pos[1] + 62;
		x_pos[3] = x_pos[2] + 117;
		x_pos[4] = x_pos[3] + 83;
		x_pos[5] = x_pos[4] + 50;
		x_pos[6] = x_pos[5] + 58;
		
		CPen	newPen;
		newPen.CreatePen (PS_SOLID, 3, RGB(0,0,0));
		//打印31行
		for (i=0; i<32; i++)
		{
			m_Print.DrawHLine(x_pos[0], y_pos[i], x_pos[6], y_pos[i], newPen);
		}
		//打印6列,其中0和6还有中间的要单独打
		for (i=0; i<7; i++)
		{
			m_Print.DrawVLine(x_pos[i], y_pos[0], x_pos[i], y_pos[31], newPen);

		}

		CSize Size;
		char m_str[6][10];
		CRect StrRect;              //Draw text in Rect
		strcpy(m_str[0], "编号");
		strcpy(m_str[1], "姓名");
		strcpy(m_str[2], "身份证号");
		strcpy(m_str[3], "电话");
		strcpy(m_str[4], "  ");
		strcpy(m_str[5], "备注");
		m_Print.SetFontFace(0, 0);
		for (i=0; i<6; i++)
		{
			StrRect.SetRect(x_pos[i], y_pos[0], x_pos[i+1], y_pos[1]);
			m_Print.DrawText(m_str[i], StrRect, 0, 0, FORMAT_HCENTER | FORMAT_VCENTER);
		}
		// now end the page
		CString Title;         //标题
		CString HeaderRight;   //日期
		CString HeaderCenter;  //经理
		CString HeaderLeft;  //店号

		CString BLine1;
		CString BLine2;
		CString BLine3;
		//标题
		Title.Format("档  案  汇  总  表");
		m_Print.SetFontFace(0, 3);
		StrRect.SetRect(x_pos[0] + 1, 20, x_pos[6], 56);//void SetRect( int x1, int y1, int x2, int y2 );
		m_Print.DrawText((LPTSTR)(LPCTSTR)Title, StrRect, 0, 0, FORMAT_HCENTER | FORMAT_VCENTER);
		//日期
		m_Print.SetFontFace(0, 0);
		HeaderRight.Format("日期:       ");
		StrRect.SetRect(x_pos[0] - 1, 56, x_pos[6], 80);
		m_Print.DrawText((LPTSTR)(LPCTSTR)HeaderRight, StrRect, 0, 0, FORMAT_RIGHT | FORMAT_VCENTER);
		//经理
		m_Print.SetFontFace(0, 0);
		HeaderRight.Format("经理:       (签字)      ");
		StrRect.SetRect(x_pos[0] - 1, 56, x_pos[6], 80);
		m_Print.DrawText((LPTSTR)(LPCTSTR)HeaderRight, StrRect, 0, 0, FORMAT_HCENTER | FORMAT_VCENTER);
		//店号
		m_Print.SetFontFace(0, 0);
		HeaderRight.Format("店号:    ");
		StrRect.SetRect(x_pos[0] - 1, 56, x_pos[6], 80);
		m_Print.DrawText((LPTSTR)(LPCTSTR)HeaderRight, StrRect, 0, 0, FORMAT_LEFT | FORMAT_VCENTER);
		
		//打印数据库内容

		m_adoRecordSet.MoveFirst();
		while(!m_adoRecordSet.IsEOF())
		{
			++lcount;			
			
			//MessageBox(ccount,NULL,MB_OK);
			
			
			if(lcount!=0 && lcount % 31 ==0)
			{
				lcount=lcount-31+1;
				m_Print.NewPage();
				ipageNum+=1;
				isNewPag = TRUE;
				//
				//打印31行
				for (i=0; i<32; i++)
				{
					m_Print.DrawHLine(x_pos[0], y_pos[i], x_pos[6], y_pos[i], newPen);
				}
				//打印6列,其中0和6还有中间的要单独打
				for (i=0; i<7; i++)
				{
					m_Print.DrawVLine(x_pos[i], y_pos[0], x_pos[i], y_pos[31], newPen);

				}
						//
				m_Print.SetFontFace(0, 0);
				for (i=0; i<6; i++)
				{
					StrRect.SetRect(x_pos[i], y_pos[0], x_pos[i+1], y_pos[1]);
					m_Print.DrawText(m_str[i], StrRect, 0, 0, FORMAT_HCENTER | FORMAT_VCENTER);
				}
						//
				Title.Format("档  案  汇  总  表");
				m_Print.SetFontFace(0, 3);
				StrRect.SetRect(x_pos[0] + 1, 20, x_pos[6], 56);//void SetRect( int x1, int y1, int x2, int y2 );
				m_Print.DrawText((LPTSTR)(LPCTSTR)Title, StrRect, 0, 0, FORMAT_HCENTER | FORMAT_VCENTER);
				//日期
				m_Print.SetFontFace(0, 0);
				HeaderRight.Format("日期:       ");
				StrRect.SetRect(x_pos[0] - 1, 56, x_pos[6], 80);
				m_Print.DrawText((LPTSTR)(LPCTSTR)HeaderRight, StrRect, 0, 0, FORMAT_RIGHT | FORMAT_VCENTER);
				//经理
				m_Print.SetFontFace(0, 0);
				HeaderRight.Format("经理:       (签字)      ");
				StrRect.SetRect(x_pos[0] - 1, 56, x_pos[6], 80);
				m_Print.DrawText((LPTSTR)(LPCTSTR)HeaderRight, StrRect, 0, 0, FORMAT_HCENTER | FORMAT_VCENTER);
				//店号
				m_Print.SetFontFace(0, 0);
				HeaderRight.Format("店号:    ");
				StrRect.SetRect(x_pos[0] - 1, 56, x_pos[6], 80);
				m_Print.DrawText((LPTSTR)(LPCTSTR)HeaderRight, StrRect, 0, 0, FORMAT_LEFT | FORMAT_VCENTER);
					//
				
			}
			//
			ccount.Format("%i",lcount);

			if(ipageNum ==1 || isNewPag)
			{
				cipageNum.Format("%i",ipageNum);
				dijiye="第"+cipageNum+"页";
				m_Print.SetFontFace(0, 0);
				StrRect.SetRect(x_pos[0], y_pos[31]+30, x_pos[6], y_pos[31]+30);
				m_Print.DrawText((LPTSTR)(LPCTSTR)dijiye, StrRect, 0, 0, FORMAT_HCENTER | FORMAT_VCENTER);
				isNewPag = FALSE;
			}
			if(lcount % 2 != 0)
			{
				m_adoRecordSet.GetFieldValue("姓名",m_XM);
				m_adoRecordSet.GetFieldValue("身份证号",m_SFZ);
				m_adoRecordSet.GetFieldValue("电话",m_DH);
				m_adoRecordSet.GetFieldValue("备注",m_BZ);
				m_adoRecordSet.GetFieldValue("介绍人",m_JSR);

				m_adoRecordSet.MoveNext();
				
			}
			//below for circle may have problem
			if(lcount % 2 == 0)
			{
				m_adoRecordSet.Clone(m_padoRecordSet);

				CString temp;

				m_padoRecordSet.MoveFirst();
				m_padoRecordSet.GetFieldValue ("姓名",temp);

				while((temp!=m_JSR) && !m_JSR.IsEmpty())
				{

					m_padoRecordSet.MoveNext ();				
					m_padoRecordSet.GetFieldValue ("姓名",temp);
					//if(temp==m_JSR)
					//	MessageBox(temp,NULL,MB_OK);
				//	if(!m_padoRecordSet.IsEOF())
				//	{
				//		m_padoRecordSet.MoveFirst();
					

				//	}
				} 
				
				
				if(!m_JSR.IsEmpty() && (temp==m_JSR)  )
				{
					    //m_padoRecordSet.MovePrevious();
						m_padoRecordSet.GetFieldValue("姓名",m_XM);
						m_padoRecordSet.GetFieldValue("身份证号",m_SFZ);
						m_padoRecordSet.GetFieldValue("电话",m_DH);
						m_padoRecordSet.GetFieldValue("备注",m_BZ);
				}
				else //if(m_JSR.IsEmpty())
				{
					m_XM=" ";
					m_SFZ=" ";
					m_DH=" ";
					m_BZ=" ";
				}

			//	m_padoRecordSet.Open(strSql,adCmdText,adOpenDynamic);
								
			}

			m_Print.SetFontFace(0, 0);
	
			for(int i=0;i<6;i++)
			{
				StrRect.SetRect(x_pos[i], y_pos[lcount]+3, x_pos[i+1], y_pos[lcount]+3);
				switch(i)
				{
				case 0: m_Print.DrawText((LPTSTR)(LPCTSTR)ccount, StrRect, 0, 0, FORMAT_HCENTER | FORMAT_VCENTER);break;
				case 1:	m_Print.DrawText((LPTSTR)(LPCTSTR)m_XM, StrRect, 0, 0, FORMAT_HCENTER | FORMAT_VCENTER);break;
				case 2:	m_Print.DrawText((LPTSTR)(LPCTSTR)m_SFZ, StrRect, 0, 0, FORMAT_HCENTER | FORMAT_VCENTER);break;
				case 3:	m_Print.DrawText((LPTSTR)(LPCTSTR)m_DH, StrRect, 0, 0, FORMAT_HCENTER | FORMAT_VCENTER);break;
				case 4:	break;
				case 5:	m_Print.DrawText((LPTSTR)(LPCTSTR)m_BZ, StrRect, 0, 0, FORMAT_HCENTER | FORMAT_VCENTER);break;
				//default: break;
				}
			}

			//m_adoRecordSet.MoveNext();
			
		}
		
		//结束打印
		m_Print.EndPage();
		m_Print.EndPrint();
	}
//	CTest2Dlg dlg;
//	dlg.DoPreparePrintDC();
//	dlg.DoPrint(m_iPrtSel);
	/*CString str;
	str.Format("%i",m_iPrtSel);
	MessageBox(str,NULL,MB_OK);*/

}

void CPrintSelect::OnBUTTONCancel() 
{
	// TODO: Add your control notification handler code here
	CDialog::OnCancel();
}


⌨️ 快捷键说明

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