📄 hospitalview.cpp
字号:
// HospitalView.cpp : implementation of the CHospitalView class
//
#include "stdafx.h"
#include "Hospital.h"
#include "HospitalDoc.h"
#include "HospitalView.h"
#include "Person.h"
#include "Diagnose.h"
#include "Medicament.h"
#include "Price.h"
#include "Recipe.h"
#include "Invoice.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CHospitalView
IMPLEMENT_DYNCREATE(CHospitalView, CFormView)
BEGIN_MESSAGE_MAP(CHospitalView, CFormView)
//{{AFX_MSG_MAP(CHospitalView)
ON_BN_CLICKED(IDC_MEDICAMENT, OnMedicament)
ON_BN_CLICKED(IDC_PRICE, OnPrice)
ON_BN_CLICKED(IDC_DIAGNOSE, OnDiagnose)
ON_BN_CLICKED(IDC_PERSON, OnPerson)
ON_CBN_SELCHANGE(IDC_TABLENAME, OnSelchangeTablename)
ON_BN_CLICKED(IDC_RECIPE, OnRecipe)
ON_BN_CLICKED(IDC_INVOICE, OnInvoice)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHospitalView construction/destruction
CHospitalView::CHospitalView()
: CFormView(CHospitalView::IDD)
{
//{{AFX_DATA_INIT(CHospitalView)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// TODO: add construction code here
m_nField = 0;
}
CHospitalView::~CHospitalView()
{
}
void CHospitalView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CHospitalView)
DDX_Control(pDX, IDC_LIST, m_listRecord);
DDX_Control(pDX, IDC_TABLENAME, m_combTableName);
//}}AFX_DATA_MAP
}
BOOL CHospitalView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CFormView::PreCreateWindow(cs);
}
void CHospitalView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
// 数据源指针
m_pDB = new CDaoDatabase;
// 数据源路径
CString sPath;
GetModuleFileName(NULL, sPath.GetBufferSetLength(MAX_PATH + 1), MAX_PATH);
sPath.ReleaseBuffer();
sPath = sPath.Left (sPath.ReverseFind('\\'));
sPath += "\\门诊信息管理数据库.mdb";
// 打开数据源
try
{
m_pDB->Open(sPath);
}
catch(CDaoException* e)
{
AfxMessageBox(e->m_pErrorInfo->m_strDescription, MB_ICONEXCLAMATION);
delete m_pDB;
e->Delete();
return;
}
// 表定义信息结构对象
CDaoTableDefInfo tabInfo;
// 得到表定义个数
int nTableDefCount = m_pDB->GetTableDefCount();
// 对表进行枚举
for (int i = 0; i < nTableDefCount; i++)
{
// 得到表定义信息
m_pDB->GetTableDefInfo(i, tabInfo);
if (tabInfo.m_lAttributes & dbSystemObject)
continue;
// 将表名添加到组合框控件
m_combTableName.AddString(tabInfo.m_strName);
}
// 记录集指针
m_pRecordSet = new CDaoRecordset(m_pDB);
}
/////////////////////////////////////////////////////////////////////////////
// CHospitalView printing
BOOL CHospitalView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CHospitalView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CHospitalView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CHospitalView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
// TODO: add customized printing code here
}
/////////////////////////////////////////////////////////////////////////////
// CHospitalView diagnostics
#ifdef _DEBUG
void CHospitalView::AssertValid() const
{
CFormView::AssertValid();
}
void CHospitalView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
CHospitalDoc* CHospitalView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CHospitalDoc)));
return (CHospitalDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CHospitalView message handlers
void CHospitalView::OnMedicament()
{
CMedicament medicamet;
medicamet.m_pRecordSet = m_pRecordSet;
if (medicamet.DoModal() == IDOK)
{
if (m_sGetString == "药剂信息")
OnSelchangeTablename();
}
}
void CHospitalView::OnPrice()
{
CPrice price;
price.m_pRecordSet = m_pRecordSet;
if (price.DoModal() == IDOK)
{
if (m_sGetString == "划价记录")
OnSelchangeTablename();
}
}
void CHospitalView::OnDiagnose()
{
// 传递记录集指针到对话框类
CDiagnose diagnose;
diagnose.m_pRecordSet = m_pRecordSet;
if (diagnose.DoModal() == IDOK)
{
if (m_sGetString == "就诊信息")
OnSelchangeTablename();
}
}
void CHospitalView::OnPerson()
{
// 传递记录集指针到对话框类
CPerson person;
person.m_pRecordSet = m_pRecordSet;
if (person.DoModal() == IDOK)
{
if (m_sGetString == "病人信息")
OnSelchangeTablename();
}
}
void CHospitalView::OpenRecordSet()
{
// 关闭上次打开的记录集
if (m_pRecordSet->IsOpen())
m_pRecordSet->Close();
// 清空列表框
m_listRecord.DeleteAllItems();
if (m_nField != 0)
{
for (long i = 0; i < m_nField; i++)
m_listRecord.DeleteColumn(0);
}
// 从组合框得到选中的表名
m_combTableName.GetLBText(m_combTableName.GetCurSel(), m_sGetString);
// 构造SQL查询语句
CString strSQL = "SELECT * FROM " + m_sGetString;
// 用构造的SQL语句打开记录集
try
{
m_pRecordSet->Open(dbOpenDynaset, strSQL);
m_pRecordSet->m_strFilter.Empty();
if (m_pRecordSet == NULL)
return;
}
catch (CDaoException *e)
{
AfxMessageBox(e->m_pErrorInfo->m_strDescription, MB_ICONEXCLAMATION);
delete m_pRecordSet;
m_pDB->Close();
delete m_pDB;
e->Delete();
return;
}
}
void CHospitalView::OnSelchangeTablename()
{
// 打开记录集
OpenRecordSet();
// 设置列表框的扩展风格
DWORD dwExStyle = LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_HEADERDRAGDROP | LVS_EX_TRACKSELECT;
m_listRecord.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
LV_COLUMN lvColumn;
lvColumn.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
lvColumn.fmt = LVCFMT_LEFT;
lvColumn.cx = 67;
// 得到记录集的字段数
m_nField = m_pRecordSet->GetFieldCount();
// 对各列进行处理
for (int i = 0; i < m_nField; i++)
{
// 得到并插入字段名
CDaoFieldInfo m_fieldinfo;
m_pRecordSet->GetFieldInfo(i, m_fieldinfo);
int len = m_fieldinfo.m_strName.GetLength();
CString temp = m_fieldinfo.m_strName;
TCHAR* szBuffer = new TCHAR[len + 1];
strcpy(szBuffer, temp.GetBuffer(len));
temp.ReleaseBuffer();
lvColumn.pszText = szBuffer;
m_listRecord.InsertColumn(i, &lvColumn);
delete szBuffer;
}
// 滚动记录集
if (m_pRecordSet->IsBOF() == FALSE)
m_pRecordSet->MoveFirst();
if (m_pRecordSet->IsEOF() == FALSE)
m_pRecordSet->MoveLast();
// 得到记录数
long count = m_pRecordSet->GetRecordCount();
// 显示记录
GetTableInfo(count, m_nField);
}
void CHospitalView::GetTableInfo(long row, long column)
{
COleVariant varValue;
// 处理各行
for (long i = 0; i < row; i++)
{
// 用记录光标重定位到第i条记录。
try
{
m_pRecordSet->SetAbsolutePosition(i);
}
catch (CDaoException* e)
{
AfxMessageBox(e->m_pErrorInfo->m_strDescription, MB_ICONEXCLAMATION);
e->Delete();
return;
}
// 处理各列
for (long j = 0; j < column; j++)
{
// 得到记录集中的值
try
{
m_pRecordSet->GetFieldValue(j, varValue);
}
catch (CDaoException* e)
{
AfxMessageBox(e->m_pErrorInfo->m_strDescription, MB_ICONEXCLAMATION);
e->Delete();
return;
}
// 将得到的OLE变量转换为字符串变量
const VARIANT* variant = LPCVARIANT(varValue);
if (variant->vt & VT_BYREF)
return;
CString string;
switch (variant->vt)
{
case VT_ERROR:
{
string = "Error";
break;
}
case VT_I2:
{
string.Format("%d", variant->iVal);
break;
}
case VT_I4:
{
string.Format("%d", variant->lVal);
break;
}
case VT_R4:
{
string.Format("%.2f", variant->fltVal);
break;
}
case VT_R8:
{
string.Format("%.2f", variant->dblVal);
break;
}
case VT_CY:
{
COleCurrency c(varValue);
string = c.Format();
break;
}
case VT_DATE:
{
COleDateTime t(variant->date);
string = t.Format("%Y-%m-%d");
break;
}
case VT_BSTR:
{
string = V_BSTRT(&varValue);
break;
}
case VT_BOOL:
{
if (variant->boolVal)
string = "TRUE";
else
string = "FALSE";
break;
}
case VT_UI1:
{
string = (CString)((char*)variant->bVal);
break;
}
default:
break;
}
// 设置各个项目
if (j == 0)
m_listRecord.InsertItem(i, string, 0);
else
m_listRecord.SetItemText(i, j, string);
}
}
}
void CHospitalView::OnRecipe()
{
CRecipe recipe;
recipe.m_pRecordSet = m_pRecordSet;
if (recipe.DoModal() != IDOK)
return;
if (WordApp.m_lpDispatch == NULL)
{
if (!WordApp.CreateDispatch("Word.Application",NULL))
{
MessageBox("创建服务失败,请重新运行应用程序!","错误");
PostMessage(WM_QUIT);
}
}
if (WordDoc.m_lpDispatch == NULL)
{
WordApp.SetVisible(true); // true 可见 false 不可见
WordApp.SetWindowState(1); // 0:正常 1:最大化 2:最小化
// 取模板路径
char exeFullPath[MAX_PATH];
GetModuleFileName(NULL, exeFullPath, MAX_PATH);
CString strPath = CString(exeFullPath);
strPath = strPath.Left(strPath.GetLength() - CString(AfxGetAppName()).GetLength() - 4);
strPath += "处方模板.dot";
// 模板文件路径
_variant_t WordTemplate = strPath;
// 利用模板文件建立新文档
WordDocs = WordApp.GetDocuments();
WordDoc = WordDocs.Add(&WordTemplate, &vtMissing, &vtMissing, &vtMissing);
// 由于用户会对WORD进行一些个性化设置,但用户的设置会引起一些
// 误操作,以下代码设置WORD“ 工具-选项-视图”中的一些选项。
window = WordApp.GetActiveWindow();
view = window.GetView();
view.SetShowPicturePlaceHolders(false); // 不显示图片框
view.SetShowBookmarks(false); // 不显示书签
view.SetShowFieldCodes(false); // 不显示域代码
// 以下将程序中的值代入WORD模板中
// 添加病人姓名
bookmarks = WordDoc.GetBookmarks();
bookmark = bookmarks.Item(&_variant_t("Name"));
range = bookmark.GetRange();
range.SetText(recipe.m_sName);
// 添加主治医师
bookmarks = WordDoc.GetBookmarks();
bookmark = bookmarks.Item(&_variant_t("Doctor"));
range = bookmark.GetRange();
range.SetText(recipe.m_sDoctor);
// 添加主诉
bookmarks = WordDoc.GetBookmarks();
bookmark = bookmarks.Item(&_variant_t("Reason"));
range = bookmark.GetRange();
range.SetText(recipe.m_sReason);
// 添加处方
bookmarks = WordDoc.GetBookmarks();
bookmark = bookmarks.Item(&_variant_t("Result"));
range = bookmark.GetRange();
range.SetText(recipe.m_sResult);
// 释放所有占用的资源
window.ReleaseDispatch();
view.ReleaseDispatch();
range.ReleaseDispatch();
fields.ReleaseDispatch();
field.ReleaseDispatch();
bookmark.ReleaseDispatch();
bookmarks.ReleaseDispatch();
}
else
{
MessageBox("上一报告正在处理中,请在当前报表处理完毕后,再生成新的报表!", "错误");
}
}
void CHospitalView::OnInvoice()
{
CInVoice invoice;
invoice.m_pRecordSet = m_pRecordSet;
if (invoice.DoModal() != IDOK)
return;
if (WordApp.m_lpDispatch == NULL)
{
if (!WordApp.CreateDispatch("Word.Application",NULL))
{
MessageBox("创建服务失败,请重新运行应用程序!","错误");
PostMessage(WM_QUIT);
}
}
if (WordDoc.m_lpDispatch == NULL)
{
WordApp.SetVisible(true); // true 可见 false 不可见
WordApp.SetWindowState(1); // 0:正常 1:最大化 2:最小化
WordDocs = WordApp.GetDocuments();
// 取模板路径
char exeFullPath[MAX_PATH];
GetModuleFileName(NULL, exeFullPath, MAX_PATH);
CString strPath = CString(exeFullPath);
strPath = strPath.Left(strPath.GetLength() - CString(AfxGetAppName()).GetLength() - 4);
strPath += "发票模板.dot";
// 模板文件路径
_variant_t WordTemplate = strPath;
// 利用模板文件建立新文档
WordDoc = WordDocs.Add(&WordTemplate, &vtMissing, &vtMissing, &vtMissing);
// 由于用户会对WORD进行一些个性化设置,但用户的设置会引起一些
// 误操作,以下代码设置WORD“ 工具-选项-视图”中的一些选项。
window = WordApp.GetActiveWindow();
view = window.GetView();
view.SetShowPicturePlaceHolders(false); // 不显示图片框
view.SetShowBookmarks(false); // 不显示书签
view.SetShowFieldCodes(false); // 不显示域代码
// 以下将程序中的值代入WORD模板中
// 添加病人姓名
bookmarks = WordDoc.GetBookmarks();
bookmark = bookmarks.Item(&_variant_t("Name"));
range = bookmark.GetRange();
range.SetText(invoice.m_sName);
// 添加金额
bookmarks = WordDoc.GetBookmarks();
bookmark = bookmarks.Item(&_variant_t("Price"));
range = bookmark.GetRange();
CString sTemp;
sTemp.Format("%0.2f元", invoice.m_fPrice);
range.SetText(sTemp);
// 添加药剂清单
sTemp = "";
for (int i = 0; i < invoice.m_nCount; i++)
sTemp += invoice.m_sList[i] + CString(13) + CString(10);
bookmarks = WordDoc.GetBookmarks();
bookmark = bookmarks.Item(&_variant_t("List"));
range = bookmark.GetRange();
range.SetText(sTemp);
// 释放所有占用的资源
window.ReleaseDispatch();
view.ReleaseDispatch();
range.ReleaseDispatch();
fields.ReleaseDispatch();
field.ReleaseDispatch();
bookmark.ReleaseDispatch();
bookmarks.ReleaseDispatch();
}
else
{
MessageBox("上一报告正在处理中,请在当前报表处理完毕后,再生成新的报表!", "错误");
}
}
void CHospitalView::OnDestroy()
{
CFormView::OnDestroy();
// 终止COM库服务函数
CoUninitialize();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -