?? rightlistview.cpp
字號:
// RightListView.cpp : implementation file
//
#include "stdafx.h"
#include "圖書館系統.h"
#include "RightListView.h"
#include "MainFrm.h"
#include "LeftTreeView.h"
#include "BOOKINFO.h"
#include "BOOkEDIT.h"
#include "BOOKQUERY.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CRightListView
IMPLEMENT_DYNCREATE(CRightListView, CFormView)
CRightListView::CRightListView()
: CFormView(CRightListView::IDD)
{
//{{AFX_DATA_INIT(CRightListView)
//}}AFX_DATA_INIT
}
CRightListView::~CRightListView()
{
}
void CRightListView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CRightListView)
DDX_Control(pDX, IDC_LIST, m_listCtrl);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CRightListView, CFormView)
//{{AFX_MSG_MAP(CRightListView)
ON_WM_SIZE()
ON_NOTIFY(NM_KILLFOCUS, IDC_LIST, OnKillfocusList)
ON_NOTIFY(NM_DBLCLK, IDC_LIST, OnDblclkList)
//}}AFX_MSG_MAP
ON_MESSAGE(LIST_EVERYONE,ListEveryOne)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRightListView diagnostics
#ifdef _DEBUG
void CRightListView::AssertValid() const
{
CFormView::AssertValid();
}
void CRightListView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CRightListView message handlers
void CRightListView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
CRect rect;
m_listCtrl.GetClientRect(&rect);
//設置列表控件風格
DWORD dwStyle=::GetWindowLong(m_hWnd,GWL_STYLE);
dwStyle|=LVS_REPORT|LVS_SHOWSELALWAYS|LVS_EDITLABELS;
::SetWindowLong(m_hWnd,GWL_STYLE,dwStyle);
dwStyle=m_listCtrl.GetExtendedStyle();
dwStyle|=LVS_EX_FULLROWSELECT;//LVS_EX_GRIDLINES|
//設置擴展風格
m_listCtrl.SetExtendedStyle(dwStyle);
m_listCtrl.InsertColumn(0, "圖書ID", LVCFMT_LEFT, rect.Width());
m_listCtrl.InsertColumn(1, "圖書名稱", LVCFMT_LEFT, rect.Width());
m_listCtrl.InsertColumn(2, "作者", LVCFMT_LEFT, rect.Width());
m_listCtrl.InsertColumn(3, "一級類型", LVCFMT_LEFT, rect.Width());
//二級類別不用來顯示,別有它用所以設為0
m_listCtrl.InsertColumn(4, "二級類型", LVCFMT_LEFT, 0);
//ON_MESSAGE(LIST_EVERYONE,ListEveryOne)
//afx_msg void ListEveryOne(WPARAM wParam,LPARAM lParam);
}
void CRightListView::OnSize(UINT nType, int cx, int cy)
{
CFormView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
CFormView::ShowScrollBar(SB_BOTH,FALSE);
if (GetSafeHwnd())
{
CRect rect;
GetClientRect(&rect);
if (m_listCtrl.GetSafeHwnd())
m_listCtrl.MoveWindow(&rect);
}
}
void CRightListView::OnDblclkList(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
CMainFrame* pMainFrm=(CMainFrame*)AfxGetMainWnd();
CLeftTreeView* pLeftView;
//尋找當前選中的記錄的位置
POSITION pos=m_listCtrl.GetFirstSelectedItemPosition();
//獲取當前記錄的位置游標
pLeftView=(CLeftTreeView*)pMainFrm->m_wndSplitter.GetPane(0,0);
int iIndex=m_listCtrl.GetNextSelectedItem(pos);
if(iIndex==-1)
{
return;
}
//獲取該項在數據庫中的序號
CString strNumber,strName,strfirClass;
strNumber=m_listCtrl.GetItemText(iIndex,0);
strName=m_listCtrl.GetItemText(iIndex,1);
strfirClass=m_listCtrl.GetItemText(iIndex,3);
//設置查詢語句
CString strSQL;
strSQL.Format("select * from 圖書信息情況 where 圖書ID='%s' ",\
strNumber);
//打開記錄集 選擇表名
if(!OpenRecordSet(m_pRecordset,strSQL))
{
AfxMessageBox("沒有成功打開數據表");
return;
}
//應該只有一條記錄
m_pRecordset->MoveFirst();
BOOKINFO dlg;
dlg.m_bookid=pLeftView->VariantToCString(m_pRecordset->GetCollect("圖書ID"));
dlg.m_bookname=pLeftView->VariantToCString(m_pRecordset->GetCollect("圖書名稱"));
dlg.m_bookwr=pLeftView->VariantToCString(m_pRecordset->GetCollect("作者"));
dlg.m_bookpub=pLeftView->VariantToCString(m_pRecordset->GetCollect("出版社"));
dlg.m_bookprice=pLeftView->VariantToCString(m_pRecordset->GetCollect("圖書價格"));
dlg.m_bookleixing1=pLeftView->VariantToCString(m_pRecordset->GetCollect("一級類型"));
dlg.m_bookleixing2=pLeftView->VariantToCString(m_pRecordset->GetCollect("二級類型"));
dlg.DoModal();
m_pRecordset->Close();
m_pRecordset=NULL;
*pResult = 0;
}
void CRightListView::OnKillfocusList(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
CMainFrame* pMainFrm=(CMainFrame*)AfxGetMainWnd();
//尋找當前選中的記錄的位置
POSITION pos=m_listCtrl.GetFirstSelectedItemPosition();
//獲取當前記錄的位置游標
int iIndex=m_listCtrl.GetNextSelectedItem(pos);
if(iIndex!=-1)
{
//使當前的選項落選,mask為LVIS_SELECTED|LVIS_FOCUSED,狀態設為0即可:)
m_listCtrl.SetItemState(iIndex,0, LVIS_SELECTED|LVIS_FOCUSED);
pMainFrm->bListSel=TRUE;
}
*pResult = 0;
}
BOOL CRightListView::OpenRecordSet(_RecordsetPtr &recPtr, CString &strSQL)
{
CMyApp* pApp=(CMyApp*)AfxGetApp();
//創建記錄集對象
m_pRecordset.CreateInstance(__uuidof(Recordset));
//在ADO操作中建議語句中要常用try...catch()來捕獲錯誤信息,
//因為它有時會經常出現一些想不到的錯誤
try
{
//從數據庫中打開表
recPtr->Open(strSQL.AllocSysString(),
pApp->m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch (_com_error e)
{
CString strError;
strError.Format("警告: 打開數據表時發生異常。 錯誤信息: %s",\
e.ErrorMessage());
AfxMessageBox(strError);
return FALSE;
}
return TRUE;
}
void CRightListView::ListEveryOne(WPARAM wParam,LPARAM lParam)
{
CString strWhere,strSQL;
//設置查詢條件
switch(wParam)
{
case 1://一級類別
{
//設置查詢條件
strWhere.Format("一級類型='%s'",m_strFirstClass);
strSQL="select * from 圖書信息情況 where "+strWhere;
break;
}
case 2://二級類別
{
//設置查詢條件
strWhere.Format("一級類型='%s' and 二級類型='%s'",\
m_strFirstClass,m_strSecondClass);
strSQL="select * from 圖書信息情況 where "+strWhere;
break;
}
case 3://查詢時
{
strSQL.Format("select * from 圖書信息情況 where %s",m_strWhere);
break;//
}
default://所有聯系人以及其它情況
strSQL="select * from 圖書信息情況";
break;//
}
ListShow(strSQL);
}
void CRightListView::ListShow(CString strSQL)
{
CMainFrame* pMainFrm=(CMainFrame*)AfxGetMainWnd();
CLeftTreeView* pLeftView;
pLeftView=(CLeftTreeView*)pMainFrm->m_wndSplitter.GetPane(0,0);
CTreeCtrl& treeCtrl=pLeftView->m_treeCtrl;
//當由于失去焦點而發生的變化則返回
if(treeCtrl.GetSelectedItem()==NULL)
{
return;
}
//這是改變后刪除,其實應該是改變前刪除才對
m_listCtrl.DeleteAllItems();
//打開數據表
if(!OpenRecordSet(m_pRecordset,strSQL))
{
return;
}
if(!m_pRecordset->BOF)
{
m_pRecordset->MoveFirst();
}
//循環插入
int i=0;
_variant_t varValue;
while(!m_pRecordset->adoEOF)
{
CString str;
m_listCtrl.InsertItem (i,str);
varValue=m_pRecordset->GetFields()->GetItem("圖書ID")->Value;
str=pLeftView->VariantToCString(varValue);
m_listCtrl.SetItemText (i, 0, str);
varValue=m_pRecordset->GetFields()->GetItem("圖書名稱")->Value;
str=pLeftView->VariantToCString(varValue);
m_listCtrl.SetItemText (i, 1, str);
varValue=m_pRecordset->GetFields()->GetItem("作者")->Value;
str=pLeftView->VariantToCString(varValue);
m_listCtrl.SetItemText (i, 2, str);
varValue=m_pRecordset->GetFields()->GetItem("一級類型")->Value;
str=pLeftView->VariantToCString(varValue);
m_listCtrl.SetItemText (i, 3, str);
varValue=m_pRecordset->GetFields()->GetItem("二級類型")->Value;
str=pLeftView->VariantToCString(varValue);
m_listCtrl.SetItemText (i, 4, str);
i++;
m_pRecordset->MoveNext();
}
m_pRecordset->Close();
m_pRecordset=NULL;
}
BOOL CRightListView::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
if(m_pRecordset!=NULL)
{
m_pRecordset->Close();
m_pRecordset=NULL;
}
//return CFrameWnd::DestroyWindow();
return CFormView::DestroyWindow();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -