如何更改与TStringGrid单元格关联的提示文本的字体大小

3

我正在使用Lazarus v0.9.30(32位编译器)。 我有以下代码用于显示存储在TStringGrid中的TColumnTitle对象关联对象中的提示文本。

procedure TTmMainForm.TmApplicationPropertiesShowHint
    (
    var HintStr: string; 
    var CanShow: boolean; 
    var HintInfo: THintInfo
    );
var
  aGrid        : TStringGrid;
  aColumnTitle : TTmColumnTitle;
  aRow         : integer;
  aColumn      : integer;
begin
  aRow    := 0;
  aColumn := 0;

  HintInfo.HintMaxWidth := 200;
  HintInfo.HideTimeout  := 10000;
  HintInfo.HintColor    := $00D7FBFA;

  //Get a pointer to the current grid.
  aGrid := TStringGrid(HintInfo.HintControl);

  //Find out what cell the mouse is pointing at.
  aGrid.MouseToCell(HintInfo.CursorPos.X, HintInfo.CursorPos.Y, aColumn, aRow);

  if ((aRow = 0) and (aColumn < aGrid.ColCount)) then
    begin
      //Get the object associated with the column title.
      aColumnTitle := TTmColumnTitle(aGrid.Objects[aColumn, aRow]);

      //Define where the hint window will be displayed.
      HintInfo.CursorRect := aGrid.CellRect(aColumn, aRow);

      //Display the hint.
      HintStr := Trim(aColumnTitle.stHint);
    end; {if}
end;   

我可以访问HintInfo对象,并希望使用它来更改提示文本的字体大小。 HintInfo对象提供对HintInfo.HintControl.Font的访问,但是使用它会更改底层TStringGrid中所有单元格文本的字体。 HintInfo对象还提供对Hintinfo.HintWindowClass.Font的访问,但无法访问Font.Size。 是否有一种方法可以修改提示的字体大小?

1个回答

4
有一个名为TScreen.HintFont的属性,旨在实现此目的,但我认为其getter方法不正确。 我现在能说的是,它不能按预期工作。 并且由于您无法访问提示窗口实例,因此最好的方法是子类化公共提示窗口类。
在以下示例中,我创建了自定义提示窗口类,在该类中,您可以通过HintInfo.HintData传递字体大小值,该值当前未使用。
uses
  Windows, Types;

type
  TCustomHintWindow = class(THintWindow)
  private
    function CalcHintRect(MaxWidth: Integer; const AHint: string;
      AData: Pointer): TRect; override;
  end;

const
  HintBorderWidth = 2;

implementation

function TCustomHintWindow.CalcHintRect(MaxWidth: Integer; const AHint: string;
  AData: Pointer): TRect;
begin
  if MaxWidth <= 0 then
    MaxWidth := Screen.Width - 4 * HintBorderWidth;
  Result := Types.Rect(0, 0, MaxWidth, Screen.Height - 4 * HintBorderWidth);
  if AHint = '' then
    Exit;
  if Assigned(AData) then
    Canvas.Font.Size := Integer(AData);
  DrawText(Canvas.GetUpdatedHandle([csFontValid]), PChar(AHint), Length(AHint),
    Result, DT_CALCRECT or DT_NOPREFIX or DT_WORDBREAK);
  Inc(Result.Right, 4 * HintBorderWidth);
  Inc(Result.Bottom, 4 * HintBorderWidth);
end; 

procedure TForm1.ApplicationProperties1ShowHint(var HintStr: string;
  var CanShow: Boolean; var HintInfo: THintInfo);
begin
  HintInfo.HintColor := $0000ECFF;
  HintInfo.HintData := Pointer(12);
  HintStr := 'Hi I''m just a testing hint...';
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  HintWindowClass := TCustomHintWindow;
end;

这是一个界面截图: enter image description here

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接