分享
 
 
 

Using a Virtual CListView with a Dynaset

王朝vc·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

Using a Virtual CListView with a Dynaset

This article was contributed by Sam Fugarino.

This example illustrates how to use an Access database file with an IE 4.0 virtual list view.

Download sample project 63KB.

Loading large amounts of data into a CListView derived class is a very slow process, even when the data is maintained in memory. Accessing data in a database slows the process down even further. One of new features of a list view control is the ability to load data when it is needed. Below are the steps envolved.

You must have a version of COMCTL32.DLL that came with IE 3.0 or later.

Open up you database file.

Use your document object to open your database, and record set. In this example I use a dynaset type recordset. This allows me to set the relative record number of a recordset object's current record by using CDaoRecordSet's SetAbsolutePosition member function. Pass the a pointer to the recordset to your view class in an appropriate manner.

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

}

Set the ListView's style to 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);

}

Set number of items in list:

You have to tell the list view how many items it will contain. This is done by sending a LVM_SETITEMCOUNT to the list control. Do this and set up the list views columns in the views OnInitialUpdate() member function. You can send a LVM_SETEXTENDEDLISTVIEWSTYLE message to the control to set any of the new extended listview styles.

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);

}

Create a handler to handle LVN_GETDISPINFO messages:

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;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有