Modifying the color of the selection rectangle in a ComboBox or Listbox 

The following example shows how to modify the selection rectangle in a ComboBox.  The same
code applies for a ListBox.  You must set either control to an owner-drawn style (i.e, set the Style property to xxOwnerDrawFixed or xxOwnerDrawVariable, where xx = cs for ComboBoxes or xx = lb for ListBoxes).
 

KEYWORDS: owner-drawn, OnDrawItem, FillRect
 


//---------------------------------------------------------------------------

void __fastcall TForm1::ComboBox2DrawItem(TWinControl *Control, 
    int Index, TRect &Rect, TOwnerDrawState State) 

    // eliminate artifacts
    ComboBox2->Canvas->FillRect(Rect); 

    // make the selection rectangle red for example
    if (State.Contains(odSelected)) 
    { 
        ComboBox2->Canvas->Brush->Color = clRed; 
        ComboBox2->Canvas->FillRect(Rect); 
    } 
    else ComboBox2->Canvas->Brush->Color = clHighlight; 

    // render the text
    ComboBox2->Canvas->TextOut(Rect.Left, Rect.Top,
                               ComboBox2->Items->Strings[Index]);