這個例子舉例說明了如何與IE4的虛列表視一起使用一個Access數據庫文件。
加載大量數據到CListView派生類是一個非常慢的過程,甚至當數據在內存是也是如此。在數據庫中存取數據時就更慢了。一個列表視的新的特點是當它需要數據是再加載的能力。下面是具體步驟。
你必須安裝IE3以後版本的COMCTL32.DLL。
打開你的數據庫文件。
使用你的文檔對象打開你的數據庫文件和記錄集。在該例子中,我用了一個動態集類型的記錄集。這樣就允許我使用CDaoRecordSet的SetAbsolutePosition成員函數設置一個記錄集對象的當前記錄相對記錄號。
BOOL CVirtualListDoc::OnOpenDocumentFile(LPCTSTR lpszPathname)
{
CString m_FileName = lpszPathname;
pDBase = new CDaoDatabase;
pDBase->Open(m_FileName);
CString strSQL = "SELECT * FROM TableName ORDER BY ColumnName";//Set up SQL statement
pRecordSet = new CDaoRecordset(pDBase);
pRecordSet->Open(dbOpenDynaset, strSQL);//Open recordset using SQL statement
pRecordSet->MoveLast();//you have to access the records in the dynaset to get GCDaoRecordSet::etRecordCount() to work
}
設置該列表視的風格為LVS_OWNERDATA;
BOOL CVirtualListView::PreCreateWindow(CREATESTRUCT& cs)
{
cs.lpszName = WC_LISTVIEW;
cs.style &= ~LVS_TYPEMASK;
cs.style |= LVS_REPORT;
cs.style |= LVS_EDITLABELS;
cs.style |= LVS_OWNERDATA;
CListView::PreCreateWindow(cs);
}
在列表中設置項的個數。你需要告訴列表視將包括多少項。這通過發送一個LVN_SETITEMCOUNT到該列表控制。在列表視的OnInitialUpdate()成員函數中來完成上述工作,並且設置列表視的列數。你能夠發送一個LVM_SETEXTENDLISTVIEWSTYLE消息給該控件來設置一些新的擴展的列表視風格。
void CVirtualListView::OnInitialUpdate()
{
CListView::OnInitialUpdate();
/*set number of items in list to number of items in RecordSet*/
/* create image list*/
imageList.Create(IDB_IMAGELIST, 16, 1, RGB(0,0,0));
GetListCtrl().SetImageList(&imageList, LVSIL_SMALL);
/* set extended stlyes*/
DWORD dwExStyle = LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | /*LVS_EX_SUBITEMIMAGES |*/
LVS_EX_HEADERDRAGDROP | LVS_EX_TRACKSELECT;
GetListCtrl().SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LPARAM(dwExStyle));
LV_COLUMN lvColumn;
lvColumn.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
lvColumn.fmt = LVCFMT_LEFT;
lvColumn.cx = 120;
for(int i = 0; i < GetFieldCount(); i++) // set up columns
{
CDaoFieldInfo fieldinfo;
pRecordSet->GetFieldInfo(i, fieldinfo);//get field name
int len = fieldinfo.m_strName.GetLength();
CString temp = fieldinfo.m_strName;
TCHAR* szBuffer = new TCHAR[len + 1];
strcpy(szBuffer, temp.GetBuffer(len));
temp.ReleaseBuffer();
lvColumn.pszText = szBuffer;
GetListCtrl().InsertColumn(i, &lvColumn);//insert column
delete szBuffer;
}
/*set number of items in ListView*/
count = pRecordSet->GetRecordCount();//Get number of records
GetListCtrl().SendMessage(LVM_SETITEMCOUNT, (WPARAM)count, (LPARAM)LVSICF_NOINVALIDATEALL);
}
建立LVN_GETDISPINFO消息的處理函數:
void CVirtualListViewView::OnGetDispInfo(NMHDR* pNMHDR, LRESULT* pResult)
{
LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
TCHAR szValue[MAX_PATH];
COleVariant varValue;
long index = pDispInfo->item.iItem;
long subItem = pDispInfo->item.iSubItem;
if(pDispInfo->item.mask & LVIF_TEXT)
{
try
{
pRecordSet->SetAbsolutePosition(index);//Set the file to desired index
}
catch(CDaoException* e)
{
return;
}
try
{
if(subItem)
pRecordSet->GetFieldValue(subItem, varValue);
else
pRecordSet->GetFieldValue(0, varValue);
}
catch(CDaoException* e)
{
return;
}
const VARIANT* variant = LPCVARIANT(varValue);
switch(variant->vt)
{
case VT_I2:{ wsprintf(szValue, "%d", variant->iVal);
break;
}
case VT_I4:{ wsprintf(szValue, "%d", variant->lVal);
break;
}
case VT_R4:{ wsprintf(szValue, "%f", variant->fltVal);
break;
}
case VT_R8:{ wsprintf(szValue, "%f", variant->dblVal);
break;
}
case VT_CY:{ COleCurrency c(varValue);
CString s = c.Format();//ie. 1.00
strcpy(szValue, s.GetBuffer(s.GetLength()));
s.ReleaseBuffer();
break;
}
case VT_DATE:{ COleDateTime t(variant->date);
CString s = t.Format( "%A, %B %d, %Y" );//Day of Week, Month Day, Year
strcpy(szValue, s.GetBuffer(s.GetLength()));
s.ReleaseBuffer();
break;
}
case VT_BSTR:{ CString str = V_BSTRT( &varValue );//convert BSTR to CString
strcpy(szValue, str.GetBuffer(str.GetLength()));
str.ReleaseBuffer();
break;
}
case VT_BOOL:{ if(variant->boolVal)
strcpy(szValue, "TRUE");
else
strcpy(szValue, "FALSE");
break;
}
case VT_UI1:{ strcpy(szValue, (char*)variant->bVal);
break;
}
default: break;
}
lstrcpyn(pDispInfo->item.pszText, szValue, pDispInfo->item.cchTextMax);//set item text
}
if(pDispInfo->item.mask & LVIF_IMAGE)
pDispInfo->item.iImage = 0;//set image to first in list
*pResult = 0;
}