Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding ToolTip to a ComboBoxEx fails

Consider the code below where 2 different kinds of combo boxes are created(WC_COMBOBOX and WC_COMBOBOXEX), and then each is attached a tool tip.

Tool tip for WC_COMBOBOX works as expected, but WC_COMBOBOXEX fails to display the tool tip.

What is the problem?

BOOL TooltipDlg_OnInitDialog(HWND hWndDialog, HWND hWndFocus, LPARAM lParam)
{
    // Load and register Tooltip, ComboBox, ComboBoxEx control classes
    INITCOMMONCONTROLSEX iccx;
    iccx.dwSize = sizeof(INITCOMMONCONTROLSEX);
    iccx.dwICC = ICC_WIN95_CLASSES | ICC_USEREX_CLASSES;
    if (!InitCommonControlsEx(&iccx))
        return FALSE;

    // Create combo boxes
    const int idc_ComboBox = 1000;
    const int idc_ComboBoxEx = 1001;
    {
        // create WC_COMBOBOX
        CreateWindow(WC_COMBOBOX, NULL,
                     WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST,
                     40, 80,
                     100, 20, 
                     hWndDialog, (HMENU)idc_ComboBox, g_hInst,
                     NULL);
        // create WC_COMBOBOXEX
        CreateWindowEx(0, WC_COMBOBOXEX, NULL,
                       WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST,
                       40, 110,
                       100, 20,
                       hWndDialog, (HMENU)(idc_ComboBoxEx), g_hInst, 
                       NULL);
    }

    // Create tooltip
    g_hwndTooltip = CreateWindowEx(0, TOOLTIPS_CLASS, L"", 
                                   TTS_ALWAYSTIP, 
                                   0, 0, 0, 0, 
                                   hWndDialog, 0, g_hInst, 0);

    // attach the tooltip to controls
    {
        TOOLINFO ti;
        ti.cbSize = sizeof(ti);    
        ti.uFlags = TTF_IDISHWND | TTF_SUBCLASS;     

        // attach to idc_ComboBox -- works fine
        ti.uId = (UINT_PTR)GetDlgItem(hWndDialog, idc_ComboBox);
        ti.lpszText = L"This is tooltip for WC_COMBOBOX.";
        SendMessage(g_hwndTooltip, TTM_ADDTOOL, 0, (LPARAM)&ti);

        // attach to idc_ComboBoxEx -- does NOT work: no tooltip displayed
        ti.uId = (UINT_PTR)GetDlgItem(hWndDialog, idc_ComboBoxEx);
        ti.lpszText = L"This is tooltip for WC_COMBOBOXEX.";
        SendMessage(g_hwndTooltip, TTM_ADDTOOL, 0, (LPARAM)&ti);    
    }

    return TRUE;
}
like image 399
Kemal Avatar asked Oct 25 '17 09:10

Kemal


1 Answers

WC_COMBOBOXEX create 2 windows - parent and child combo box control, which have the same size as parent and all mouse messages go to this child, not for parent. so need subclass child combobox control. we can get it via CBEM_GETCOMBOCONTROL message. so code must look like:

    HWND hwndCBex = CreateWindowEx(0, WC_COMBOBOXEX, ...);
    ti.uId = (UINT_PTR)SendMessage(hwndCBex, CBEM_GETCOMBOCONTROL, 0, 0);
    ti.lpszText = L"This is tooltip for WC_COMBOBOXEX.";
    SendMessage(g_hwndTooltip, TTM_ADDTOOL, 0, (LPARAM)&ti);  
like image 130
RbMm Avatar answered Oct 05 '22 03:10

RbMm