Visual C++ 7.1Visual C++ 8.0Visual C++ 7.0Visual Studio 2005Windows 2000Visual C++ 6.0Windows XPMFC中级开发Visual StudioWindowsC++
具有附加格式条件的multicolumn ComboBox
根据显示值具有附加格式条件的multicolumn ComboBox。

引言
一切始于对动态多列组合框的研究。我发现的在创建控件时,对行数或列数有限制。所以我决定开发一个更灵活的。
一切始于多列功能。后来,在我的工作中,我看到了与Janus GridEX控件一起开发的一个精彩功能。它提供了FormatConditions
来定义颜色和字体样式,具体取决于字段值,所以我实现了我自己的变体。
背景
有一些注意事项需要考虑
- 除了格式条件,您还可以定义一个默认格式条件(类型
FCO_Default
),如果找不到匹配的格式条件,则使用它。对于默认格式条件,Value1
指针可以为NULL
。 - 列可以定义为固定宽度或动态宽度。动态宽度由函数
CalcDropdownListWidth()
计算。因此,在更改或添加行之后,应调用此函数。 - 并非所有格式条件运算符对每种类型的值都有意义(
Between
对字符串没有意义,Contains
对数值没有意义),因此请谨慎处理...
Using the Code
- 在您的资源编辑器中,创建一个组合框并将所有者绘制属性设置为
Variable
,将包含字符串属性设置为true
,并将下拉类型设置为DropDownList
。 - 使用类向导在您的窗口中创建一个
CComboBox
对象。 - 在您的类文件中包含MCComboBox.h文件。
- 将
CComboBox
对象类型重命名为CMCComboBox
。 - 在
OnInitDialog()
和OnInitialUpdate()
中,您必须定义列。格式条件和行数据可以在稍后的代码中添加。
这是产生您在文章顶部看到的精彩下拉列表的代码片段。代码创建一个多列组合框,其中包含3列,3个格式条件,一个默认格式条件和30行数据
// Adding the columns
this->m_cboMultiColumn.Add_Column( );
this->m_cboMultiColumn.Add_Column( );
this->m_cboMultiColumn.Add_Column( );
// also use the format conditions in the edit control
this->m_cboMultiColumn.Set_ShowFormatConditionInEditField( true );
// add the format conditions
// red text and black background, if in column 0 the text "Row 3" appears
CFormatCondition *pFormatCondition =
this->m_cboMultiColumn.Create_FormatCondition( _T("TestCondition1"), 0,
CFormatCondition::FCO_Equal, new CMCCell( CString(_T("Row 3" ) ) ) );
if ( pFormatCondition != NULL )
{
pFormatCondition->Set_Bold( true );
pFormatCondition->Set_TextColor( RGB( 255, 0, 0 ) );
pFormatCondition->Set_BackColor( RGB( 0, 0, 0 ) );
}
// green text if in column 1 a date/time equal to 05.02.2007 12:00:00 appears
pFormatCondition = this->m_cboMultiColumn.Create_FormatCondition
( _T("Date 1"), 1, CFormatCondition::FCO_Equal,
new CMCCell( COleDateTime(2007, 2, 5, 12,0,0 ), CMCCell::CT_Time ) );
if ( pFormatCondition != NULL )
{
pFormatCondition->Set_TextColor( RGB( 0, 255, 0 ) );
}
// blue text if in column 1 a date/time greater than 05.02.2007 12:00:00 appears
pFormatCondition = this->m_cboMultiColumn.Create_FormatCondition
( _T("Date 2"), 2, CFormatCondition::FCO_GreaterThan, new CMCCell( (int)4 ) );
if ( pFormatCondition != NULL )
{
pFormatCondition->Set_TextColor( RGB( 0, 0, 255 ) );
}
// default format condition with golden text on dark red background,
// italic and striked out (uhhhhh.....)
pFormatCondition = this->m_cboMultiColumn.Create_FormatCondition
( _T("Default"), 2, CFormatCondition::FCO_Default, new CMCCell( (int)0 ) );
if ( pFormatCondition != NULL )
{
pFormatCondition->Set_TextColor( RGB( 255, 192, 64 ) );
pFormatCondition->Set_BackColor( RGB( 128, 0, 0 ) );
pFormatCondition->Set_Italic( true );
pFormatCondition->Set_StrikeThru( true );
}
// add 30 rows of data...
COleDateTime dtDateTime( 2007, 2,5,10,0,0);
for ( int nIndex = 0; nIndex < 30; nIndex++ )
{
CMCRow *pRow = new CMCRow( );
if ( pRow )
{
pRow->Set_ItemData( (long)( nIndex + 1 ) );
CString strText;
strText.Format( _T("Row %d"), nIndex, nIndex );
pRow->Set_CellValue( 0, strText );
pRow->Set_CellValue( 1, dtDateTime, CMCCell::CT_Time );
pRow->Set_CellValue( 2, nIndex );
this->m_cboMultiColumn.Add_Row( pRow );
}
dtDateTime += COleDateTimeSpan( 0, 1, 0, 0 );
}
// recalc the needed width for the dropdown list
this->m_cboMultiColumn.CalcDropdownListWidth( );
就是这样。玩得开心。