1

For some reason, I want to highlight the subitem when mouse move over it, change its font to [fsUnderline, fsBold]. But when the mouse move away, the subitem's font change to its default state.

But I don't know how to realize this function. Can anyone give me some help?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
coo
  • 114
  • 9
  • Related, [Delphi TListview OwnerDraw SubItems - change default font (it's bold somehow after you Draw on the canvas)](http://stackoverflow.com/q/13485761/576719) and [Simple TListView OwnerDraw Text Font Size and Color example?](http://stackoverflow.com/q/9537433/576719). – LU RD Oct 25 '14 at 05:46
  • 3
    Set the `HotTrack` property of the ListView to true, then use the [OnAdvancedCustomDrawSubItem](http://docwiki.embarcadero.com/Libraries/XE6/en/Vcl.ComCtrls.TLVAdvancedCustomDrawSubItemEvent) event to draw the sub-items and check if the value is *cdsHot* for the [State](http://docwiki.embarcadero.com/Libraries/XE6/en/Vcl.ComCtrls.TCustomDrawState) property . – RRUZ Oct 25 '14 at 05:49
  • Thanks. But after I add the following code, and when I move mouse horizontally through different colomns, the subItem parameter is the same(=4, ie the last column.) procedure TCnCustBuildForm.lstTaskAdvancedDrawSubItem(Sender: TCustomListView; Item: TListItem; SubItem: Integer; State: TCustomDrawState; Stage: TCustomDrawStage; var DefaultDraw: Boolean); begin if (cdsHot in State) and (taskContent[Item.Index, SubItem] = 'error') then begin Sender.Canvas.Font.Style := Sender.Canvas.Font.Style + [fsUnderline, fsBold]; end end; – coo Oct 25 '14 at 07:18
  • You need `RowSelect` to be `True` for `HotTrack` to have the desired effect for sub items – David Heffernan Oct 25 '14 at 08:25
  • I have already set RowSelect to be True. And I find that when I move mouse vertically, the UI updates immedially. But horizontally, when I move mouse over next subitem, it's font updated after 7 seconds. Yes, it can change it's font after mouse over it, but slowly. – coo Oct 25 '14 at 11:20
  • Sorry, ask again: Move mouse in the same row, through different columns, how to update the FONT of new subitem in time? – coo Oct 25 '14 at 14:22
  • I don't know. I think you may need to do the tracking yourself. With OnMouseMove handling. – David Heffernan Oct 25 '14 at 20:55

1 Answers1

1

YES, with David's advice, I realize the MouseMove function, it runs:

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;
coo
  • 114
  • 9