📄 lview.c
字号:
case LVN_ODFINDITEM:
// We should do a reverse look up here to see if
// an item exists for the text passed.
return -1;
}
}
return 0;
}
//----------------------------------------------------------------------
// DoCommandMain - Process WM_COMMAND message for window.
//
LRESULT DoCommandMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
WORD idItem, wNotifyCode;
HWND hwndCtl;
INT i;
// Parse the parameters.
idItem = (WORD) LOWORD (wParam);
wNotifyCode = (WORD) HIWORD (wParam);
hwndCtl = (HWND) lParam;
// Call routine to handle control message.
for (i = 0; i < dim(MainCommandItems); i++) {
if (idItem == MainCommandItems[i].Code)
return (*MainCommandItems[i].Fxn)(hWnd, idItem, hwndCtl,
wNotifyCode);
}
return 0;
}
//----------------------------------------------------------------------
// DoDestroyMain - Process WM_DESTROY message for window.
//
LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
PostQuitMessage (0);
return 0;
}
//======================================================================
// Command handler routines
//----------------------------------------------------------------------
// DoMainCommandExit - Process Program Exit command.
//
LPARAM DoMainCommandExit (HWND hWnd, WORD idItem, HWND hwndCtl,
WORD wNotifyCode) {
SendMessage (hWnd, WM_CLOSE, 0, 0);
return 0;
}
//----------------------------------------------------------------------
// DoMainCommandChView - Process View xxx command.
//
LPARAM DoMainCommandChView (HWND hWnd, WORD idItem, HWND hwndCtl,
WORD wNotifyCode) {
HWND hwndLV;
LONG lStyle;
hwndLV = GetDlgItem (hWnd, IDC_LISTVIEW);
lStyle = GetWindowLong (hwndLV, GWL_STYLE);
lStyle &= ~LVS_TYPEMASK;
switch (idItem) {
case IDC_LICON:
lStyle |= LVS_ICON;
break;
case IDC_SICON:
lStyle |= LVS_SMALLICON;
break;
case IDC_LIST:
lStyle |= LVS_LIST;
break;
case IDC_RPT:
lStyle |= LVS_REPORT;
break;
}
SetWindowLong (hwndLV, GWL_STYLE, lStyle);
return 0;
}
//----------------------------------------------------------------------
// DoMainCommandAbout - Process the Help | About menu command.
//
LPARAM DoMainCommandAbout(HWND hWnd, WORD idItem, HWND hwndCtl,
WORD wNotifyCode) {
// Use DialogBox to create modal dialog box.
DialogBox (hInst, TEXT ("aboutbox"), hWnd, AboutDlgProc);
return 0;
}
//======================================================================
// About Dialog procedure
//
BOOL CALLBACK AboutDlgProc(HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
switch (wMsg) {
case WM_COMMAND:
switch (LOWORD (wParam)) {
case IDOK:
case IDCANCEL:
EndDialog (hWnd, 0);
return TRUE;
}
break;
}
return FALSE;
}
//======================================================================
// Helper routines for list view control management
//----------------------------------------------------------------------
// AddItem - Add an item to the list view control.
//
INT AddItem (HWND hwndCtl, INT nItem, LPTSTR pszName, LPTSTR pszType,
INT nSize) {
LVITEM lvi;
TCHAR szTmp[40];
lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
lvi.iItem = nItem;
lvi.iSubItem = 0;
lvi.pszText = pszName;
lvi.iImage = 0;
lvi.lParam = nItem;
SendMessage (hwndCtl, LVM_INSERTITEM, 0, (LPARAM)&lvi);
lvi.mask = LVIF_TEXT;
lvi.iItem = nItem;
lvi.iSubItem = 1;
lvi.pszText = pszType;
SendMessage (hwndCtl, LVM_SETITEM, 0, (LPARAM)&lvi);
wsprintf (szTmp, TEXT ("%d"), nSize);
lvi.mask = LVIF_TEXT;
lvi.iItem = nItem;
lvi.iSubItem = 2;
lvi.pszText = szTmp;
SendMessage (hwndCtl, LVM_SETITEM, 0, (LPARAM)&lvi);
return 0;
}
//----------------------------------------------------------------------
// GetItemData - This routine returns a pointer to the data. It
// first checks the caches before calling directly to the database.
//
PLVDATAITEM GetItemData (INT nItem) {
INT nCacheIndex;
PLVDATAITEM pdi;
// See if it's in the top cache.
if (nItem < TOPCACHESIZE) {
nCacheIndex = nItem;
pdi = &lvdiTopCache[nCacheIndex];
}
// See if it's in the bottom cache.
else if (nItem > LVCNT - BOTCACHESIZE) {
nCacheIndex = nItem - (LVCNT - BOTCACHESIZE);
pdi = &lvdiBotCache[nCacheIndex];
}
// See if item's in the main cache.
else if ((nItem >= nCacheItemStart) &&
(nItem < nCacheItemStart + nCacheSize)) {
nCacheIndex = nItem - nCacheItemStart;
pdi = &lvdiCache[nCacheIndex];
}
// Otherwise it's not in any cache.
else
pdi = GetDatabaseItem (nItem);
return pdi;
}
//----------------------------------------------------------------------
INT LoadACache (PLVDATAITEM pCache, INT nStart, INT nSize) {
PLVDATAITEM pdi;
INT i;
for (i = 0; i < nSize; i++) {
// Get a pointer to the data.
pdi = GetDatabaseItem (nStart+i);
// Save the data in the cache.
lstrcpy (pCache[i].szName, pdi->szName);
lstrcpy (pCache[i].szType, pdi->szType);
pCache[i].nSize = pdi->nSize;
pCache[i].nImage = pdi->nImage;
pCache[i].nState = pdi->nState;
}
return 0;
}
//----------------------------------------------------------------------
// LoadMainCache - This routine loads the hint cache. If the
// recommended range is already in the top or bottom caches, the range
// is adjusted to grab items outside the end caches.
//
// The logic expects the total number of items to be greater than the
// size of the start and end caches.
//
INT LoadMainCache (INT nStart, INT nEnd) {
INT nOverlap;
// Size the hint range to fit the cache.
if (nEnd - nStart > CACHESIZE)
nEnd = nStart + CACHESIZE;
// See if end of hint in bottom cache.
if (nEnd > LVCNT - BOTCACHESIZE) {
// If completely in bottom cache, keep old data.
if (nStart > LVCNT - BOTCACHESIZE)
return 0;
// If partial overlap, adjust end points to get data just
// above the bottom cache.
nOverlap = nEnd - (LVCNT - BOTCACHESIZE);
nEnd = LVCNT - BOTCACHESIZE - 1;
if (nStart - nOverlap < TOPCACHESIZE)
nStart = TOPCACHESIZE;
else
nStart -= nOverlap;
}
// See if start of hint in top cache.
if (nStart < TOPCACHESIZE) {
// If completely in top cache, keep old data.
if (nEnd < TOPCACHESIZE)
return 0;
// Adjust the starting value to just beyond top cache end.
nOverlap = TOPCACHESIZE - nStart;
nStart = TOPCACHESIZE;
if (nOverlap + nEnd > (LVCNT - BOTCACHESIZE))
nEnd = LVCNT - BOTCACHESIZE;
else
nEnd += nOverlap;
}
// If hint already completely contained in the cache, exit.
if ((nStart >= nCacheItemStart) &&
(nEnd < nCacheItemStart + nCacheSize))
return 0;
// Flush old data in cache. We should really be smart here to
// see whether part of the data is already in the cache.
FlushMainCache ();
// Load the new data.
nCacheSize = nEnd - nStart;
nCacheItemStart = nStart;
LoadACache (lvdiCache, nStart, nCacheSize);
return 0;
}
//----------------------------------------------------------------------
INT LoadTopCache (void) {
LoadACache (lvdiTopCache, 0, TOPCACHESIZE);
return 0;
}
//----------------------------------------------------------------------
INT LoadBotCache (void) {
LoadACache (lvdiBotCache, LVCNT - BOTCACHESIZE, BOTCACHESIZE);
return 0;
}
//----------------------------------------------------------------------
void FlushMainCache (void) {
INT i;
// Send the data back to the database.
for (i = 0; i < nCacheSize; i++) {
SetDatabaseItem (nCacheItemStart+i, &lvdiCache[i]);
}
return;
}
//----------------------------------------------------------------------
void FlushEndCaches (void) {
INT i;
// Flush the top cache.
for (i = 0; i < TOPCACHESIZE; i++) {
SetDatabaseItem (i, &lvdiCache[i]);
}
// Flush the bottom cache.
for (i = 0; i < BOTCACHESIZE; i++) {
SetDatabaseItem (LVCNT - BOTCACHESIZE + i, &lvdiBotCache[i]);
}
return;
}
//======================================================================
// Code for fictional database to be displayed in the list view control
//
//----------------------------------------------------------------------
// InitDatabaseItem - Copy an item into the database.
//
INT InitDatabaseItem (INT nItem, LPTSTR pszName, LPTSTR pszType,
INT nSize) {
lstrcpy (lvdatabase[nItem].szName, pszName);
lstrcpy (lvdatabase[nItem].szType, pszType);
lvdatabase[nItem].nSize = nSize;
lvdatabase[nItem].nImage = 0;
lvdatabase[nItem].nState = 0;
return 0;
}
//----------------------------------------------------------------------
// InitDatabase - Create fictional data for fictional database.
//
void InitDatabase (void) {
TCHAR szName[64];
TCHAR szType[64];
HCURSOR hOldCur;
INT i;
hOldCur = SetCursor (LoadCursor (NULL, IDC_WAIT));
for (i = 0; i < LVCNT; i++) {
wsprintf (szName, TEXT ("File%d"), i);
wsprintf (szType, TEXT ("Type%d"), 1000 - i);
InitDatabaseItem (i, szName, szType, i+1000);
}
SetCursor (hOldCur);
return;
}
//----------------------------------------------------------------------
// GetDatabaseItem - Return a pointer to data in the database.
//
PLVDATAITEM GetDatabaseItem (INT nItem) {
// Normally, this would be more work. But since
// we have only a simulated data store, the
// code is trivial.
return &lvdatabase[nItem];
}
//----------------------------------------------------------------------
// SetDatabaseItem - Copy data from list view control back into database.
//
INT SetDatabaseItem (INT nItem, PLVDATAITEM pIn) {
lstrcpy (lvdatabase[nItem].szName, pIn->szName);
lstrcpy (lvdatabase[nItem].szType, pIn->szType);
lvdatabase[nItem].nSize = pIn->nSize;
lvdatabase[nItem].nImage = pIn->nImage;
lvdatabase[nItem].nState = pIn->nState;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -