Using a Virtual ClistView with a Dynaset

zhaozj2021-02-08  266

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

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

{

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 (WPAR_SETITETETET, (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_bst (& 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: {structure (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;

}

转载请注明原文地址:https://www.9cbs.com/read-1625.html

New Post(0)