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

📄 mydlgbrowse.cpp

📁 从列表进行打印的源码,请大家下载参考对照,从中发表意见,加深修改.
💻 CPP
字号:
// MyDlgBrowse.cpp : implementation file
//

#include "stdafx.h"
#include "myprinter.h"
#include "MyDlgBrowse.h"
#include "MyDlgBrowse.h"
#include "Columns.h"
#include "Column.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMyDlgBrowse dialog


CMyDlgBrowse::CMyDlgBrowse(CWnd* pParent /*=NULL*/)
	: CDialog(CMyDlgBrowse::IDD, pParent)
{
	EnableAutomation();

	b_ShowAll=TRUE;
	b_Date=FALSE;
	b_Hospital=FALSE;

	HRESULT hr;
	//初始化连接指针
	CoInitialize(NULL);
	hr=p_connection.CreateInstance(_uuidof(Connection));
	if(FAILED(hr))
	{
		CWnd::MessageBox ("_ConnectionPtr初始化错误!");
	
	}
	
   //初始化recordset指针
	hr=p_recordset.CreateInstance(_uuidof(Recordset));
	if(FAILED(hr))
	{
		CWnd::MessageBox ("_RecordsetPtr初始化错误!");

	}
	
   //初始化recordset指针
	hr=p_rec.CreateInstance(_uuidof(Recordset));
	if(FAILED(hr))
	{
		CWnd::MessageBox ("_RecordsetPtr初始化错误!");

	}


	//{{AFX_DATA_INIT(CMyDlgBrowse)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CMyDlgBrowse::OnFinalRelease()
{
	// When the last reference for an automation object is released
	// OnFinalRelease is called.  The base class will automatically
	// deletes the object.  Add additional cleanup required for your
	// object before calling the base class.

	CDialog::OnFinalRelease();
}

void CMyDlgBrowse::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyDlgBrowse)
	DDX_Control(pDX, IDC_STATIC_TEXT, m_Static_Text);
	DDX_Control(pDX, IDC_COMBO_HOSPTIAL, m_Combo_Hospital);
	DDX_Control(pDX, IDC_COMBO_DATE, m_Combo_Date);
	DDX_Control(pDX, IDC_CHECK_HOSPITAL, m_Check_Hospital);
	DDX_Control(pDX, IDC_CHECK_DATE, m_Check_Date);
	DDX_Control(pDX, IDC_DATAGRID, m_DataGrid);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMyDlgBrowse, CDialog)
	//{{AFX_MSG_MAP(CMyDlgBrowse)
	ON_BN_CLICKED(IDC_BUTTON_REFRESH, OnButtonRefresh)
	ON_BN_CLICKED(IDC_CHECK_DATE, OnCheckDate)
	ON_BN_CLICKED(IDC_CHECK_HOSPITAL, OnCheckHospital)
	ON_BN_CLICKED(IDC_RADIO_ALL, OnRadioAll)
	ON_BN_CLICKED(IDC_BUTTON_DELALL, OnButtonDelAll)
	ON_BN_CLICKED(IDC_BUTTON_DEL, OnButtonDel)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BEGIN_DISPATCH_MAP(CMyDlgBrowse, CDialog)
	//{{AFX_DISPATCH_MAP(CMyDlgBrowse)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()

// Note: we add support for IID_IMyDlgBrowse to support typesafe binding
//  from VBA.  This IID must match the GUID that is attached to the 
//  dispinterface in the .ODL file.

// {7F814C14-5AC7-48DC-883E-88F6351EA7B0}
static const IID IID_IMyDlgBrowse =
{ 0x7f814c14, 0x5ac7, 0x48dc, { 0x88, 0x3e, 0x88, 0xf6, 0x35, 0x1e, 0xa7, 0xb0 } };

BEGIN_INTERFACE_MAP(CMyDlgBrowse, CDialog)
	INTERFACE_PART(CMyDlgBrowse, IID_IMyDlgBrowse, Dispatch)
END_INTERFACE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlgBrowse message handlers

