604
社区成员
发帖
与我相关
我的任务
分享
void __fastcall TForm1::FormCreate(TObject *Sender)
{
ComboBox1->Style = csOwnerDrawVariable;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ComboBox1MeasureItem(TWinControl *Control,
int Index, int &Height)
{
if (Index > -1) Height = 24;
}
//---------------------------------------------------------------------------
//
void __fastcall TForm1::ComboBox1DrawItem(TWinControl *Control, int Index,
TRect &Rect, TOwnerDrawState State)
{
TComboBox *cbx = dynamic_cast <TComboBox *> (Control);
// 填充背景,当前选择的行高亮
cbx->Canvas->Brush->Color = State.Contains(odSelected)? clHighlight: cbx->Color;
cbx->Canvas->FillRect(Rect);
// 字体颜色,可随意设置
cbx->Canvas->Font->Color = clBlack;
// 当前行的文本
String strText = cbx->Items->Strings[Index];
// 绘制文本的区域,左边略留一些空间
RECT rct(Rect);
rct.left += 2;
// 绘制文本,垂直居中
::DrawText(cbx->Canvas->Handle, strText.c_str(), strText.Length(), &rct, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
}
ComboBox1->Style = csOwnerDrawVariable; // 或 csOwnerDrawFixed
ComboBox1->ItemHeight = 24;