2

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.

2
  • Override the CalcHintRect function. The rectangle returned as a result of that function will be the hint size.
    – TLama
    Jul 13, 2012 at 19:19
  • Sorry, novice follow-up. Can you provide the code so I know how and where to implement? Jul 14, 2012 at 3:04

2 Answers 2

3

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;
15
  • 1
    @preah - Try 'Result := classes.Rect(..' Jul 14, 2012 at 12:16
  • 2
    @Preah - Call the inherited method first: Result:=inherited CalcHintRect(MaxWidth,Hint,AData);, then change Result.Left, Result.Right as you wish. Jul 14, 2012 at 12:23
  • 1
    @TLama - Maybe call the inherited method with only specifying MaxWidth is better? Jul 14, 2012 at 12:27
  • 1
    Setting Result.right and Result.bottom are in effect doing the same thing as TLama's solution. I want the height automatically set, but the width I set. Jul 14, 2012 at 12:29
  • 1
    No if the Hint parameter is different from ''. In that case the height is being calculated to fit the Hint text. If you would pass the '', then you would get (almost) the height of the screen. For you (if you say the width I set) will be enough to calculate the rectangle by yourself without inherited call.
    – TLama
    Jul 14, 2012 at 12:55
0

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.

1
  • Where do I place and call this procedure? Jul 14, 2012 at 3:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.