How to make a list box with Office XP theme like selection rectangle?

Viewed 1344

What would be the most simple and clean way to show a focused/selected listbox item with a Office XP style?

See this sample image to show the idea more clearer:

enter image description here

I think I need to set the Listbox Style to either lbOwnerDrawFixed or lbOwnerDrawVariable and then modify the OnDrawItem event?

This is where I am stuck, I am not really sure what code to write in there, so far I tried:

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
  with (Control as TListBox).Canvas do
  begin
    if odSelected in State then
    begin
      Brush.Color := $00FCDDC0;
      Pen.Color   := $00FF9933;
      FillRect(Rect);
    end;

    TextOut(Rect.Left, Rect.Top, TListBox(Control).Items[Index]);
  end;
end;

I should of known that would not work, I get all kind of funky things going on:

enter image description here

What am I doing wrong, more importantly what do I need to change to make it work?

Thanks.

2 Answers
Related