?? 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();
// 數(shù)據(jù)源指針
m_pDB = new CDaoDatabase;
// 數(shù)據(jù)源路徑
CString sPath;
GetModuleFileName(NULL, sPath.GetBufferSetLength(MAX_PATH + 1), MAX_PATH);
sPath.ReleaseBuffer();
sPath = sPath.Left (sPath.ReverseFind('\\'));
sPath += "\\門診信息管理數(shù)據(jù)庫.mdb";
// 打開數(shù)據(jù)源
try
{
m_pDB->Open(sPath);
}
catch(CDaoException* e)
{
AfxMessageBox(e->m_pErrorInfo->m_strDescription, MB_ICONEXCLAMATION);
delete m_pDB;
e->Delete();
return;
}
// 表定義信息結(jié)構(gòu)對象
CDaoTableDefInfo tabInfo;
// 得到表定義個(gè)數(shù)
int nTableDefCount = m_pDB->GetTableDefCount();
// 對表進(jìn)行枚舉
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 == "劃價(jià)記錄")
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()
{
// 關(guān)閉上次打開的記錄集
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);
// 構(gòu)造SQL查詢語句
CString strSQL = "SELECT * FROM " + m_sGetString;
// 用構(gòu)造的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();
// 設(shè)置列表框的擴(kuò)展風(fēng)格
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;
// 得到記錄集的字段數(shù)
m_nField = m_pRecordSet->GetFieldCount();
// 對各列進(jìn)行處理
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();
// 得到記錄數(shù)
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++)
{
// 用記錄光標(biāo)重定位到第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變量轉(zhuǎn)換為字符串變量
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;
}
// 設(shè)置各個(gè)項(xiàng)目
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("創(chuàng)建服務(wù)失敗,請重新運(yùn)行應(yīng)用程序!","錯(cuò)誤");
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進(jìn)行一些個(gè)性化設(shè)置,但用戶的設(shè)置會引起一些
// 誤操作,以下代碼設(shè)置WORD“ 工具-選項(xiàng)-視圖”中的一些選項(xiàng)。
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);
// 添加主治醫(yī)師
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("上一報(bào)告正在處理中,請?jiān)诋?dāng)前報(bào)表處理完畢后,再生成新的報(bào)表!", "錯(cuò)誤");
}
}
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("創(chuàng)建服務(wù)失敗,請重新運(yùn)行應(yīng)用程序!","錯(cuò)誤");
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 += "發(fā)票模板.dot";
// 模板文件路徑
_variant_t WordTemplate = strPath;
// 利用模板文件建立新文檔
WordDoc = WordDocs.Add(&WordTemplate, &vtMissing, &vtMissing, &vtMissing);
// 由于用戶會對WORD進(jìn)行一些個(gè)性化設(shè)置,但用戶的設(shè)置會引起一些
// 誤操作,以下代碼設(shè)置WORD“ 工具-選項(xiàng)-視圖”中的一些選項(xiàng)。
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("上一報(bào)告正在處理中,請?jiān)诋?dāng)前報(bào)表處理完畢后,再生成新的報(bào)表!", "錯(cuò)誤");
}
}
void CHospitalView::OnDestroy()
{
CFormView::OnDestroy();
// 終止COM庫服務(wù)函數(shù)
CoUninitialize();
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -