www.gusucode.com > ADO操作access全过程和CListCtrl的用法C++源码程序 > ADO操作access全过程和CListCtrl的用法/tele/Brow.cpp

    // Brow.cpp : implementation file
//

#include "stdafx.h"
#include "Tele.h"
#include "Brow.h"
#include "ado.h"
#include "teledlg.h"
#include "update.h"


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

/////////////////////////////////////////////////////////////////////////////
// CBrow dialog
CString CBrow::g_strItem;


CBrow::CBrow(CWnd* pParent /*=NULL*/)
	: CDialog(CBrow::IDD, pParent)
{
	//{{AFX_DATA_INIT(CBrow)
	m_strItem = _T("");
	m_strSelect = _T("");
	//}}AFX_DATA_INIT
}


void CBrow::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBrow)
	DDX_Control(pDX, IDC_LIST, m_ListCtrl);
	DDX_Text(pDX, IDC_EDIT_ITEM, m_strItem);
	DDX_Text(pDX, IDC_EDIT_SELECT, m_strSelect);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CBrow, CDialog)
	//{{AFX_MSG_MAP(CBrow)
	ON_BN_CLICKED(IDC_BUTTON_LOG, OnButtonLog)
	ON_BN_CLICKED(IDC_BUTTON_DEL, OnButtonDel)
	ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
	ON_BN_CLICKED(IDC_BUTTON_SELECT, OnButtonSelect)
	ON_BN_CLICKED(IDC_BUTTON_MODIFY, OnButtonModify)
	ON_NOTIFY(NM_CLICK, IDC_LIST, OnClickList)
	ON_NOTIFY(NM_RDBLCLK, IDC_LIST, OnRdblclkList)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBrow message handlers

void CBrow::OnButtonLog() 
{
	m_ListCtrl.DeleteAllItems();
	CADORecordset* pRs = new CADORecordset(CTeleApp::g_pDb);
	CString TempSql;
	int i = -1;
	if(pRs->Open((LPCTSTR)"SELECT *  FROM telelist"))
		//if(pRs->Open("SELECT *  FROM telelist WHERE name like '%张%' "))
	{
		while(!pRs->IsEof())
		{
			LV_ITEM lvitem;
			lvitem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE;
			lvitem.state = 0;      
			lvitem.stateMask = 0; 
			char temp[50];
			
			lvitem.iItem = ++i;
			lvitem.iSubItem = 0;
			lvitem.pszText =itoa(i, temp, 10); 
			m_ListCtrl.InsertItem(&lvitem);

			pRs->GetFieldValue("name",TempSql);
			m_ListCtrl.SetItemText(i, 1, (LPCTSTR)TempSql);

			pRs->GetFieldValue("tele",TempSql);
			m_ListCtrl.SetItemText(i, 2, (LPCTSTR)TempSql);

			pRs->GetFieldValue("remarks",TempSql);
			m_ListCtrl.SetItemText(i, 3, (LPCTSTR)TempSql);

			pRs->GetFieldValue("ID",TempSql);
			m_ListCtrl.SetItemText(i, 4, (LPCTSTR)TempSql);
			
			pRs->MoveNext();
		}
		pRs->Close();
	}
	else
		AfxMessageBox("记录集创建失败");
	delete pRs;	
}

BOOL CBrow::OnInitDialog()
{
	CDialog::OnInitDialog();

	LONG lStyle = m_ListCtrl.SendMessage
		(LVM_GETEXTENDEDLISTVIEWSTYLE);
	lStyle |= LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | 
		LVS_EX_HEADERDRAGDROP;
	m_ListCtrl.SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE, 0,
		(LPARAM)lStyle);
	LV_COLUMN lvc;
	
	lvc.mask = LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH /*| LVCF_FMT*/;
	
	lvc.iSubItem = 0;
	lvc.pszText = (char *)"序号";
	lvc.cx = 50;
	m_ListCtrl.InsertColumn(0,&lvc);
	
	lvc.iSubItem = 1;
	lvc.pszText = (char *)"姓名";
	lvc.cx = 100;
	m_ListCtrl.InsertColumn(1,&lvc);
	
	lvc.iSubItem = 2;
	lvc.pszText = (char *)"电话";
	lvc.cx =200;
	m_ListCtrl.InsertColumn(2,&lvc);
	
	lvc.iSubItem = 3;
	lvc.pszText = (char *)"备注";
	lvc.cx = 200;
	m_ListCtrl.InsertColumn(3,&lvc);

	lvc.iSubItem = 3;
	lvc.pszText = (char *)"标记号";
	lvc.cx = 100;
	m_ListCtrl.InsertColumn(4,&lvc);

	return TRUE;

}



void CBrow::OnButtonDel() 
{
	UpdateData(TRUE);

		if ( strcmp(m_strItem,"") == 0 )
	{
		AfxMessageBox("此处请填写标记号");
		return;
	}

	CString strSql
//		= "UPDATE tblTest SET Description = 'hi, Mars, this is from the earth' WHERE ID = 21";  //update
		= "DELETE FROM telelist WHERE ID = " + m_strItem ;
			
	((static_cast<CTeleApp *>(AfxGetApp()))->g_pDb)->Execute(strSql);
	OnButtonLog();

	
}

void CBrow::OnButtonAdd() 
{
	CTeleDlg test;
	test.DoModal();
}

void CBrow::OnButtonSelect() 
{
	UpdateData(TRUE);

	if ( strcmp(m_strSelect,"") == 0 )
	{
		AfxMessageBox("请输入你要查询的姓名");
		return;
	}

	CString strTemp;
	strTemp = "SELECT *  FROM telelist WHERE name like '%" + m_strSelect + "%'";
	m_ListCtrl.DeleteAllItems();
	CADORecordset* pRs = new CADORecordset(CTeleApp::g_pDb);
	CString TempSql;
	int i = -1;
	if(pRs->Open((LPCTSTR) strTemp ))
		
	{
		while(!pRs->IsEof())
		{
			LV_ITEM lvitem;
			lvitem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE;
			lvitem.state = 0;      
			lvitem.stateMask = 0; 
			char temp[50];
			
			lvitem.iItem = ++i;
			lvitem.iSubItem = 0;
			lvitem.pszText =itoa(i, temp, 10); 
			m_ListCtrl.InsertItem(&lvitem);

			pRs->GetFieldValue("name",TempSql);
			m_ListCtrl.SetItemText(i, 1, (LPCTSTR)TempSql);

			pRs->GetFieldValue("tele",TempSql);
			m_ListCtrl.SetItemText(i, 2, (LPCTSTR)TempSql);

			pRs->GetFieldValue("remarks",TempSql);
			m_ListCtrl.SetItemText(i, 3, (LPCTSTR)TempSql);

			pRs->GetFieldValue("ID",TempSql);
			m_ListCtrl.SetItemText(i, 4, (LPCTSTR)TempSql);
			
			pRs->MoveNext();
		}
		pRs->Close();
	}
	else
		AfxMessageBox("记录集创建失败");
	delete pRs;	
	
}

void CBrow::OnButtonModify() 
{
	
	UpdateData(TRUE);
	
		if ( strcmp(m_strItem,"") == 0 )
	{
		AfxMessageBox("请填写标记号");
		return;
	}

	CBrow::g_strItem = m_strItem;

	CUpdate test;
	test.DoModal();
}



void CBrow::OnClickList(NMHDR* pNMHDR, LRESULT* pResult) 
{

	 //-- Get the number of selected rows
	int nSelRows = m_ListCtrl.GetSelectedCount();

	if(!nSelRows) //-- If there are no rows selected,jump out here
		return;

	//-- Get the position of the first selected row
	POSITION pos =m_ListCtrl.GetFirstSelectedItemPosition();

	//-- Get the index of next selected row
	int i = m_ListCtrl.GetNextSelectedItem(pos);

	TRACE("First Item %d\n",i);

	if (i != -1) //-- Execute this loop as long as GetNextSelectedItem() returns -1
	
		
	

		m_strItem = m_ListCtrl.GetItemText(i, 4);
		
		UpdateData(FALSE);
	

	return;
		

	*pResult = 0;
}




void CBrow::OnRdblclkList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here

		// TODO: Add your control notification handler code here
	//m_strItem = itoa(HitTestEx(CPoint &point, int *col) const);
	
	 //-- Get the number of selected rows
	int nSelRows = m_ListCtrl.GetSelectedCount();

	if(!nSelRows) //-- If there are no rows selected,jump out here
		return;

	//-- Get the position of the first selected row
	POSITION pos =m_ListCtrl.GetFirstSelectedItemPosition();

	//-- Get the index of next selected row
	int i = m_ListCtrl.GetNextSelectedItem(pos);

	TRACE("First Item %d\n",i);

	while (i != -1) //-- Execute this loop as long as GetNextSelectedItem() returns -1
	{
		m_ListCtrl.DeleteItem(i);
		i = m_ListCtrl.GetNextSelectedItem(pos); //-- Get the index of the next selected row
		TRACE("Next Item %d\n",i);
	}

	return;
	
	*pResult = 0;
}