Pergunta

I'm attempting to add a set of tooltips to a set of checkboxes on a dialog inheriting from CAxDialogImpl, where CSG32GridViewControlDlg is my dialog's class.

Using the example code on MSDN as a base, I've added the following code:

void CSG32GridViewControlDlg::AddCheckboxTooltip(const int toolId, PTSTR tooltipText)
{
    HWND hCheckbox = GetDlgItem(toolId);


    char label1[501];
    ::GetWindowText(hCheckbox, label1, 500);

    HINSTANCE hInstance = _AtlBaseModule.GetResourceInstance( ); // This bit I'm not sure about...

    // Need to create the ToolTip first
    HWND hWndToolTip = ::CreateWindowEx(NULL, TOOLTIPS_CLASS, NULL,
                    WS_POPUP | TTS_ALWAYSTIP | TTS_BALLOON,
                    CW_USEDEFAULT, CW_USEDEFAULT, 
                    CW_USEDEFAULT, CW_USEDEFAULT,
                    hCheckbox, 
                    NULL,
                    hInstance, 0);

    if (!hCheckbox || !hWndToolTip)
    {
        return;
    }                              

    TOOLINFO toolInfo = { 0 };
    toolInfo.cbSize = sizeof(toolInfo);
    toolInfo.hwnd    = hCheckbox;
    toolInfo.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
    toolInfo.uId    = (UINT_PTR)hWndToolTip;
    toolInfo.lpszText = tooltipText;

    LRESULT result = ::SendMessage(hWndToolTip, TTM_ADDTOOL, 0, (LPARAM) &toolInfo);
    return hWndToolTip;
}

Debugging the code, I can see that the message gets sent, and the result comes back as "1", which would seem to suggest that everything's worked successfully. But when I mouseover the checkbox in question... no tooltip.

What could I do to check if the tooltip is successfully registered, and why might it not be appearing on Mouseover? Alternatively, is there a better way to approach this?

Foi útil?

Solução

There are a few problems with your code. The starting point worth mentioning is obviosuly this: How to Create a Tooltip for a Control.

  1. Parent window for the tooltip control should be the dialog, not the checkbox
  2. uId member for the tool with TTF_IDISHWND flag needs to be the tool window, and not tooltip control window

This gives code:

HWND AddCheckboxTooltip(const int toolId, PTSTR tooltipText)
{
    HWND hCheckbox = GetDlgItem(toolId);

    // Need to create the ToolTip first
    HWND hWndToolTip = ::CreateWindowEx(NULL, TOOLTIPS_CLASS, NULL,
                    WS_POPUP | TTS_ALWAYSTIP | TTS_BALLOON,
                    CW_USEDEFAULT, CW_USEDEFAULT, 
                    CW_USEDEFAULT, CW_USEDEFAULT,
                    m_hWnd, 
                    NULL,
                    _AtlBaseModule.GetModuleInstance(), 0);

    if (!hCheckbox || !hWndToolTip)
        return NULL;

    TOOLINFO toolInfo = { 0 };
    toolInfo.cbSize = sizeof(toolInfo);
    toolInfo.hwnd    = hCheckbox;
    toolInfo.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
    toolInfo.uId    = (UINT_PTR)hCheckbox;
    toolInfo.lpszText = tooltipText;

    LRESULT result = ::SendMessage(hWndToolTip, TTM_ADDTOOL, 
        0, (LPARAM) &toolInfo);
    return hWndToolTip;
}

LRESULT OnInitDialog(UINT, WPARAM, LPARAM, BOOL& /*bHandled*/)
{
    AddCheckboxTooltip(IDC_CHECK1, _T("Checkbox Tooltip Test"));
    //...

Which runs as:

enter image description here

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top