www.gusucode.com > VC++打印表格的源代码例子源码程序 > VC++打印表格的源代码例子源码程序/code/PrintTableDlg.cpp

    // PrintTableDlg.cpp : implementation file
// Download by http://www.NewXing.com

#include "stdafx.h"
#include "PrintTable.h"
#include "PrintTableDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPrintTableDlg dialog

CPrintTableDlg::CPrintTableDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPrintTableDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPrintTableDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CPrintTableDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPrintTableDlg)
	DDX_Control(pDX, IDC_LIST1, m_List);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPrintTableDlg, CDialog)
	//{{AFX_MSG_MAP(CPrintTableDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTPRINT, OnButprint)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPrintTableDlg message handlers

BOOL CPrintTableDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	m_Title[0] = "商品名称";
	m_Title[1] = "销售数量";
	m_Title[2] = "销售金额";
	m_Title[3] = "售货员";
	m_List.SetExtendedStyle(LVS_EX_FLATSB  					//扁平风格显示滚动条
		|LVS_EX_FULLROWSELECT         					//允许整行选中
		|LVS_EX_HEADERDRAGDROP      					//允许整列拖动
		|LVS_EX_ONECLICKACTIVATE     					//单击选中项
		|LVS_EX_GRIDLINES);           						//画出网格线
	//设置列标题及列宽度
	m_List.InsertColumn(0,"商品名称",LVCFMT_LEFT,100,0);
	m_List.InsertColumn(1,"销售数量",LVCFMT_LEFT,100,1);
	m_List.InsertColumn(2,"销售金额",LVCFMT_LEFT,100,2);
	m_List.InsertColumn(3,"售货员",LVCFMT_LEFT,100,3);
	try
	{
		m_pConnection.CreateInstance("ADODB.Connection");  			//创建连接对象实例
		_bstr_t strConnect="DRIVER={Microsoft Access Driver (*.mdb)};\
			uid=;pwd=;DBQ=Database.mdb;";
		m_pConnection->Open(strConnect,"","",adModeUnknown); 		//打开数据库
	}
	catch (_com_error e)											//捕捉错误
	{
		AfxMessageBox(e.Description()); 							//弹出错误
	}
	_bstr_t sql;
	sql = "select * from SellInfo";
	m_pRecordset.CreateInstance(__uuidof(Recordset));
	m_pRecordset->Open(sql,m_pConnection.GetInterfacePtr()
		,adOpenDynamic,adLockOptimistic,adCmdText);
	int i=0;
	while(m_pRecordset->adoEOF==0)
	{
		m_List.InsertItem(i,"");								//向列表视图控件中插入行
		//向列表视图控件中插入列
		m_List.SetItemText(i,0,(char*)(_bstr_t)m_pRecordset->GetCollect("商品名称"));
		m_List.SetItemText(i,1,(char*)(_bstr_t)m_pRecordset->GetCollect("销售数量"));
		m_List.SetItemText(i,2,(char*)(_bstr_t)m_pRecordset->GetCollect("销售金额"));
		m_List.SetItemText(i++,3,(char*)(_bstr_t)m_pRecordset->GetCollect("售货员"));
		m_pRecordset->MoveNext();							//将记录集指针移动到下一条记录
	}
	m_pRecordset->Close();
	m_pConnection->Close();
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CPrintTableDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CPrintTableDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CPrintTableDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CPrintTableDlg::OnButprint() 
{
	// TODO: Add your control notification handler code here
	int count = m_List.GetItemCount();
	CDC* pDC = GetDC();
	//获得屏幕分辨率
	double xscreen = pDC->GetDeviceCaps(LOGPIXELSX);
	double yscreen = pDC->GetDeviceCaps(LOGPIXELSY);
	ReleaseDC(pDC);
	CPrintDialog dlg (FALSE,PD_RETURNDEFAULT);	//构造打印对话框
	if (dlg.DoModal() == IDOK)
	{
		CDC dc;
		dc.Attach(dlg.GetPrinterDC());
		double xprint = dc.GetDeviceCaps(LOGPIXELSX);
		double yprint = dc.GetDeviceCaps(LOGPIXELSY);
		double ratex = (double)(xprint)/xscreen; //计算屏幕和打印机分辨率的比例
		double ratey = (double)(yprint)/yscreen;
		CRect rect(25*ratex,0,dc.GetDeviceCaps(PHYSICALWIDTH)-50*ratex
			,dc.GetDeviceCaps(PHYSICALHEIGHT));							//打印纸区域
		CRect rc;
		int k=0;
		dc.StartDoc("print");											//开始打印
		for (int i=0;i<(count+2)*4;i++)									//根据列表项数量循环
		{
			if(i%4 == 0)												//判断绘制横线条件
			{
				//绘制横线
				dc.MoveTo(rect.left,(rect.top+40*(i/4))*ratey);
				dc.LineTo(rect.right,(rect.top+40*(i/4))*ratey);
			}
			//设置文本区域
			rc.left   = rect.left+rect.Width()*(i%4)/4;
			rc.top    = rect.top+40*(i/4)*ratey;
			rc.right  = rc.left+rect.Width()/4;
			rc.bottom = rc.top+30*ratey;
			if(i < 4)													//绘制标题
				dc.DrawText(m_Title[i],&rc,DT_CENTER|DT_SINGLELINE|DT_VCENTER );
			else														//绘制表文
				dc.DrawText(m_List.GetItemText(i/4-1,k++),&rc,DT_CENTER|DT_SINGLELINE|DT_VCENTER);
			if(k == 4)
				k = 0;
		}
		//绘制竖线
		for (int j=0;j<5;j++)
		{
			dc.MoveTo(rect.left+rect.Width()*j/4,rect.top*ratey);
			dc.LineTo(rect.left+rect.Width()*j/4,(rect.top+40*(i/4-1))*ratey);
		}
		dc.EndDoc();													//结束打印
	}
}