Search code examples
c++delphic++buildervcl

Make ShowHint work on custom control with several different Rects, each with their own Hint


Using C++ Builder 2009

I have a custom control that inherits from TCustomControl, on which I paint several squares (TRect) with content etc.

I now wanted to show Hint as I hover over every square, but I'm not sure how to implement this best.

I attempted to simply use TCustomControl's ShowHint, and change Hint as I hover over the squares, but the problem is that the control doesn't show hint anymore after it first disappears, unless I leave the control and come back to it. I hoped I could simply 'reset' it's state while hovering from one square to another but it doesn't work.

Assuming my approach is wrong to start with, kindly let me know what I should do to get the desired effect ?

if (State == rsHover && Item->FState != rsHover) // Not in the rsHover state yet, but going to
    {
    if (Item->Hint.Length())
        {
        if (ShowHint)
            {
            // Attempt to reset Hint's internal working, to no avail
            Hint = L"" ;
            ShowHint = false ;
            }

        Hint = Item->Hint ;

        ShowHint = true ;
        }
    else
        {
        ShowHint = false ;
        }
    }
else if (State != rsHover)
    {
    ShowHint = false ;
    }

Solution

  • The correct way to implement this feature is to make your component handle the CM_HINTSHOW message. The message's LParam value will be a pointer to a THintInfo record, whose fields you can freely modify as needed (in particular, HintStr and CursorRect).

    To access the record, you can either

    • type-cast the LParam directly to THintInfo*.

    • type-cast the entire TMessage to TCMHintShow, and then access its HintInfo field.

    By defining your own CursorRect rectangles, you can "[divide your control] into several hint regions", each with a different HintStr value. The CursorPos field indicates the mouse's current position within the control. When the mouse moves outside of the current CursorRect, the control will receive a new CM_HINTSHOW message, and you can update the CursorRect and HintStr fields as needed.