How can I show a hint that is specific to the item under the mouse as a user moves their mouse down a list of items in a TCheckListBox?
In Delphi 2010.
Tom
The way I do this is to make use of the TApplication.OnShowHint event. This allows you to customise the HintStr parameter and in you can do this based on the position contained in the HintInfo parameter.
As Remy points out in the comments, you can also handle CM_HINTSHOW to achieve the same effect and this can be cleaner to implement in some ways if you are already subclassing the standard VCL controls.
I have implemented a interface based framework to make use of this throughout my app. Basically in TApplication.OnShowHint, HintInfo.HintControl is asked if it supports this interface. If so then it is given an opportunity to customise the hint text. It works beautifully.
Basing hints off raw MouseMove events works perfectly well but it seems a little wasteful to call ItemAtPos on every MouseMove event rather than waiting until it's actually time to show the hint. That's why I have a slight preference for the approach described above.
Create a TCheckListBox with four items called "alpha", "beta", "gamma", and "delta" (for instance). Then do
const
CheckListBoxHints: array[0..3] of string = ('first hint', 'second hint', 'third hint', 'fourth hint');
var
prevIndex: integer = -1;
procedure TForm1.CheckListBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var
index: integer;
begin
index := CheckListBox1.ItemAtPos(point(X, Y), true);
if index <> -1 then
CheckListBox1.Hint := CheckListBoxHints[index]
else
CheckListBox1.Hint := '';
if index <> prevIndex then
Application.CancelHint;
prevIndex := index;
end;
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With