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

📄 mydlgaddname.cpp

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

#include "stdafx.h"
#include "myprinter.h"
#include "MyDlgAddName.h"
#include "Columns.h"
#include "Column.h"
#include "MyClass.h"


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

/////////////////////////////////////////////////////////////////////////////
// CMyDlgAddName property page

IMPLEMENT_DYNCREATE(CMyDlgAddName, CPropertyPage)

CMyDlgAddName::CMyDlgAddName() : CPropertyPage(CMyDlgAddName::IDD)
{
	EnableAutomation();
	//{{AFX_DATA_INIT(CMyDlgAddName)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT

	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初始化错误!");

	}


}

CMyDlgAddName::~CMyDlgAddName()
{
}

void CMyDlgAddName::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.

	CPropertyPage::OnFinalRelease();
}

void CMyDlgAddName::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyDlgAddName)
	DDX_Control(pDX, IDC_STATIC_TEXT, m_Static_Text);
	DDX_Control(pDX, IDC_EDIT_SPEC, m_Edit_Spec);
	DDX_Control(pDX, IDC_EDIT_PLANT, m_Edit_Plant);
	DDX_Control(pDX, IDC_EDIT_PINYIN, m_Edit_PinYin);
	DDX_Control(pDX, IDC_EDIT_NUM, m_Edit_Num);
	DDX_Control(pDX, IDC_COMBO_UNIT, m_Combo_Unit);
	DDX_Control(pDX, IDC_EDIT_NAME, m_Edit_Name);
	DDX_Control(pDX, IDC_DATAGRID, m_DataGrid);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMyDlgAddName, CPropertyPage)
	//{{AFX_MSG_MAP(CMyDlgAddName)
	ON_BN_CLICKED(IDC_BUTTON1_DELNAME, OnButton1DelName)
	ON_BN_CLICKED(IDC_BUTTON_COMMEND, OnButtonCommend)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

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

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

// {BD3BEC5C-8424-4793-911D-CD368433141A}
static const IID IID_IMyDlgAddName =
{ 0xbd3bec5c, 0x8424, 0x4793, { 0x91, 0x1d, 0xcd, 0x36, 0x84, 0x33, 0x14, 0x1a } };

BEGIN_INTERFACE_MAP(CMyDlgAddName, CPropertyPage)
	INTERFACE_PART(CMyDlgAddName, IID_IMyDlgAddName, Dispatch)
END_INTERFACE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlgAddName message handlers

void CMyDlgAddName::OnOK() 
{
	// TODO: Add extra validation here
	CString str_con;
	HRESULT hr;

	this->m_Edit_Name .GetWindowText (this->str_Name );
	this->m_Edit_PinYin .GetWindowText (str_PinYin);
	this->m_Combo_Unit .GetWindowText (str_Unit);
	this->m_Edit_Plant .GetWindowText (str_Plant);
	this->m_Edit_Num .GetWindowText (str_Num);
	this->m_Edit_Spec .GetWindowText (str_Spec);

	try
	{
		
		if(str_Num!="")
		{
			str_con.Format ("select * from 药品名称 where 药品编号='%s'",str_Num);
			
			if(this->p_recordset ->GetState()==adStateOpen) p_recordset->Close();

			hr=p_recordset->Open (_variant_t(str_con),
			_variant_t((IDispatch *)p_connection,true),
			adOpenKeyset,
			adLockOptimistic,
			adCmdText
			);
			if(FAILED(hr))
			{
				CWnd::MessageBox ("记录集打开失败");
			}
			if(p_recordset->GetRecordCount ()>0)
			{
				this->MessageBox ("药品编号重复,请重新选择或重新输入!\n\n也可以使用【<<<推荐】按钮,系统自动完成");
				return;
			}
	
		}//end if(str_Num!="")
	//	else
		//{
		//}

	}//end try
	catch(_com_error &err)
	{
		CWnd::MessageBox (err.Description ()+"\n你填写的信息有错误!(wklh12-1002)");
	}

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

		hr=p_recordset->Open (_variant_t("药品名称"),
		_variant_t((IDispatch *)p_connection,true),
		adOpenKeyset,
		adLockOptimistic,
		adCmdTable
		);
	}//end try
	catch(_com_error &err)
	{
		CWnd::MessageBox (err.Description ()+"\n你填写的信息有错误!(wklh12-1004)");
	}

	try
	{
		if(str_Name=="")
		{
			this->MessageBox ("请在【药品名称】中填入药品的名称");
			return;
		}
		if(str_PinYin=="")
		{
			this->MessageBox ("请在【拼音】中填入药品名称的拼音首字母");
			return;
		}
		this->p_recordset ->AddNew ();
		p_recordset->PutCollect (_variant_t("药品名称"),_variant_t(str_Name));
		p_recordset->PutCollect (_variant_t("拼音"),_variant_t(str_PinYin));
		
		if(str_Unit!="")
		{
			p_recordset->PutCollect (_variant_t("药品单位"),_variant_t(str_Unit));
		}
		if(str_Plant!="")
		{
			p_recordset->PutCollect (_variant_t("生产厂家"),_variant_t(str_Plant));
		}
		if(str_Num!="")
		{
			p_recordset->PutCollect (_variant_t("药品编号"),_variant_t(str_Num));
		}
		if(str_Spec!="")
		{
			p_recordset->PutCollect (_variant_t("规格"),_variant_t(str_Spec));
		}

		p_recordset->Update();
		
	}//end try
	catch(_com_error &err)
	{
		CWnd::MessageBox (err.Description ()+"\n你填写的信息有错误!(wklh12-1003)");
	}

	this->m_Edit_Name .SetWindowText ("");
	this->m_Edit_PinYin .SetWindowText ("");
	this->m_Combo_Unit .SetWindowText ("");
	this->m_Edit_Plant .SetWindowText ("");
	this->m_Edit_Num .SetWindowText ("");
	this->m_Edit_Spec .SetWindowText ("");

	this->InitDataGrid ();
	this->m_Combo_Unit .ResetContent ();
	this->InitUnit ();


	//CPropertyPage::OnOK();
}

BOOL CMyDlgAddName::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	
	// TODO: Add extra initialization here
	this->InitDataGrid ();
	this->InitUnit ();


	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CMyDlgAddName::InitDataGrid()
{
	HRESULT hr;
	CString str;
	int i;

	try
	{
		if(this->p_rec ->GetState()==adStateOpen) p_rec->Close();
	}
	catch(_com_error &err)
	{
		CWnd::MessageBox (err.Description ());
	}



	str.Format ("select * from 药品名称 ORDER BY 拼音 DESC");
	
	try
	{
		hr=p_rec->Open (_variant_t(str),
			_variant_t((IDispatch *)p_connection,true),
			adOpenKeyset,
			adLockOptimistic,
			adCmdText
			);
		if(FAILED(hr))
		{
			CWnd::MessageBox ("记录集打开失败");
		}
	}

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

	//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(80.0);
		m_DataGrid.GetColumns().GetItem(_variant_t("药品编号")).SetWidth(40.0);
		m_DataGrid.GetColumns().GetItem(_variant_t("规格")).SetWidth(20.0);
		m_DataGrid.GetColumns().GetItem(_variant_t("药品单位")).SetWidth(20.0);
		m_DataGrid.GetColumns().GetItem(_variant_t("生产厂家")).SetWidth(100.0);
		//m_DataGrid_Main.GetColumns().GetItem(_variant_t("生产厂家")).SetWidth(70.0);
		//m_DataGrid_Main.GetColumns().GetItem(_variant_t("单位")).SetWidth(30.0);
		
	}
	if(i<0)		i=0;
	str.Format ("数据库中共有 %d 条记录",i);
	this->m_Static_Text .SetWindowText (str);	

}

void CMyDlgAddName::InitUnit()
{

	HRESULT hr;
	CString str;
	
	if(p_recordset ->GetState()==adStateOpen) p_recordset->Close();

	try
	{
		hr=p_recordset->Open (_variant_t("药品单位"),
			_variant_t((IDispatch *)p_connection,true),
			adOpenKeyset,
			adLockOptimistic,
			adCmdTable
			);
		if(FAILED(hr))
		{
			CWnd::MessageBox ("记录集打开失败");
		}
	}

	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_Unit.AddString (str);
		p_recordset->MoveNext ();
	}
	
		//p_MyRstNewBill->Update ();

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

	return ;


}

void CMyDlgAddName::OnButton1DelName() 
{
	// TODO: Add your control notification handler code here
		//this->MessageBox ("您确定要删除这一条信息吗?","警告!",MB_YESNO);

	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);
		p_rec->Update ();
	}
	catch(_com_error &err)
	{
		CWnd::MessageBox (err.Description ());
	}

	this->InitDataGrid ();
	this->m_Combo_Unit .ResetContent ();
	this->InitUnit ();
}

