Pascal Bergeron
Pascal Bergeron

Reputation: 893

Why do I get white column separators on my custom-drawn listview?

Basically, I have the following Delphi 2007 code (CustomDrawItem):

procedure TForm1.ListViewCustomDrawItem(Sender: TCustomListView;
  Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
 if (Item.SubItems[0] = '...') then
  ListView.Canvas.Brush.Color := clSkyBlue;
end;

On my Windows XP, everything works perfectly:

On Windows XP

But on Windows 7, here's what I have:

On Windows 7

Now, of course, I'd like to know what's the right code to fill these vertical white stripes. But also, I'd like to know why is this happening. Does it come from Delphi? Windows 7? My code?

Upvotes: 2

Views: 1222

Answers (1)

RRUZ
RRUZ

Reputation: 136391

It seems a Windows 7 paint behavior, as workaround you can set ownerdraw property to true and use the OnDrawItem event instead.

Like so

uses
  CommCtrl;

{$R *.dfm}

procedure TForm7.ListView1DrawItem(Sender: TCustomListView; Item: TListItem;
  Rect: TRect; State: TOwnerDrawState);
var
 LIndex : integer;
 ListView :  TListView;
 LRect: TRect;
 LText: string;
begin
  ListView:=TListView(Sender);
  for LIndex := 0 to ListView.Columns.Count-1 do
  begin

    if not ListView_GetSubItemRect(ListView.Handle, Item.Index, LIndex, LVIR_BOUNDS, @LRect) then
       Continue;

    if Item.Selected and (not ListView.IsEditing) then
    begin
      ListView.Canvas.Brush.Color := clHighlight;
      ListView.Canvas.Font.Color  := clHighlightText;
    end
    else
    if (Item.SubItems[0] = '...') then
    begin
      ListView.Canvas.Brush.Color := clSkyBlue;
      ListView.Canvas.Font.Color  := ListView.Font.Color;
    end
    else
    begin
      ListView.Canvas.Brush.Color := ListView.Color;
      ListView.Canvas.Font.Color  := ListView.Font.Color;
    end;

    ListView.Canvas.FillRect(LRect);
    if LIndex = 0 then LText := Item.Caption
    else LText := Item.SubItems[LIndex-1];

    ListView.Canvas.TextRect(LRect, LRect.Left + 2, LRect.Top, LText);
  end;
end;

Windows 7

enter image description here

Windows XP

enter image description here

Upvotes: 7

Related Questions