How can I show hints for droppeddown TComboBox items in Delphi 10.3.3?

586 Views Asked by At

I have to show hints for the dropped down TComboBox items.

For this, I could use the OnMouseMove event to handle the hovering message. It takes the mouse X,Y client coordinates as parameters. If I could determine which is the first item to draw (dropdown list with a vertical scrollbar) then I could determine the item under the cursor using the ItemHeight value.

Is there any Win32 API call of message to get this value? The IDE does not support this information, or I couldn't find it.

1

There are 1 best solutions below

1
Brian On

One option is to use an cxOwnerDrawXXXX style ComboBox and the OnDrawItem event which tells you when you are drawing a focused item. I used this method myself once to update a static hint in a text box that provides more information on the focused / hovered over item.

Example putting hint text in a StaticText control:

procedure TForm1.ComboBox1CloseUp(Sender: TObject);
begin
  StaticText1.Caption := '';
end;

procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer; Rect:
    TRect; State: TOwnerDrawState);
begin
  // Update a hint on a static control
  if  (odSelected in State) and (Control as TComboBox).DroppedDown  then
    StaticText1.Caption := 'Hint for value:' + (Control as TComboBox).Items[Index];

  // Draw list item
  with (Control as TComboBox).Canvas do
  begin
    FillRect(Rect);
    TextOut(Rect.Left, Rect.Top, (Control as TComboBox).Items[Index]);
  end;
end;

Could be adapted to activate/show/store a hint instead of updating a static control.