An (unsolvable?) TListView problem
Hello,
after trying a lot, I am a litle helpless with the following problem:
When I set
OwnerDraw := True
ViewStyle := vsReport
in a TListView, and the user resizes a column, Windows (?)
seems to forget to inform the component that it should repaint
its columns. The result is a very ugly listview.
Is there a way to solve this problem?
Here is my sample code to demonstrate the problem:
===============
// File LV.pas
unit LV;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls;
type
TForm1 = class(TForm)
ListView1: TListView;
procedure ListView1CustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
var
b: integer;
rs: TSize;
r, myrect: TRect;
begin
r := Item.DisplayRect(drBounds);
with Sender.Canvas do
begin
b := ListView1.columns[0].width;
rs := TextExtent(item.caption);
TextRect(r, b-3-rs.cx, r.top+1, item.caption);
myRect := r;
inc(myrect.left, b);
myRect.Right := myrect.left + ListView1.columns[1].width;
TextRect(myRect, myRect.left+2, MyRect.top+1, item.subitems[0]);
end;
DefaultDraw := false;
end;
end.
===============
// File LV.dfm
object Form1: TForm1
Left = 188
Top = 107
Width = 377
Height = 276
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object ListView1: TListView
Left = 10
Top = 6
Width = 347
Height = 237
Columns = <
item
Caption = 'Column 1'
end
item
Caption = 'Column 2'
end>
HotTrackStyles = []
Items.Data = {
B10000000200000000000000FFFFFFFFFFFFFFFF01000000000000001A414141
4141414141414141414141414141414141414141414141185757575757575757
5757575757575757575757575757575700000000FFFFFFFFFFFFFFFF01000000
0000000025424242424242424242424242424242424242424242424242424242
42424242424242424242265A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A
5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A}
OwnerDraw = True
TabOrder = 0
ViewStyle = vsReport
OnCustomDrawItem = ListView1CustomDrawItem
end
end
==============
Frank Heyne