void CMyDlgBrowse::OnButtonRefresh() 
{
	// TODO: Add your control notification handler code here
	//CString str;
	str_Rst="";
	//str="";
	str_Date="";
	str_Hospital="";

	if(this->m_Check_Date .GetCheck ())
	{
		this->m_Combo_Date .GetWindowText (this->str_Date);
	}
	if(this->m_Check_Hospital .GetCheck ())
	{
		this->m_Combo_Hospital .GetWindowText (this->str_Hospital);
	}

	if(str_Date.GetLength ()!=0&&str_Hospital.GetLength ()==0)
	{
		str_Rst.Format ("select * from 打印单据 where 日期='%s'",str_Date);
	}
	else if(str_Date.GetLength ()==0&&str_Hospital.GetLength ()!=0)
	{
		str_Rst.Format ("select * from 打印单据 where 购货单位='%s'",str_Hospital);
	}
	else if(str_Date.GetLength ()!=0&&str_Hospital.GetLength ()!=0)
	{
		str_Rst.Format ("select * from 打印单据 where 日期='%s' and 购货单位='%s'",str_Date,str_Hospital);
	}
	else if(str_Date.GetLength ()==0&&str_Hospital.GetLength ()==0)
	{
		str_Rst.Format ("all");
	}
	else
	{
		this->MessageBox ("不好意思,系统出了点错 ^_^ !  xianyun-1112");
		this->InitDataGrid ("0");
		return;
	}
	this->InitDataGrid (str_Rst);

}

void CMyDlgBrowse::OnCheckDate() 
{
	// TODO: Add your control notification handler code here
	if(this->m_Check_Date .GetCheck ()||this->m_Check_Hospital .GetCheck ())
	{
		((CButton *)GetDlgItem (IDC_RADIO_ALL))->SetCheck(0);
	}
	else
	{
		((CButton *)GetDlgItem (IDC_RADIO_ALL))->SetCheck(1);
	}

	if(this->m_Check_Date .GetCheck ())
	{
		//THIS->
		this->m_Combo_Date .EnableWindow (TRUE);		
	}
	else
	{

		this->m_Combo_Date.ResetContent ();
		this->m_Combo_Date .EnableWindow (FALSE);
	}

	this->m_Combo_Date .ResetContent ();
	this->InitDate ();
	//添加日期
}

void CMyDlgBrowse::OnCheckHospital() 
{
	// TODO: Add your control notification handler code here
	if(this->m_Check_Date .GetCheck ()||this->m_Check_Hospital .GetCheck ())
	{
		((CButton *)GetDlgItem (IDC_RADIO_ALL))->SetCheck(0);
	}
	else
	{
		((CButton *)GetDlgItem (IDC_RADIO_ALL))->SetCheck(1);
	}

	if(this->m_Check_Hospital .GetCheck ())
	{
		//this-
		this->m_Combo_Hospital .EnableWindow (TRUE);
	}
	else
	{
		this->m_Combo_Hospital .ResetContent ();
		this->m_Combo_Hospital .EnableWindow (FALSE);
	}

	this->m_Combo_Hospital .ResetContent ();
	this->InitHospital ();
}

BOOL CMyDlgBrowse::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	this->m_Check_Date .SetCheck (0);
	this->m_Check_Hospital .SetCheck (0);
	((CButton *)GetDlgItem (IDC_RADIO_ALL))->SetCheck(1);
	this->m_Combo_Date .EnableWindow (FALSE);
	this->m_Combo_Hospital .EnableWindow (FALSE);
	this->InitDataGrid ("all");
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

