【问题标题】:Show tooltips for a TStringGrid cell显示 TStringGrid 单元格的工具提示
【发布时间】:2019-09-01 16:00:23
【问题描述】:

我正在尝试在每个单元格的 TStringGrid 控件上显示工具提示。目的是在单元格的内容不适合单元格时显示工具提示。现在我什至无法让工具提示显示鼠标悬停的单元格的正确内容。

到目前为止,我尝试的是使用 Application.hint 和 MyGrid.Hint 并将其设置为单元格的内容。看起来它总是在提示触发前半秒左右获取光标下单元格的内容。

甚至尝试设置 mousemove 事件并像这样设置工具提示:

procedure TForm1.GridMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Single);
var
  Row   : Integer;
  Column: TColumn;
  Col   : Integer;
  sHint : string;
begin
  Row := ProcessGrid.RowByPoint(X,Y);
  Column := ProcessGrid.ColumnByPoint(X,Y);
  if Assigned(Column) then
    Col := Column.Index
  else
    Col := -1;
  if (Row>-1) and (Col>-1) then
  begin
    sHint := ProcessGrid.Cells[Col,Row];
//    Application.Hint := sHint;
    ProcessGrid.Hint := sHint;
  end;
  Caption := Format('Col:%d Row:%d Hint: %s', [Col, Row, sHint]);
end;

上述代码表单中的标题显示了正确的单元格内容。然而,提示没有,我无法理解原因。一定是我忽略了一些简单的事情。

使用 Delphi 10.3.2 并尝试让它在 macOS 上运行。

【问题讨论】:

    标签: macos delphi firemonkey


    【解决方案1】:

    试试这个代码

    procedure TForm1.GridMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    var
      Row   : Integer;
      Col   : Integer;
    begin
      Grid.MouseToCell(X, Y, Col, Row);
    
      if (Row >= 0) and (Col >= 0) and (Grid.Hint <> Grid.Cells[Col, Row]) then
      begin
        Application.CancelHint;
        Grid.Hint:= Grid.Cells[Col,Row];
      end;
    end;
    

    当单元格的内容不适合单元格时,下一个代码会显示提示

    procedure TForm1.GridMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    var
      Row   : Integer;
      Col   : Integer;
    begin
      Grid.MouseToCell(X, Y, Col, Row);
    
      if (Row >= 0) and (Col >= 0) and (Grid.Hint <> Grid.Cells[Col, Row]) and
         (Grid.Canvas.TextWidth(Grid.Cells[Col, Row]) > Grid.ColWidths[Col]) then
      begin
        Application.CancelHint;
        Grid.Hint:= Grid.Cells[Col,Row];
      end;
    end;
    

    【讨论】:

      【解决方案2】:

      感谢@ghostdz!

      不得不为 Firemonkey 稍微调整一下。

      procedure TForm1.GridMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Single);
      var
        Row   : Integer;
        Column: TColumn;
        Col   : Integer;
      begin
        Row := Grid.RowByPoint(X,Y);
        Column := Grid.ColumnByPoint(X,Y);
        if Assigned(Column) then
          Col := Column.Index
        else
          Col := -1;
      
        Application.CancelHint;
        if (Row >= 0) and (Col >= 0) and (Grid.Hint <> Grid.Cells[Col, Row])
         and (Grid.Canvas.TextWidth(Grid.Cells[Col, Row]) > Column.Width)
         then
          Grid.Hint:= Grid.Cells[Col,Row];
        else
          Grid.Hint:= '';
      end;
      

      还是有点挑剔。 有时它不起作用。它也不是像素完美的,但我想现在必须这样做。

      【讨论】:

        猜你喜欢
        • 2015-08-30
        • 2014-12-08
        • 2018-10-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-20
        • 1970-01-01
        相关资源
        最近更新 更多