문제

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.

도움이 되었습니까?

해결책

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;

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top