65.9K
CodeProject 正在变化。 阅读更多。
Home

CListBoxColorPickerST v1.1

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.56/5 (6投票s)

2001年10月7日

2分钟阅读

viewsIcon

79064

downloadIcon

2375

一个 CListBox 派生类, 用于从预定义列表中选择颜色

Sample Image

摘要

CListBoxColorPickerST 是一个基于 CListBox 类的派生类,在每个项目左侧显示彩色条。该控件在诸如工业过程监控等应用中非常有用,其中每个显示的对象可以根据其当前状态(运行、停止、故障...)具有不同的颜色;可以使用 CListBoxColorPickerST 开发颜色图例,以视觉上将每种可能的颜色与文本描述关联起来。

如何在应用程序中集成 CListBoxColorPickerST

在你的项目中包含以下文件

  • ListBoxColorPickerST.h
  • ListBoxColorPickerST.cpp
使用对话框编辑器创建一个标准的列表框,例如,命名为 IDC_LBXCOLORS。您需要设置以下属性

“Notify”属性不是必需的,但为了捕获列表框的消息,例如双击事件,它将是必需的。

然后为该列表框创建一个成员变量

CListBoxColorPickerST m_lbxColors;
现在将列表框附加到 CListBoxColorPickerST。对于基于对话框的应用程序,在 OnInitDialog 中
// Call the base-class method
CDialog::OnInitDialog();

// Create the IDC_LBXCOLORS list box
m_lbxColors.SubclassDlgItem(IDC_LBXCOLORS, this);
或者在 DoDataExchange 中
// Call the base method
CDialog::DoDataExchange(pDX);

// Create the IDC_LBXCOLORS list box
DDX_Control(pDX, IDC_LBXCOLORS, m_lbxColors);

类方法

AddString
将字符串添加到列表框。

// Parameters:
//     [IN]   lpszItem
//            Points to the null-terminated string that is to be added.
//     [IN]   crColor
//            Color to be associated with the string.
//
// Return value:
//     The zero-based index of the string in the list box.
//     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 AddString(LPCTSTR lpszItem, COLORREF crColor)
InsertString
在列表框中的特定位置插入字符串。
// Parameters:
//     [IN]   nIndex
//            Specifies the zero-based index of the position to insert the string.
//            If this parameter is -1, the string is added to the end of the list.
//     [IN]   lpszItem
//            Pointer to the null-terminated string that is to be inserted.
//     [IN]   crColor
//            Color to be associated with the string.
//
// Return value:
//     The zero-based index of the position at which the string was inserted.
//     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 InsertString(int nIndex, LPCTSTR lpszString, COLORREF crColor)
DeleteString
从列表框中删除字符串。
// 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 DeleteString(int nIndex)
ReplaceString
在列表框中的特定位置替换字符串。
// 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]   crColor
//            Color to be associated with the string.
//
// 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 ReplaceString(int nIndex, LPCTSTR lpszString, COLORREF crColor)
ResetContent
清除列表框中的所有条目。
void ResetContent()
SetColor
将颜色与列表框中特定位置的字符串关联。
// Parameters:
//     [IN]   nIndex
//            Specifies the zero-based index of the string.
//     [IN]   crColor
//            Color to be associated with the string.
//     [IN]   bRepaint
//            If TRUE the control will be repainted.
//
// Return value:
//     LB_ERR if an error occurs.
//
int SetColor(int nIndex, COLORREF crColor, BOOL bRepaint = TRUE)
GetColor
返回与列表框中特定位置的字符串关联的颜色。
// Parameters:
//     [IN]   nIndex
//            Specifies the zero-based index of the color to be retrieved.
//
// Return value:
//     The color associated with the specified string.
//     The return value is -1 if an error occurs.
//
COLORREF GetColor(int nIndex)
GetSelectedColor
返回与列表框中当前选定的字符串关联的颜色。
// Return value:
//     The color associated with the currently selected string.
//     The return value is -1 if an error occurs or no string is selected.
//
COLORREF GetSelectedColor()
SetItemData
设置与列表框项目关联的 32 位值。
// 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 SetItemData(int nIndex, DWORD dwItemData)
GetItemData
返回与列表框项目关联的 32 位值。
// 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 GetItemData(int nIndex)
SetItemDataPtr
设置指向列表框项目的指针。
// 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 SetItemDataPtr(int nIndex, void* pData)
GetItemDataPtr
返回指向列表框项目的指针。
// 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* GetItemDataPtr(int nIndex)
SelectColor
在列表框中搜索特定颜色。如果找到,则关联的
字符串将被选中。搜索将在颜色的第一次出现处结束。
// Parameters:
//     [IN]   crColor
//            Color to search for.
//
// Return value:
//     The zero-based index of the position at which the color was found.
//     The return value is LB_ERR if an error occurs or the spcified color
//     is not found.
//
int SelectColor(COLORREF crColor)
GetTextAndColor
返回列表框项目及其关联的颜色。
// Parameters:
//     [IN]   nIndex
//            Specifies the zero-based index of the item.
//     [OUT]  lpszBuffer
//            Pointer to the buffer that will receive the string.
//            The buffer must have sufficient space for the string and a 
//            terminating null character. The size of the string can be 
//            determined ahead of time by calling the GetTextLen member function.
//     [OUT]  rString
//            A reference to a CString object.
//     [OUT]  lpcrColor
//            Pointer to a COLORREF variable that will receive the color associated with
//            the specified item.
//
// Return value:
//     The length (in bytes) of the string, excluding the terminating null character. 
//     If nIndex does not specify a valid index, the return value is LB_ERR.
//
int GetTextAndColor(int nIndex, LPTSTR lpszBuffer, COLORREF* lpcrColor)
int GetTextAndColor(int nIndex, CString& rString, COLORREF* lpcrColor)

致谢

CListBoxColorPickerST 基于 James R. Twine 和 Mark Jackson (mark@mjsoft.co.uk) 的控件。

免责声明

软件和附带文件按“原样”分发,不提供任何形式的保证,无论是明示的还是暗示的。对于可能造成的损害或功能,不承担任何责任。用户必须承担使用此软件的全部风险。

SoftechSoftware 主页
SoftechSoftware 电子邮件

© . All rights reserved.