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

📄 rightlistview.cpp

📁 用MFC和Access开发的数据库系统
💻 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 + -