int CMyDlgBrowse::InitDataGrid(CString str)
{
	HRESULT hr;
	CString str_;
	int i=0;

	try
	{
		if(this->p_rec ->GetState()==adStateOpen) 
			p_rec->Close();
	}
	catch(_com_error &err)
	{
		CWnd::MessageBox (err.Description ());
		return -1;
	}
//处理传递过来的参数
	if(str=="0")
	{
		return 0;
	}
	else if(str=="all")
	{
		//str_.Format ("select distinct 日期 from 打印单据 ORDER BY 日期 DESC");
		str_.Format ("select * from 打印单据 ORDER BY 日期 DESC");
	}
	else
	{
		str_=str;
	}
	//打开数据库
	try
	{
		hr=p_rec->Open (_variant_t(str_),
			_variant_t((IDispatch *)p_connection,true),
			adOpenKeyset,
			adLockOptimistic,
			adCmdText
			);
		if(FAILED(hr))
		{
			CWnd::MessageBox ("记录集打开失败");
			return -1;
		}
	}

	catch(_com_error &err)
	{
		CWnd::MessageBox (err.Description ());
		return -1;
	}

	//this->MessageBox ("OK    ^_^");

//更新表格
	
	this->m_DataGrid.SetRefDataSource(p_rec);
	i=p_rec->GetRecordCount ();
	if(i)
	{
		//m_DataGrid.GetColumns().GetItem(_variant_t("拼音")).SetWidth(40.0);
		m_DataGrid.GetColumns().GetItem(_variant_t("药品编号")).SetWidth(50.0);
		m_DataGrid.GetColumns().GetItem(_variant_t("商品名称")).SetWidth(80.0);
		m_DataGrid.GetColumns().GetItem(_variant_t("生产厂家")).SetWidth(80.0);
		m_DataGrid.GetColumns().GetItem(_variant_t("单位")).SetWidth(30.0);
		m_DataGrid.GetColumns().GetItem(_variant_t("数量")).SetWidth(40.0);
		m_DataGrid.GetColumns().GetItem(_variant_t("单价")).SetWidth(50.0);
		m_DataGrid.GetColumns().GetItem(_variant_t("金额")).SetWidth(70.0);
		m_DataGrid.GetColumns().GetItem(_variant_t("商品批号")).SetWidth(30.0);
		m_DataGrid.GetColumns().GetItem(_variant_t("有效期")).SetWidth(20.0);
		m_DataGrid.GetColumns().GetItem(_variant_t("打印")).SetWidth(0.0);
		m_DataGrid.GetColumns().GetItem(_variant_t("备注")).SetWidth(30.0);
		m_DataGrid.GetColumns().GetItem(_variant_t("购货单位")).SetWidth(70.0);
		m_DataGrid.GetColumns().GetItem(_variant_t("日期")).SetWidth(30.0);
		m_DataGrid.GetColumns().GetItem(_variant_t("NO")).SetWidth(50.0);
		m_DataGrid.GetColumns().GetItem(_variant_t("仓库")).SetWidth(60.0);
		
		
		//m_DataGrid.GetColumns().GetItem(_variant_t("规格")).SetWidth(20.0);
		//m_DataGrid.GetColumns().GetItem(_variant_t("药品单位")).SetWidth(20.0);
		
		
		
	}
	
	if(i<0)		i=0;
	str_.Format ("数据库中共有 %d 条记录",i);
	this->m_Static_Text .SetWindowText (str_);
	return 0;
}

void CMyDlgBrowse::OnRadioAll() 
{
	// TODO: Add your control notification handler code here
	if(((CButton *)GetDlgItem (IDC_RADIO_ALL))->GetCheck())//被选中
	{
		this->m_Check_Date .SetCheck (0);
		this->m_Check_Hospital .SetCheck (0);
		this->m_Combo_Date .EnableWindow (FALSE);
		this->m_Combo_Hospital .EnableWindow (FALSE);
	}
	str_Rst.Format ("all");
	this->InitDataGrid (str_Rst);
}

void CMyDlgBrowse::InitDate()
{
	HRESULT hr;
	CString str;

	str.Format ("select distinct 日期 from 打印单据");

	if(p_recordset ->GetState()==adStateOpen) 
		p_recordset->Close();

	try
	{
		hr=p_recordset->Open (_variant_t(str),
			_variant_t((IDispatch *)p_connection,true),
			adOpenKeyset,
			adLockOptimistic,
			adCmdText
			);
		if(FAILED(hr))
		{
			CWnd::MessageBox ("记录集打开失败");
			return;
		}
	}
	catch(_com_error &err)
	{
		CWnd::MessageBox (err.Description ());
		return ;
	}

	if(p_recordset->BOF ==TRUE&&p_recordset->adoEOF ==TRUE)
	{
		CWnd::MessageBox ("数据库中没有记录!");
		return ;
	}

	int i_RecordNum =p_recordset->RecordCount ;
	if(i_RecordNum>0)
	{	
		p_recordset->MoveFirst ();
	}

	while(p_recordset->adoEOF==VARIANT_FALSE)
	{

		if(p_recordset->Fields ->GetItem (_variant_t("日期"))->Value.vt!=VT_NULL)
		{
			str=(char *)(_bstr_t)(p_recordset->Fields ->GetItem (_variant_t("日期"))->Value);
		}
		else
		{
			//p_MyRstNewBill->Fields ->GetItem (_variant_t("pwd"))->Value=_variant_t(str_ChangePwd);
		}
		this->m_Combo_Date .AddString (str);
		p_recordset->MoveNext ();
	}
	
		//p_MyRstNewBill->Update ();

	if(p_recordset->GetState()==adStateOpen) 
		p_recordset->Close();

	//return ;

}