void CMyDlgAddName::OnButtonCommend() 
{
	// TODO: Add your control notification handler code here
	CString str,str1;
	HRESULT hr;
	int i,j;

	this->m_Edit_Num .GetWindowText(this->str_Num );
	if(str_Num.GetLength ()==0)
	{
		for(i=10;i<10000;i++)
		{
			str.Format ("select * from 药品名称 where 药品编号='YH%d'",i);
			try
			{
				if(this->p_recordset ->GetState()==adStateOpen) p_recordset->Close();
			}
			catch(_com_error &err)
			{
				CWnd::MessageBox (err.Description ());
			}
			try
			{
				hr=p_recordset->Open (_variant_t(str),
					_variant_t((IDispatch *)p_connection,true),
					adOpenKeyset,
					adLockOptimistic,
					adCmdText
					);
				if(FAILED(hr))
				{
					CWnd::MessageBox ("记录集打开失败");
				}
			}
			catch(_com_error &err)
			{
				CWnd::MessageBox (err.Description ());
				return ;
			}

			if(p_recordset->GetRecordCount ()<0)
			{
				this->MessageBox ("系统无法完成自动搜索功能,请手动填写!");
				return;
			}
			else if(p_recordset->GetRecordCount ()==0)
			{
				str.Format ("YH%d",i);
				this->m_Edit_Num .SetWindowText (str);
				break;
			}
		}//end for(int i=10;i<10000;i++)
		if(i>10000)
		{
			this->MessageBox ("系统无法完成自动搜索功能,请手动填写!");
			return;
		}
	}//end if(str_Num.GetLength ()==0)
	else
	{
		j=str_Num.GetLength ();
		for(i=1;i<=j;i++)
		{
			str1=str_Num.Mid (j-i,1);
			if(str1=="0"||str1=="1"||str1=="2"||str1=="3"||str1=="4"||str1=="5"||str1=="6"||str1=="7"||str1=="8"||str1=="9")
			{
				continue;
				//i+=1;
				//break;
			}
			else
			{
				//i+=1;
				break;
			}
		}

		if(i<=2)//所输入的内容没有数字
		{
			for(i=10;i<10000;i++)
			{
				str.Format ("select * from 药品名称 where 药品编号='YH%d'",i);
				try
				{
					if(this->p_recordset ->GetState()==adStateOpen) 
						p_recordset->Close();
				}
				catch(_com_error &err)
				{
					CWnd::MessageBox (err.Description ());
				}
				try
				{
					hr=p_recordset->Open (_variant_t(str),
						_variant_t((IDispatch *)p_connection,true),
						adOpenKeyset,
						adLockOptimistic,
						adCmdText
						);
					if(FAILED(hr))
					{
						CWnd::MessageBox ("记录集打开失败");
					}
				}
				catch(_com_error &err)
				{
					CWnd::MessageBox (err.Description ());
					return ;
				}

				if(p_recordset->GetRecordCount ()<0)
				{
					this->MessageBox ("系统无法完成自动搜索功能,请手动填写!");
					return;
				}
				else if(p_recordset->GetRecordCount ()==0)
				{
					str.Format ("YH%d",i);
					this->m_Edit_Num .SetWindowText (str);
					break;
				}
			}//end for(int i=10;i<10000;i++)
		}//end if(i<=2)
		else
		{
			j=atoi(str_Num.Right (i-1));

			for(i=j;i<10000;i++)
			{				
				str.Format ("select * from 药品名称 where 药品编号='YH%d'",i);
				try
				{
					if(this->p_recordset ->GetState()==adStateOpen) 
						p_recordset->Close();
				}
				catch(_com_error &err)
				{
					CWnd::MessageBox (err.Description ());
				}
				try
				{
					hr=p_recordset->Open (_variant_t(str),
						_variant_t((IDispatch *)p_connection,true),
						adOpenKeyset,
						adLockOptimistic,
						adCmdText
						);
					if(FAILED(hr))
					{
						CWnd::MessageBox ("记录集打开失败");
					}
				}
				catch(_com_error &err)
				{
					CWnd::MessageBox (err.Description ());
					return ;
				}

				if(p_recordset->GetRecordCount ()<0)
				{
					this->MessageBox ("系统无法完成自动搜索功能,请手动填写!");
					return;
				}
				else if(p_recordset->GetRecordCount ()==0)
				{
					str.Format ("YH%d",i);
					this->m_Edit_Num .SetWindowText (str);
					break;
				}
			}//end for(int i=10;i<10000;i++)
		}
	}
}

void CMyDlgAddName::InitNum()
{
	HRESULT hr;
	CString str;
	
	if(p_recordset ->GetState()==adStateOpen) p_recordset->Close();

	try
	{
		hr=p_recordset->Open (_variant_t("药品单位"),
			_variant_t((IDispatch *)p_connection,true),
			adOpenKeyset,
			adLockOptimistic,
			adCmdTable
			);
		if(FAILED(hr))
		{
			CWnd::MessageBox ("记录集打开失败");
		}
	}

	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_Num.AddString (str);
		p_recordset->MoveNext ();
	}
	
		//p_MyRstNewBill->Update ();

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

	return ;


}

⌨️ 快捷键说明

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