MFC and .NET Explorations

10 October 2004

Tooltips and List View

About tooltips and how to create per sub-item tooltips.

Create the tool tip control using CtoolTipCtrl::Create function.

Many tools (for instance subitems in a list control) can be attached to the same tool tip using AddTool. Each tool is given an id. It is possible to change the rect of the tool using SetToolRect. Can be usefull with a list control.

How to update tooltips when the columns of a ClistCtrl are resized?

Use OnCustomDraw to catch the repaint of a subitem as in:

void CListCtrlEx::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)

{

    LPNMLVCUSTOMDRAW  lplvcd = (LPNMLVCUSTOMDRAW)pNMHDR;

    int iCol = lplvcd->iSubItem;

    int iRow = lplvcd->nmcd.dwItemSpec;

 

    switch(lplvcd->nmcd.dwDrawStage)

    { 

          case CDDS_PREPAINT:

                *pResult = CDRF_NOTIFYSUBITEMDRAW|CDRF_NOTIFYPOSTPAINT;   // ask for subitem notifications.

                break;

          case CDDS_ITEMPREPAINT:

                // Necessary for CDDS_ITEMPREPAINT|CDDS_SUBITEM to be called

                *pResult = CDRF_NOTIFYSUBITEMDRAW|CDRF_NOTIFYPOSTPAINT;

                break;

          case CDDS_ITEMPREPAINT|CDDS_SUBITEM:

                *pResult = CDRF_NOTIFYPOSTPAINT | CDRF_NOTIFYSUBITEMDRAW | CDRF_DODEFAULT;

case CDDS_ITEMPREPAINT|CDDS_SUBITEM could be a good place to resize the tool for the tooltip.

First make sure that the tool exists using CToolTipCtrl::GetToolInfo then get the size of the current subitem with ClistCtrl::GetSubItemRect. Then call SetToolRect to change the registered rectangle of the tool.

Tried it… It works!