Author Topic: Setting up tooltips for MFC controls  (Read 4398 times)

0 Members and 1 Guest are viewing this topic.

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6941
  • AKA Daniel
Setting up tooltips for MFC controls
« on: June 02, 2010, 01:29:09 AM »
the first thing you need to do is handle TTN_NEEDTEXT

I.e.
Code: [Select]
ON_NOTIFY_EX(TTN_NEEDTEXT,0,&YourClass::OnToolNeedText)

then add your member function, note the docs say TTN_NEEDTEXT handlers can ignore the pResult parameter, so I do.
here's a sample

Code: [Select]
BOOL CBlockManagerTabDlg::OnToolNeedText( UINT id, NMHDR* pNMHDR, LRESULT* pResult )
{
  if(pNMHDR)
  {
    //get the resource ID from sender
    UINT nID = ::GetDlgCtrlID((HWND)pNMHDR->idFrom);
    LPTOOLTIPTEXT lpTTT = (LPTOOLTIPTEXT)pNMHDR;

    //is window handle to the tool
    if ( !(lpTTT->uFlags & TTF_IDISHWND) )
      return FALSE;

    CToolTipCtrl* pToolTip = ::AfxGetModuleThreadState()->m_pToolTip;
    if (!pToolTip)
      return FALSE;

    //magically enables \r\n
    pToolTip->SetMaxTipWidth(SHRT_MAX);

    //init ptr
    LPCTSTR lpszTipMsg = NULL;

    //add our text(TCHAR[80] max) when we find our resource ID
    switch(nID)
    {
    case IDC_BUTTON_DSK_TOP:
      {
        lpszTipMsg = _T("Go to Desktop Folder");
      }
      break;
    case IDC_BUTTON_HOME:
      {
        lpszTipMsg = _T("Go to Default Folder");
      }
      break;
    case IDC_BUTTON_MY_DOC:
      {
        lpszTipMsg = _T("Go to My Documents Folder");
      }
      break;
    case IDC_LIST_BLKICON:
      {
        pToolTip->CenterWindow(CWnd::FromHandle((HWND)pNMHDR->idFrom));
        lpszTipMsg = _T("Double Click or Drag a block to insert");
      }
      break;
    case IDC_TREE_FLDR:
      {
        pToolTip->CenterWindow(CWnd::FromHandle((HWND)pNMHDR->idFrom));
        lpszTipMsg = _T("Double Click to insert\r\nDrag to open\r\nRight Click Folder to set as default");
      }
      break;
    } //switch

    if(!lpszTipMsg)
      return FALSE;

    //copy buf to tooltip
    ::lstrcpyn(lpTTT->szText,lpszTipMsg,sizeof(lpTTT->szText));
  }
  return TRUE;
}


« Last Edit: June 02, 2010, 01:32:53 AM by Daniel »

frtfff

  • Bull Frog
  • Posts: 215
Re: Setting up tooltips for MFC controls
« Reply #1 on: June 02, 2010, 04:25:36 AM »
 :-)

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6941
  • AKA Daniel
Re: Setting up tooltips for MFC controls
« Reply #2 on: September 08, 2010, 10:00:14 PM »
So I can find it later  :laugh:

For palettes use

Code: [Select]
ON_NOTIFY_EX_RANGE(TTN_NEEDTEXT,0,0xFFFF,&CPalette***ChildDlg::OnToolNeedText)

&&

Code: [Select]
this->EnableToolTips(TRUE);