当鼠标悬停在TListView的子项上时,如何动态更改子项的字体?

时间:2014-10-25 05:30:29

标签: delphi

出于某种原因,我想在鼠标移过它时突出显示子项,将其字体更改为[fsUnderline, fsBold]。但是当鼠标移开时,子项的字体会变为默认状态。

但我不知道如何实现这个功能。谁能给我一些帮助?

1 个答案:

答案 0 :(得分:1)

是的,根据David的建议,我意识到MouseMove功能,它运行:

procedure TCnCustBuildForm.lstTaskMouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
var
Rect:TRect;
I, J: Integer;
S: string;
ItemWidth: Integer;
begin
if taskRowNum > 0 then
for J := 0 to lstTask.Items.Count - 1 do
begin
    Rect  := lstTask.Items[J].DisplayRect(drBounds);

    Rect.Left := Rect.Left + lstTask.Columns.Items[0].Width;

    for I := 1 to lstTask.Columns.Count - 2 do
    begin
      S := taskContent[J, I];

      ItemWidth := ListView_GetStringWidth(lstTask.Handle, PChar(S));

      //if ItemWidth > lstTask.Column[I].Width then
      //  ItemWidth := lstTask.Column[I].Width;

      if (taskContent[J, I] = 'error') and (x > Rect.Left) and (y > Rect.Top) and (x < Rect.Left + ItemWidth ) and (y < Rect.Bottom) then
      begin
          TListView(Sender).Cursor := crHandPoint;

          //InvalidateRect(lstTask.Handle, Rect, True);

          if lstTaskMouseMoveTrackingRect <> Rect then
          begin
            InvalidateRect(lstTask.Handle, lstTaskMouseMoveTrackingRect, True);
            InvalidateRect(lstTask.Handle, Rect, True);
          end;
          lstTaskMouseMoveTrackingRect := Rect;
          Exit;
      end;

      Rect.Left := Rect.Left + lstTask.Columns.Items[I].Width;
    end;
end;

TListView(Sender).Cursor := crDefault;
InvalidateRect(lstTask.Handle, lstTaskMouseMoveTrackingRect, True);
end;