Setting size of hint window (THintWindow) in Delphi/Lazarus

人走茶凉 提交于 2019-12-12 17:06:45

问题


I am trying to make a custom hint in Lazarus. So far I have dynamically loaded the text in the hint, and customized the font face, font size, and font color. I would like to limit the width of the hint window. Any ideas? Here is my code.

type
  TExHint = class(THintWindow)
  constructor Create(AOwner: TComponent); override;

...

constructor TExHint.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  with Canvas.Font do
  begin
    Name  := 'Hanuman';
    Size  := Size + 3;
  end;
  //Canvas.Width := ;
end;

Thanks for any help.


回答1:


I have only Lazarus source and notepad now, but I'll try to explain you how the THintWindow is used since it's the most important to understand:

  • If you assign your class name to the HintWindowClass global variable, then you let's say register your hint window class for global usage by the application. Then every time the application will going to show hint, it will use your hint window class and call your overriden functions along with the functions from the base THintWindow class you didn't override. Here is how to register your hint window class for use in a scope of the application:

HintWindowClass := TExHint;
  • For getting the hint window size the application calls the CalcHintRect function whenever the hint is going to be shown. To adjust the hint window size by your own you need to override this function and
    as result return the bounds rectangle you want to have. If you wouldn't override it, the CalcHintRect function from the base class (from the THintWindow class) would be used, so you should override it:

type
  TExHint = class(THintWindow)
  public
    constructor Create(AOwner: TComponent); override;
    function CalcHintRect(MaxWidth: Integer; const AHint: String;
      AData: Pointer): TRect; override;
  end;

constructor TExHint.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  with Canvas.Font do
  begin
    Name := 'Hanuman';
    Size := Size + 3;
  end;
end;

function TExHint.CalcHintRect(MaxWidth: Integer; const AHint: String;
  AData: Pointer): TRect;
begin
  // here you need to return bounds rectangle for the hint
  Result := Rect(0, 0, SomeWidth, SomeHeight);
end;



回答2:


You should be able to override CreateParams and set the width to whatever you like.

procedure TExHint.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);
  Params.Width := X; 
end;

I haven't tested this but it should work.



来源:https://stackoverflow.com/questions/11472874/setting-size-of-hint-window-thintwindow-in-delphi-lazarus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!