2

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?

Community
  • 1
  • 1
Pascal Bergeron
  • 761
  • 3
  • 12
  • 27
  • 1
    I have a faint recollection of this being a known issue, caused by the new operating system. – Andreas Rejbrand Nov 07 '12 at 20:09
  • BTW, doesn't the world come to an end when you hover the first item on the coloured line? – Andreas Rejbrand Nov 07 '12 at 20:13
  • I can reproduce the issue with Delphi XE2. It is likely just the way ListView controls work on Windows 7. To change the behavior, you will likely have to set `DefaultDraw := False` and then custom-draw the entire background and text for each item as needed. – Remy Lebeau Nov 07 '12 at 20:22
  • I found a simple workaround, but doing any kind of painting in a `CustomDraw` causes a lot of strange rendering bugs, so I'll not even post it. – Andreas Rejbrand Nov 07 '12 at 20:28

1 Answers1

7

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

RRUZ
  • 134,889
  • 20
  • 356
  • 483