Adding ToolTips to a List-View control


Adding a tooltip to an entry in a list-view is far simpler than MSDN would have you belive. In fact, the list-view control already supports tooltips, you just need to enable it by setting the extended list-view style and then setting the tooltip when Windows asks for one.

Given a dialog with a list-view control, the following code snippet shows what needs to be added to enable tooltips for each item:

char g_szToolTip[100] = {0};

BOOL CALLBACK DlgProc (
                        HWND hwndDlg,
                        UINT uMsg,
                        WPARAM wParam,
                        LPARAM lParam
                        )
{
    switch (uMsg)
    {
    case WM_INITDIALOG:
    {
        HWND hListView = GetDlgItem(hwndDlg, IDC_YOUR_LIST_VIEW_CONTROL);

        // LVS_EX_INFOTIP enables tooltips
        // LVS_EX_LABELTIP ensures the full tooltip is shown
        //    and is not partially hidden
        ListView_SetExtendedListViewStyle(hListView, LVS_EX_INFOTIP  |
                                                     LVS_EX_LABELTIP);
    }
    break;

    case WM_NOTIFY :
    {
        LPNMHDR lpnm = (LPNMHDR) lParam;

        switch (lpnm->code)
        {
            case LVN_GETINFOTIP:
                {
                    LPNMLVGETINFOTIP pGetInfoTip = (LPNMLVGETINFOTIP)lParam;

                    if (pGetInfoTip != NULL)
                    {
                        // this is where the tooltip text is set
                        g_szToolTip[0] = '\0';
                        StringCchPrintf(
                            g_szToolTip,
                            ARRAYSIZE(g_szToolTip),
                            "%s",
                            "Some helpful tooltip");

                        pGetInfoTip->pszText = g_szToolTip;
                    }

                }
                break;

            default:
                break;
        }
    }
    break;

    default:
        break;
    }

    return FALSE;
}

Check out the following links for more information about the list-view control and tooltips:

ListView_SetExtendedListViewStyle Macro
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/listview/macros/listview_setextendedlistviewstyle.asp

Extended List-View Styles
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/listview/ex_styles.asp

LVN_GETINFOTIP Notification
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/listview/notifications/lvn_getinfotip.asp

Comments (1)
  1. Alex says:

    This will not work, at least for me it didn't.

    You have to copy your text into the buffer pointed by pGetInfoTip->pszText, but not just assign a new value to this pointer.

Comments are closed.

Skip to main content