www.gusucode.com > VC++ 名片、工作证打印程序-源码程序 > VC++ 名片、工作证打印程序-源码程序/code/PrintWorkDlg.cpp

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

#include "stdafx.h"
#include "PrintWork.h"
#include "PrintWorkDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CPrintWorkDlg dialog

CPrintWorkDlg::CPrintWorkDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPrintWorkDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPrintWorkDlg)
	m_Edit1 = _T("");
	m_Edit2 = _T("");
	m_Edit3 = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CPrintWorkDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPrintWorkDlg)
	DDX_Control(pDX, IDC_LIST1, m_List);
	DDX_Control(pDX, IDC_FRAME, m_Frame);
	DDX_Control(pDX, IDC_STATIC1, m_Static1);
	DDX_Text(pDX, IDC_EDIT1, m_Edit1);
	DDX_Text(pDX, IDC_EDIT2, m_Edit2);
	DDX_Text(pDX, IDC_EDIT3, m_Edit3);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPrintWorkDlg, CDialog)
	//{{AFX_MSG_MAP(CPrintWorkDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_NOTIFY(NM_CLICK, IDC_LIST1, OnClickList1)
	ON_BN_CLICKED(IDC_BUTPRINT, OnButprint)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPrintWorkDlg message handlers

BOOL CPrintWorkDlg::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_List.SetExtendedStyle(LVS_EX_FLATSB  					//扁平风格显示滚动条
		|LVS_EX_FULLROWSELECT         					//允许整行选中
		|LVS_EX_HEADERDRAGDROP      					//允许整列拖动
		|LVS_EX_ONECLICKACTIVATE     					//单击选中项
		|LVS_EX_GRIDLINES);           						//画出网格线
	//设置列标题及列宽度
	m_List.InsertColumn(0,"姓名",LVCFMT_LEFT,120,0);
	m_List.InsertColumn(1,"部门",LVCFMT_LEFT,120,1);
	m_List.InsertColumn(2,"职务",LVCFMT_LEFT,120,2);
	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()); 							//弹出错误
	}
	CString sql = "select * from workcard";
	m_pRecordset.CreateInstance(__uuidof(Recordset)); 			//创建记录集对象实例
	m_pRecordset->Open(_bstr_t(sql), m_pConnection.GetInterfacePtr(),
		adOpenDynamic, adLockOptimistic, adCmdText); 			//执行SQL得到记录集
	while (!m_pRecordset->adoEOF)							//记录集不为空时循环
	{
		m_List.InsertItem(0,"");								//向列表视图控件中插入行
		//向列表视图控件中插入列
		m_List.SetItemText(0,0,(char*)(_bstr_t)m_pRecordset->GetCollect("姓名"));
		m_List.SetItemText(0,1,(char*)(_bstr_t)m_pRecordset->GetCollect("部门"));
		m_List.SetItemText(0,2,(char*)(_bstr_t)m_pRecordset->GetCollect("职务"));
		m_pRecordset->MoveNext();							//将记录集指针移动到下一条记录
	}
	m_pRecordset->Close();									//关闭记录集
	m_pConnection->Close();

	m_Font.CreatePointFont(200,"宋体");
	m_Static1.SetFont(&m_Font);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CPrintWorkDlg::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 CPrintWorkDlg::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 CPrintWorkDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CPrintWorkDlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	int pos = m_List.GetSelectionMark();
	m_Edit1 = m_List.GetItemText(pos,0);
	m_Edit2 = m_List.GetItemText(pos,1);
	m_Edit3 = m_List.GetItemText(pos,2);
	UpdateData(FALSE);
	*pResult = 0;
}

void CPrintWorkDlg::OnButprint() 
{
	// TODO: Add your control notification handler code here
	CDC* pDC = GetDC();
	CPrintDialog dlg(false);							//定义打印机对话框
	dlg.GetDefaults();								//获取打印机默认值
	CDC dc;
	dc.Attach(dlg.GetPrinterDC());						//关联打印机
	double ratex = dc.GetDeviceCaps(LOGPIXELSX) /
		pDC->GetDeviceCaps(LOGPIXELSX);				//屏幕与打印机分辩率比率(水平方向)
	double ratey = dc.GetDeviceCaps(LOGPIXELSY) /
		pDC->GetDeviceCaps(LOGPIXELSY); 				//屏幕与打印机分辩率比率(垂直方向)
	dc.StartDoc("print");								//开始打印
	CRect rect,framerect;
	m_Frame.GetWindowRect(&framerect);
	dc.Rectangle(10*ratex,10*ratey,(framerect.Width()+10)*ratex
		,(framerect.Height()+10)*ratey);
	char classname[255];
	for (int i=1000;i<1007;i++)						//循环控件标识符
	{
		CWnd *control = this->GetDlgItem(i);				//获取控件
		if (NULL != control)
		{
			CString str;
			control->GetWindowText(str);
			control->GetWindowRect(&rect);		//获取位置
			GetClassName(control->GetSafeHwnd(),classname,255);//获取类名
			if (strcmp(classname,"Edit") == 0)				//判断类型
			{
				//绘制直线
				dc.MoveTo((rect.left-framerect.left+10)*ratex,
					(rect.bottom-framerect.top+10)*ratey);
				dc.LineTo((rect.right-framerect.left+10)*ratex,
					(rect.bottom-framerect.top+10)*ratey);
			}
			if (str == "工作证")
			{
				CFont font;
				font.CreatePointFont(200*ratex,"宋体");
				dc.SelectObject(&font);
				dc.TextOut((rect.left-framerect.left+10)*ratex
					,(rect.top-framerect.top+10)*ratey,str);							//输出内容
				font.DeleteObject();
			}
			else
			{
				CFont font;
				font.CreatePointFont(120*ratex,"宋体");
				dc.SelectObject(&font);
				dc.TextOut((rect.left-framerect.left+10)*ratex
					,(rect.top-framerect.top+10)*ratey,str);							//输出内容
				font.DeleteObject();
			}
		}
	}
	dc.EndDoc();//结束打印
}