响应LVN_ODFINDITEM.
以下为例子:
//This functions is called when the user "writes" in the list box to find an item.
void CVirtualListDlg::OnOdfinditemList(NMHDR* pNMHDR, LRESULT* pResult)
{
// pNMHDR has information about the item we should find
// In pResult we should save which item that should be selected
NMLVFINDITEM* pFindInfo = (NMLVFINDITEM*)pNMHDR;
/* pFindInfo->iStart is from which item we should search.
We search to bottom, and then restart at top and will stop
at pFindInfo->iStart, unless we find an item that match
*/
// Set the default return value to -1
// That means we didn't find any match.
*pResult = -1;
//Is search NOT based on string?
if( (pFindInfo->lvfi.flags & LVFI_STRING) == 0 )
{
//This will probably never happend...
return;
}
/*
Let's look on a sample list;
Name
Anders
* Anna
Annika
Bob
Emma
Emmanuel
Anna is selected.
If "A" is written, Annika should be selected.
If "AND" is written, Anders should be selected.
If "ANNK" is written, the selection should stay on Anna.
If "E" is written, Emma should be selected.
*/
//This is the string we search for
CString searchstr = pFindInfo->lvfi.psz;
// TRACE(_T("Find: %s\n"), searchstr);
int startPos = pFindInfo->iStart;
//Is startPos outside the list (happens if last item is selected)
if(startPos >= m_list.GetItemCount())
startPos = 0;
int currentPos=startPos;
//Let's search...
do
{
//Do this word begins with all characters in searchstr?
if( _tcsnicmp(m_database[currentPos].m_name, searchstr, searchstr.GetLength()) == 0)
{
//Select this item and stop search.
*pResult = currentPos;
break;
}
//Go to next item
currentPos++;
//Need to restart at top?
if(currentPos >= m_list.GetItemCount())
currentPos = 0;
//Stop if back to start
}while(currentPos != startPos);