📄 listbox.c
字号:
case LB_GETTEXT: { PLISTBOXITEM plbi; pData = (PLISTBOXDATA)(pWin->dwData);; plbi = lstGetItem (pData, (int)wParam); if (plbi) strcpy ((char*)lParam, plbi->key); else return LB_ERR; } case LB_SETTEXT: { PLISTBOXITEM plbi; char* newStr; pData = (PLISTBOXDATA)(pWin->dwData); plbi = lstGetItem (pData, (int)wParam); if (plbi) { newStr = strdup ((const char*)lParam); if (newStr) { free (plbi->key); plbi->key = newStr; InvalidateRect (hWnd, NULL, TRUE); } else return LB_ERR; } else return LB_ERR; break; } case LMSG_VSCROLL: pData = (PLISTBOXDATA)(pWin->dwData); switch((int)wParam){ case SB_LINEUP: pData->itemTop-=1; break; case SB_LINEDOWN: pData->itemTop+=1; break; case SB_THUMBTRACK: pData->itemTop = pWin->pVScroll->nPos; break; } winInvalidateRect(hWnd,NULL,true); break; default: return DefWindowProc(hWnd, iMsg, wParam, lParam); } return true;}static BOOL lstInitListBoxData ( LISTBOXDATA* pData, int len){ int i; PLISTBOXITEM plbi; pData->itemHeight = DEF_LB_ITEM_HEIGHT;//default list box item height pData->itemHilighted = 0; // init item buffer. if (!(pData->buffStart = malloc (len * sizeof (LISTBOXITEM)))) return FALSE; pData->buffLen = len; pData->buffEnd = pData->buffStart + len; pData->freeList = pData->buffStart; plbi = pData->freeList; //make a free list link table for (i = 0; i < len - 1; i++) { plbi->next = plbi + 1; plbi ++; } plbi->next = NULL; return TRUE;}static voidlstListBoxCleanUp( LISTBOXDATA* pData){ PLISTBOXITEM plbi; PLISTBOXITEM next; plbi = pData->head; while (plbi) { free (plbi->key); next = plbi->next; if (plbi < pData->buffStart || plbi > pData->buffEnd) free (plbi); plbi = next; } free (pData->buffStart);}static void lstResetListBoxContent ( PLISTBOXDATA pData){ int i; PLISTBOXITEM plbi, next; pData->itemCount = 0; pData->itemTop = 0; pData->itemHilighted = 0; plbi = pData->head; while (plbi) { free (plbi->key); next = plbi->next; if (plbi < pData->buffStart || plbi > pData->buffEnd) free (plbi); plbi = next; } pData->head = NULL; pData->freeList = pData->buffStart; plbi = pData->freeList; for (i = 0; i < pData->buffLen - 1; i++) { plbi->next = plbi + 1; plbi ++; } plbi->next = NULL; return;}static PLISTBOXITEM lstAllocItem ( PLISTBOXDATA pData){ PLISTBOXITEM plbi; if (pData->freeList) { plbi = pData->freeList; pData->freeList = plbi->next; } else plbi = (PLISTBOXITEM) malloc (sizeof (LISTBOXITEM)); return plbi;}static void lstFreeItem ( PLISTBOXDATA pData, PLISTBOXITEM plbi){ if (plbi < pData->buffStart || plbi > pData->buffEnd)//不在预分配区 free (plbi); else { plbi->next = pData->freeList; pData->freeList = plbi; }}//add a new item//dwStyle: sortable//pData : //newItem: //pos : position(pos<0 insert to begin of listbox)static int lstAddNewItem ( DWORD dwStyle, PLISTBOXDATA pData, PLISTBOXITEM newItem, int pos){ PLISTBOXITEM plbi; PLISTBOXITEM insPosItem = NULL; int insPos = 0; newItem->next = NULL; if (!pData->head) insPosItem = NULL; else if (dwStyle & LBS_SORT) { plbi = pData->head; if (strcmp (newItem->key, plbi->key) < 0) { insPosItem = NULL; insPos = 0; } else{ while (plbi->next) { if (strcmp (newItem->key, plbi->next->key) <= 0) break; plbi = plbi->next; insPos ++; } insPosItem = plbi; } } else { plbi = pData->head; if (pos < 0) { while (plbi->next) { plbi = plbi->next; insPos ++; } insPosItem = plbi; } else if (pos > 0) { int index = 0; while (plbi->next) { if (pos == index) break; plbi = plbi->next; index ++; insPos ++; } insPosItem = plbi; } } if (insPosItem) { plbi = insPosItem->next; insPosItem->next = newItem; newItem->next = plbi; insPos ++; } else { plbi = pData->head; pData->head = newItem; newItem->next = plbi; } pData->itemCount ++; return insPos;}static PLISTBOXITEM lstRemoveItem ( PLISTBOXDATA pData, int* pos){ int index = 0; PLISTBOXITEM plbi, prev; if (!pData->head) return NULL; if (*pos < 0) { //若pos<0,则移除最后一个节点 prev = pData->head; plbi = pData->head; while (plbi->next) { prev = plbi; plbi = plbi->next; index ++; } if (plbi == pData->head) { pData->head = pData->head->next; *pos = 0; return plbi; } else { prev->next = plbi->next; *pos = index; return plbi; } } else if (*pos == 0) { //如果Pos==0,则移除最前一个结点 plbi = pData->head; pData->head = plbi->next; return plbi; } else { //根据实际指定的位置移除 index = 0; prev = pData->head; plbi = pData->head; while (plbi->next) { if (*pos == index) break; prev = plbi; plbi = plbi->next; index ++; } if (plbi == pData->head) { pData->head = pData->head->next; *pos = 0; return plbi; } else { prev->next = plbi->next; *pos = index; return plbi; } } return NULL;}static PLISTBOXITEM lstGetItem ( PLISTBOXDATA pData, int pos){ int i; PLISTBOXITEM plbi; plbi = pData->head; for (i=0; i < pos && plbi; i++) plbi = plbi->next; return plbi;}static int lstFindItem ( PLISTBOXDATA pData, int start, char* key, BOOL bExact){ PLISTBOXITEM plbi; int keylen = strlen (key); if (start >= (pData->itemCount - 1)) start = 0; else if (start < 0) start = 0; plbi = lstGetItem (pData, start); while (plbi){ if (bExact && (keylen != strlen (plbi->key))) { plbi = plbi->next; start ++; continue; } if (strncasecmp (key, plbi->key, keylen) == 0) return start; plbi = plbi->next; start ++; } return LB_ERR;}static BOOLlstInvalidateUnderItem ( HWND hwnd, PLISTBOXDATA pData, int pos){ RECT rcInv; if (pos > (pData->itemTop + pData->itemVisibles)) return FALSE; if (pos <= pData->itemTop) { winInvalidateRect (hwnd, NULL, TRUE); return TRUE; } GetClientRect (hwnd, &rcInv); lstGetItemsRect (pData, pos, -1, &rcInv); if (rcInv.top < rcInv.bottom) winInvalidateRect (hwnd, &rcInv, TRUE); return TRUE;}static voidlstGetItemsRect ( PLISTBOXDATA pData, int start, int end, RECT* prc){ if (start < 0) start = 0; prc->top = (start - pData->itemTop)*pData->itemHeight; if (end >= 0) prc->bottom = (end - pData->itemTop + 1)*pData->itemHeight - 1;}static void lstSetVScrollInfo ( HWND hWnd, PLISTBOXDATA pData, BOOL fRedraw){ SCROLLINFO si; si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS; si.nMax = pData->itemCount - 1; si.nMin = 0; si.nPage = pData->itemVisibles; si.nPos = pData->itemTop; SetScrollInfo (hWnd, SB_VERT, &si, fRedraw); EnableScrollBar (hWnd, SB_VERT, TRUE);}static BOOLlstIsNofity( HWND hWnd){ PWindowsTree pWin; pWin = (PWindowsTree)hWnd; if(pWin->dwStyle & LBS_NOTIFY) return true; else return false;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -