📄 listboxst.cpp
字号:
return nNewIndex;
} // End of InsertString
// Deletes a string from the list box.
//
// Parameters:
// [IN] nIndex
// Specifies the zero-based index of the string to be deleted.
//
// Return value:
// A count of the strings remaining in the list box.
// The return value is LB_ERR if nIndex specifies an index greater than
// the number of items in the list.
//
int CListBoxST::DeleteString(int nIndex)
{
int nRetValue = LB_ERR;
DeleteItemData(nIndex);
nRetValue = CListBox::DeleteString(nIndex);
return nRetValue;
} // End of DeleteString
// Replaces a string at a specific location in the list box.
//
// Parameters:
// [IN] nIndex
// Specifies the zero-based index of the position to replace the string.
// [IN] lpszItem
// Pointer to the null-terminated string that is to be replaced.
// [IN] nImage
// Image to be associated with the string.
// Pass -1L to associate no image.
//
// Return value:
// The zero-based index of the position at which the string was replaced.
// The return value is LB_ERR if an error occurs; the return value
// is LB_ERRSPACE if insufficient space is available to store the new string.
//
int CListBoxST::ReplaceString(int nIndex, LPCTSTR lpszString, int nImage)
{
int nRetValue;
nRetValue = DeleteString(nIndex);
if (nRetValue != LB_ERR)
{
nRetValue = InsertString(nIndex, lpszString, nImage);
} // if
return nRetValue;
} // End of ReplaceString
// Clears all the entries from the list box.
//
void CListBoxST::ResetContent()
{
FreeResources();
CListBox::ResetContent();
} // End of ResetContent
// Sets the 32-bit value associated with the list box item.
//
// Parameters:
// [IN] nIndex
// Specifies the zero-based index of the item.
// [IN] dwItemData
// Specifies the value to be associated with the item.
//
// Return value:
// LB_ERR if an error occurs.
//
int CListBoxST::SetItemData(int nIndex, DWORD dwItemData)
{
return ReplaceItemData(nIndex, dwItemData, NULL, 0, 0, MASK_DWDATA);
} // End of SetItemData
// Returns the 32-bit value associated with the list box item.
//
// Parameters:
// [IN] nIndex
// Specifies the zero-based index of the item.
//
// Return value:
// The 32-bit value associated with the item, or LB_ERR if an error occurs.
//
DWORD CListBoxST::GetItemData(int nIndex)
{
STRUCT_LBDATA* lpLBData = NULL;
lpLBData = (STRUCT_LBDATA*)CListBox::GetItemDataPtr(nIndex);
if (lpLBData != (LPVOID)-1L)
return lpLBData->dwItemData;
return (DWORD)LB_ERR;
} // End of GetItemData
// Sets a pointer to a list box item.
//
// Parameters:
// [IN] nIndex
// Specifies the zero-based index of the item.
// [IN] pData
// Specifies the pointer to be associated with the item.
//
// Return value:
// LB_ERR if an error occurs.
//
int CListBoxST::SetItemDataPtr(int nIndex, void* pData)
{
return ReplaceItemData(nIndex, 0, pData, 0, 0, MASK_LPDATA);
} // End of SetItemDataPtr
// Returns a pointer of a list box item.
//
// Parameters:
// [IN] nIndex
// Specifies the zero-based index of the item.
//
// Return value:
// Pointer associated with the item, or -1 if an error occurs.
//
void* CListBoxST::GetItemDataPtr(int nIndex)
{
STRUCT_LBDATA* lpLBData = NULL;
lpLBData = (STRUCT_LBDATA*)CListBox::GetItemDataPtr(nIndex);
if (lpLBData != (LPVOID)-1L)
return lpLBData->pData;
return (LPVOID)-1L;
} // End of GetItemDataPtr
int CListBoxST::Move(int nOldIndex, int nNewIndex, BOOL bSetCurSel)
{
int nInsertedIndex = LB_ERR;
CString sText;
STRUCT_LBDATA* lpLBData = NULL;
STRUCT_LBDATA csLBData;
// If index is out of range
if ((UINT)nOldIndex >= (UINT)GetCount()) return LB_ERR;
// Get item text
GetText(nOldIndex, sText);
// Get associated data
::ZeroMemory(&csLBData, sizeof(csLBData));
lpLBData = (STRUCT_LBDATA*)CListBox::GetItemData(nOldIndex);
if (lpLBData != (LPVOID)-1L)
{
::CopyMemory(&csLBData, lpLBData, sizeof(csLBData));
} // if
// Delete string
DeleteString(nOldIndex);
// Insert string at new position
nInsertedIndex = InsertString(nNewIndex, sText);
// Restore associated data
ReplaceItemData(nInsertedIndex, csLBData.dwItemData, csLBData.pData, csLBData.nImage, csLBData.dwFlags, MASK_ALL);
// Select item
if (bSetCurSel && nInsertedIndex != LB_ERR && nInsertedIndex != LB_ERRSPACE)
SetCurSel(nInsertedIndex);
return nInsertedIndex;
} // End of Move
// Moves a list box item up by one position
//
// Parameters:
// [IN] nIndex
// Specifies the zero-based index of the item.
// [IN] bSetCurSel
// If TRUE the item will be hilighted
//
// Return value:
// The zero-based index of the position at which the string was moved.
// The return value is LB_ERR if an error occurs; the return value
// is LB_ERRSPACE if insufficient space is available to store the string.
//
int CListBoxST::MoveUp(int nIndex, BOOL bSetCurSel)
{
int nRetValue = nIndex;
if (nIndex > 0)
{
nRetValue = Move(nIndex, nIndex - 1, bSetCurSel);
} // if
return nRetValue;
} // End of MoveUp
// Moves a list box item down by one position
//
// Parameters:
// [IN] nIndex
// Specifies the zero-based index of the item.
// [IN] bSetCurSel
// If TRUE the item will be hilighted
//
// Return value:
// The zero-based index of the position at which the string was moved.
// The return value is LB_ERR if an error occurs; the return value
// is LB_ERRSPACE if insufficient space is available to store the string.
//
int CListBoxST::MoveDown(int nIndex, BOOL bSetCurSel)
{
int nRetValue = nIndex;
if (nIndex < GetCount() - 1)
{
nRetValue = Move(nIndex, nIndex + 1, bSetCurSel);
} // if
return nRetValue;
} // End of MoveDown
// Moves a list box item to the top most position
//
// Parameters:
// [IN] nIndex
// Specifies the zero-based index of the item.
// [IN] bSetCurSel
// If TRUE the item will be hilighted
//
// Return value:
// The zero-based index of the position at which the string was moved.
// The return value is LB_ERR if an error occurs; the return value
// is LB_ERRSPACE if insufficient space is available to store the string.
//
int CListBoxST::MoveTop(int nIndex, BOOL bSetCurSel)
{
int nRetValue = nIndex;
if (nIndex > 0)
{
nRetValue = Move(nIndex, 0, bSetCurSel);
} // if
return nRetValue;
} // End of MoveTop
// Moves a list box item to the bottom most position
//
// Parameters:
// [IN] nIndex
// Specifies the zero-based index of the item.
// [IN] bSetCurSel
// If TRUE the item will be hilighted
//
// Return value:
// The zero-based index of the position at which the string was moved.
// The return value is LB_ERR if an error occurs; the return value
// is LB_ERRSPACE if insufficient space is available to store the string.
//
int CListBoxST::MoveBottom(int nIndex, BOOL bSetCurSel)
{
int nRetValue = nIndex;
if (nIndex < GetCount() - 1)
{
nRetValue = Move(nIndex, GetCount() - 1, bSetCurSel);
} // if
return nRetValue;
} // End of MoveBottom
// Enables or disables a list box item.
//
// Parameters:
// [IN] nIndex
// Specifies the zero-based index of the item.
// [IN] bEnable
// Specifies whether the given item is to be enabled or disabled.
// If this parameter is TRUE, the item will be enabled.
// If this parameter is FALSE, the item will be disabled.
// [IN] bRepaint
// If TRUE the control will be repainted.
//
void CListBoxST::EnableItem(int nIndex, BOOL bEnable, BOOL bRepaint)
{
STRUCT_LBDATA* lpLBData = NULL;
// Get pointer to associated datas (if any)
lpLBData = (STRUCT_LBDATA*)CListBox::GetItemDataPtr(nIndex);
if (lpLBData != NULL && lpLBData != (LPVOID)-1L)
{
if (bEnable)
ReplaceItemData(nIndex, 0, NULL, 0, (lpLBData->dwFlags & ~TEST_BIT0), MASK_DWFLAGS);
else
ReplaceItemData(nIndex, 0, NULL, 0, (lpLBData->dwFlags | TEST_BIT0), MASK_DWFLAGS);
if (bRepaint) Invalidate();
} // if
} // End of EnableItem
// Specifies whether a list box item is enabled.
//
// Parameters:
// [IN] nIndex
// Specifies the zero-based index of the item.
//
// Return value:
// TRUE if the item is enabled; otherwise FALSE.
//
BOOL CListBoxST::IsItemEnabled(int nIndex)
{
STRUCT_LBDATA* lpLBData = NULL;
// Get pointer to associated datas (if any)
lpLBData = (STRUCT_LBDATA*)CListBox::GetItemDataPtr(nIndex);
if (lpLBData != NULL && lpLBData != (LPVOID)-1L)
{
return !((lpLBData->dwFlags & TEST_BIT0) == TEST_BIT0);
//if (lpLBData->bDisabled) return FALSE;
} // if
return TRUE;
} // End of IsItemEnabled
// Sets how a selected list box item will be hilighted.
//
// Parameters:
// [IN] byRowSelect
// Selection type. Can be one of the following values:
// ST_FULLROWSELECT Hilight full list box item (Default)
// ST_FULLTEXTSELECT Hilight half list box item (Part containing text)
// ST_TEXTSELECT Hilight only list box text
// [IN] bRepaint
// If TRUE the control will be repainted.
//
void CListBoxST::SetRowSelect(BYTE byRowSelect, BOOL bRepaint)
{
switch (byRowSelect)
{
case ST_FULLROWSELECT:
case ST_FULLTEXTSELECT:
case ST_TEXTSELECT:
// Store new selection type
m_byRowSelect = byRowSelect;
if (bRepaint) Invalidate();
break;
default:
// Bad value
ASSERT(FALSE);
break;
} // switch
} // End of SetRowSelect
// Sets the image list to use in the list box.
//
// Parameters:
// [IN] pImageList
// Pointer to an CImageList object containing the image list
// to use in the list box. Pass NULL to remove any previous
// associated image list.
//
void CListBoxST::SetImageList(CImageList* pImageList)
{
m_pImageList = pImageList;
// Get icons size
if (m_pImageList)
ImageList_GetIconSize(*m_pImageList, (LPINT)&m_szImage.cx, (LPINT)&m_szImage.cy);
else
::ZeroMemory(&m_szImage, sizeof(m_szImage));
Invalidate();
} // End of SetImageList
// Sets the image to use in a list box item
//
// Parameters:
// [IN] nIndex
// Specifies the zero-based index of the item.
// [IN] nImage
// Specifies the zero-based index of the image
// inside the imagelist to use.
// [IN] bRepaint
// If TRUE the control will be repainted.
//
void CListBoxST::SetImage(int nIndex, int nImage, BOOL bRepaint)
{
ReplaceItemData(nIndex, 0, NULL, nImage, 0, MASK_NIMAGE);
if (bRepaint) Invalidate();
} // End of SetImage
// Returns the image index associated to a list box item.
//
// Parameters:
// [IN] nIndex
// Specifies the zero-based index of the item.
// [OUT] lpnImage
// Pointer to a int variable that will receive the index
// of the image inside the imagelist.
// This variable will be set to -1L if no image is associated.
//
void CListBoxST::GetImage(int nIndex, LPINT lpnImage)
{
STRUCT_LBDATA* lpLBData = NULL;
ASSERT(lpnImage != NULL);
if (lpnImage)
{
// Get pointer to associated datas (if any)
lpLBData = (STRUCT_LBDATA*)CListBox::GetItemDataPtr(nIndex);
if (lpLBData != NULL && lpLBData != (LPVOID)-1L)
*lpnImage = lpLBData->nImage;
else
*lpnImage = -1L;
} // if
} // End of GetImage
#undef MASK_DWDATA
#undef MASK_LPDATA
#undef MASK_NIMAGE
#undef MASK_DWFLAGS
#undef MASK_ALL
#undef TEST_BIT0
#undef LBST_CX_BORDER
#undef LBST_CY_BORDER
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -