比如将数据库查询打包到一个CDataBase类中,把查询到的数据显示到一个列表控件时,只要调用CDataBase类的一个成员函数CDataBase::InitListCtrl(CListCtrl*) ,在调用时,将目标的列表控件的指针作为参数提供,在CDataBase::InitListCtrl(CListCtrl*)函数内部,通过此指针来实现到控件的显示,在需要这样的地方,为列表控件绑定一个CListCtrl类型的控制变量即可。
BOOL CDlgConfList::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
//设置风格
LONG lStyle;
lStyle = GetWindowLong(m_ctlList1.m_hWnd, GWL_STYLE);//获取当前窗口style
lStyle &= ~LVS_TYPEMASK; //清除显示方式位
lStyle |= LVS_REPORT; //设置style
SetWindowLong(m_ctlList1.m_hWnd, GWL_STYLE, lStyle);//设置style
DWORD dwStyle = m_ctlList1.GetExtendedStyle();
dwStyle |= LVS_EX_FULLROWSELECT;//选中某行使整行高亮(只适用与report风格的listctrl)
dwStyle |= LVS_EX_GRIDLINES;//网格线(只适用与report风格的listctrl)
//dwStyle |= LVS_EX_CHECKBOXES;//item前生成checkbox控件
m_ctlList1.SetExtendedStyle(dwStyle); //设置扩展风格
m_ctlList1.InsertColumn( 0, "ID", LVCFMT_LEFT, 0 );//插入列,将宽度设置成0,在外观上就看不到此列,用来存放会员号
m_ctlList1.InsertColumn( 1, "会议室ID", LVCFMT_LEFT, 80 );
m_ctlList1.InsertColumn( 2, "会议室名称", LVCFMT_LEFT, 80 );
m_ctlList1.InsertColumn( 3, "容量", LVCFMT_LEFT, 60 );
m_ctlList1.InsertColumn( 4, "在线", LVCFMT_LEFT, 60 );
m_ctlList1.InsertColumn( 5, "开始", LVCFMT_LEFT, 160 );
m_ctlList1.InsertColumn( 6, "截止", LVCFMT_LEFT, 160 );
m_ctlList1.InsertItem(0, 0);//插入行
m_ctlList1.SetItemText(0, 1, "50");
m_ctlList1.SetItemText(0, 2, "公共聊天室");//设置数据
m_ctlList1.SetItemText(0, 3, "50");
m_ctlList1.SetItemText(0, 4, "5");
m_ctlList1.SetItemText(0, 5, "2010-12-1");
m_ctlList1.SetItemText(0, 6, "2010-12-1");
db1.InitListCtrl(&m_ctlList1);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
黄色部分是初始化对话框,并设置列表控件的样式,插入一行作个样子。
//把会议室列表的初始化打包到这儿
void CDataBase::InitListCtrl(CListCtrl* pListCtrl)
{
/*直接插入行测试指针
pListCtrl->InsertItem(0, 0);//插入行
pListCtrl->SetItemText(0, 1, "公共聊天室");//设置数据
pListCtrl->SetItemText(0, 2, "50");
pListCtrl->SetItemText(0, 3, "5");
pListCtrl->SetItemText(0, 4, "2010-12-1");
pListCtrl->SetItemText(0, 5, "2010-12-1");
*/
InitDataBase();//这是必须的
HRESULT hr;
_RecordsetPtr pRentRecordset;
hr=pRentRecordset.CreateInstance(__uuidof(Recordset));
if(FAILED(hr))
{
AfxMessageBox("createinstance of Recordset failed!\n can`t initiate List control!");
return;
}
CString strSql;
_variant_t var;
CString strValue;
int curItem=0;
strSql="SELECT * FROM Conf";
try
{
hr=pRentRecordset->Open(_variant_t(strSql),
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
if(SUCCEEDED(hr))
{
while(!pRentRecordset->adoEOF)
{
var = pRentRecordset->GetCollect((long)0);
if(var.vt != VT_NULL)
strValue = (LPCSTR)_bstr_t(var);
pListCtrl->InsertItem(curItem,strValue);
var = pRentRecordset->GetCollect("ConfID");
if(var.vt != VT_NULL)
strValue = (LPCSTR)_bstr_t(var);
pListCtrl->SetItemText(curItem,1,strValue);
var = pRentRecordset->GetCollect("ConfName");
if(var.vt != VT_NULL)
strValue = (LPCSTR)_bstr_t(var);
pListCtrl->SetItemText(curItem,2,strValue);
var = pRentRecordset->GetCollect("MaxNum");
if(var.vt != VT_NULL)
strValue = (LPCSTR)_bstr_t(var);
pListCtrl->SetItemText(curItem,3,strValue);
var = pRentRecordset->GetCollect("OnlineNum");
if(var.vt != VT_NULL)
strValue = (LPCSTR)_bstr_t(var);
pListCtrl->SetItemText(curItem,4,strValue);
var = pRentRecordset->GetCollect("StartTime");
if(var.vt != VT_NULL)
strValue = (LPCSTR)_bstr_t(var);
pListCtrl->SetItemText(curItem,5,strValue);
var = pRentRecordset->GetCollect("EndTime");
if(var.vt != VT_NULL)
strValue = (LPCSTR)_bstr_t(var);
pListCtrl->SetItemText(curItem,6,strValue);
pRentRecordset->MoveNext();
curItem++;
}
}
else
{
AfxMessageBox("Open recordset fail!");
}
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
return;
}
pRentRecordset->Close();
pRentRecordset=NULL;
}
红色部分为数据层,它的内部通过提供的列表控件指针来显示数据,数据库的查询用的是ADO