Before drawing the list, the control should have a
list of items (fonts) that will themselves be used as string. We know that
the VCL makes it extremely simply to fill out a control with the list of
fonts in the computer. For our example, we can use the OnCreate event of
the form to initialize the controls as follows:
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
// Fill out the control with the fonts of the computer
for(int i = 0; i < Screen->Fonts->Count; i++)
{
ListBox1->Items->Add(Screen->Fonts->Strings[i]);
ComboBox1->Items->Add(Screen->Fonts->Strings[i]);
}
int F = ComboBox1->Items->IndexOf("Times New Roman");
if( F ) // If the font exists, select it
ComboBox1->ItemIndex = F;
}
//---------------------------------------------------------------------------
The only thing left to do is to draw each item of the
list by changing its font. This can be taken care of in the OnDrawItem of
the control. Here is the implement of each of these events:
//---------------------------------------------------------------------------
void __fastcall TForm1::ListBox1DrawItem(TWinControl *Control, int Index,
TRect &Rect, TOwnerDrawState State)
{
if( State.Contains(odSelected) )
ListBox1->Canvas->Brush->Color = clHighlight;
else
ListBox1->Canvas->Brush->Color = clWhite;
ListBox1->Canvas->FillRect(Rect);
ListBox1->Canvas->Pen->Color = clWhite;
ListBox1->Canvas->Font->Name = ListBox1->Items->Strings[Index];
ListBox1->Canvas->Font->Size = 12;
ListBox1->Canvas->TextOut(Rect.Left, Rect.Top,
ListBox1->Items->Strings[Index]);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ComboBox1DrawItem(TWinControl *Control, int Index,
TRect &Rect, TOwnerDrawState State)
{
if( State.Contains(odSelected) )
ComboBox1->Canvas->Brush->Color = clHighlight;
else
ComboBox1->Canvas->Brush->Color = clWhite;
ComboBox1->Canvas->FillRect(Rect);
ComboBox1->Canvas->Font->Name = ComboBox1->Items->Strings[Index];
ComboBox1->Canvas->Font->Size = 12;
ComboBox1->Canvas->TextOut(Rect.Left, Rect.Top-2,
ComboBox1->Items->Strings[Index]);
}
//---------------------------------------------------------------------------
|
|
You can use this same technique in other controls. |