抱歉,无法对how to turn off hottracking on Delphi ListView?发表评论,我开始提出新问题。
遵循上述链接的建议,但当我点击标题文本时,列表显示如下:
我在drawItem中设置了brush.color,并在每行中设置了drawSubItem:
Sender.Canvas.Brush.Color := mycolor
但如果我点击标题的文字,那么在标题区域中,只有文字部分有彩色背景。
代码:(运行环境:XE6,Win8.1)
TMyTaskListView = class(TListView)
protected
procedure CreateWnd; override;
end;
procedure TMyTaskListView.CreateWnd;
begin
inherited;
SetWindowTheme(WindowHandle, nil, nil);
end;
var
ListView1: TMyTaskListView;
Form.createForm:
ListView1.Columns.Add.Caption := 'TaskNo';
ListView1.Columns.Add.Caption := 'Task1';
ListView1.Columns.Add.Caption := 'Task2';
ListView1.Columns.Add.Caption := 'Task3';
ListView1.Columns.Add.Caption := 'Task Status';
ListView1.Items.Count := 5;
procedure TForm1.ListView1Data(Sender: TObject; Item: TListItem);
begin
Item.Caption := IntToStr(Item.Index);
Item.SubItems.Add('done');
Item.SubItems.Add('error');
Item.SubItems.Add('error');
Item.SubItems.Add('error');
end;
procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
if lstTaskItemCurClickBackgroundIndex = Item.Index then
Sender.Canvas.Brush.Color := $cc;
end;
procedure TForm1.ListView1CustomDrawSubItem(Sender: TCustomListView;
Item: TListItem; SubItem: Integer; State: TCustomDrawState;
var DefaultDraw: Boolean);
begin
if lstTaskItemCurClickBackgroundIndex = Item.Index then
Sender.Canvas.Brush.Color := $cc;
end;
procedure TForm1.ListView1Click(Sender: TObject);
var
ARect: TRect;
Idx: Integer;
SubItemIndex: Integer;
pt: TPoint;
item : TLIstItem;
hittestinfo: TLVHitTestInfo;
Rect: TRect;
begin
Idx := -1;
pt:= ListView1.ScreenToClient( mouse.cursorpos );
item := ListView1.GetItemAt( pt.x, pt.y );
If assigned( item ) then
Idx := item.Index
else
begin
FillChar( hittestinfo, sizeof( hittestinfo ), 0 );
hittestinfo.pt := pt;
If -1 <>ListView1.perform( LVM_SUBITEMHITTEST, 0, lparam(@hittestinfo))
Then
Begin
Idx := hittestinfo.iItem;
SubItemIndex := hittestinfo.iSubItem;
End
Else
if (ListView1.Items.Count > 0) then
Idx := 0;
end;
if Idx >= 0 then
begin
Rect := ListView1.Items[Idx].DisplayRect(drBounds);
if lstTaskItemPrevClickBackgroudColorRect <> Rect then
begin
InvalidateRect(ListView1.Handle, lstTaskItemPrevClickBackgroudColorRect, True);
InvalidateRect(ListView1.Handle, Rect, True);
end;
lstTaskItemPrevClickBackgroudColorRect := Rect;
lstTaskItemCurClickBackgroundIndex := Idx;
end;
end;