将ToolTip添加到ComboBoxEx失败

时间:2017-10-25 09:44:23

标签: c++ winapi combobox controls tooltip

考虑下面的代码,其中创建了2种不同类型的组合框(WC_COMBOBOX和WC_COMBOBOXEX),然后每个都附加了一个工具提示。

WC_COMBOBOX的工具提示按预期工作,但WC_COMBOBOXEX无法显示工具提示。

有什么问题?

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;
}

1 个答案:

答案 0 :(得分:5)

WC_COMBOBOXEX创建2个窗口 - 父和子组合框控件,其大小与父级相同,所有鼠标消息都发送给此子级,而不是父级。所以需要子类子组合框控件。我们可以通过CBEM_GETCOMBOCONTROL消息获取它。因此代码必须如下:

    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);