void CMyDlgBrowse::InitHospital()
{
	HRESULT hr;
	CString str;

	str.Format ("select distinct 购货单位 from 打印单据");

	if(p_recordset ->GetState()==adStateOpen) 
		p_recordset->Close();

	try
	{
		hr=p_recordset->Open (_variant_t(str),
			_variant_t((IDispatch *)p_connection,true),
			adOpenKeyset,
			adLockOptimistic,
			adCmdText
			);
		if(FAILED(hr))
		{
			CWnd::MessageBox ("记录集打开失败");
			return;
		}
	}
	catch(_com_error &err)
	{
		CWnd::MessageBox (err.Description ());
		return ;
	}

	if(p_recordset->BOF ==TRUE&&p_recordset->adoEOF ==TRUE)
	{
		CWnd::MessageBox ("数据库中没有记录!");
		return ;
	}

	int i_RecordNum =p_recordset->RecordCount ;
	if(i_RecordNum>0)
	{	
		p_recordset->MoveFirst ();
	}

	while(p_recordset->adoEOF==VARIANT_FALSE)
	{

		if(p_recordset->Fields ->GetItem (_variant_t("购货单位"))->Value.vt!=VT_NULL)
		{
			str=(char *)(_bstr_t)(p_recordset->Fields ->GetItem (_variant_t("购货单位"))->Value);
		}
		else
		{
			//p_MyRstNewBill->Fields ->GetItem (_variant_t("pwd"))->Value=_variant_t(str_ChangePwd);
		}
		this->m_Combo_Hospital  .AddString (str);
		p_recordset->MoveNext ();
	}
	
		//p_MyRstNewBill->Update ();

	if(p_recordset->GetState()==adStateOpen) 
		p_recordset->Close();

}

void CMyDlgBrowse::OnButtonDelAll() 
{
	// TODO: Add your control notification handler code here
	CString str;
	try
	{
		if(p_rec->GetState()==adStateOpen &&p_rec->GetRecordCount ()>0) //p_MyRstDBNewBill->Close();
		{
			str.Format ("您确定要删除这 %d 条信息吗?\t删除后将不能恢复!",p_rec->GetRecordCount ());
			if(this->MessageBox (str,"警告!",MB_YESNO)==7)//按确定等于6,按取消等于7
			{
				//this->MessageBox ("取消");
				return;
			}
		}
	}
	catch(_com_error &err)
	{
		CWnd::MessageBox (err.Description ());
	}

	try
	{
		while(p_rec->adoEOF==VARIANT_FALSE&&p_rec->GetRecordCount () !=0)
		{
			//int iii=p_rec->GetRecordCount();
			p_rec->MoveLast();
			if(p_rec->GetState()==adStateOpen) //p_MyRstDBNewBill->Close();
			{
				p_rec->Delete(adAffectCurrent );//adAffectCurrent
			}
			//p_rec->Update ();
		}
		p_rec->Update ();
	}
	catch(_com_error &err)
	{
		CWnd::MessageBox (err.Description ());

	}

	this->InitDataGrid (str_Rst);
}

void CMyDlgBrowse::OnButtonDel() 
{
	// TODO: Add your control notification handler code here

	try
	{
		if(p_rec->GetState()==adStateOpen &&p_rec->GetRecordCount ()>0) //p_MyRstDBNewBill->Close();
		{
			if(this->MessageBox ("您确定要删除这一条信息吗?\t删除后将不能恢复!","警告!",MB_YESNO)==7)//按确定等于6,按取消等于7
			{
				//this->MessageBox ("取消");
				return;
			}
		}
	}
	catch(_com_error &err)
	{
		CWnd::MessageBox (err.Description ());
	}

	try
	{
		if(p_rec->GetState()==adStateOpen) //p_MyRstDBNewBill->Close();
			p_rec->Delete(adAffectCurrent);//adAffectAll
		p_rec->Update ();
	}
	catch(_com_error &err)
	{
		CWnd::MessageBox (err.Description ());
	}

	this->InitDataGrid (str_Rst);
}

⌨️ 快捷键说明

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