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。有没有办法修改提示的字体大小?

4

1 回答 1

4

TScreen.HintFont有用于此目的的属性,但是在我看来它的吸气剂是错误的。我现在可以说一件事,它没有按预期工作。并且由于您无权访问提示窗口实例,因此您可以做的最好的事情是继承公共提示窗口类。

在下面的示例中,我创建了自定义提示窗口类,您可以在其中通过传递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;

这是一个屏幕截图:

在此处输入图像描述

于 2012-03-26T13:34:47.333 回答