使用画布美化列表项

自定义绘制ListBox窗体

ListBox style 属性更改为 lbOwnerDrawFixed

运行后的效果

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
  var
  i:integer;
begin
  with control as Tlistbox do
  begin
    Canvas.FrameRect(ClientRect);          //填充ListBox窗体边框

  if oDSelected in State then              //处于选中状态
    begin
      Canvas.Brush.Color:=clRed;
      Canvas.RoundRect(Rect.Left,Rect.Top,Rect.Right,Rect.Bottom,0,0);
      setBkMode(Canvas.Handle,TRANSPARENT);
      Canvas.TextOut(Rect.Left, Rect.Top,Items.strings[index]);
    end else
    begin
      Canvas.Brush.Color:=clSkyBlue;
      Canvas.RoundRect(Rect.Left,Rect.Top,Rect.Right,Rect.Bottom,15,15);
      Canvas.TextRect(Rect,Rect.Left,Rect.Top,Items.strings[index]);
    end;

  end;
end;

 

posted @ 2014-06-05 22:23  拱卒  阅读(170)  评论(0)    收藏  举报