用 Windows API实现自己的多行文本的ToolTip

CToolTipCtrl使用的时候,需要在窗口的PreTranslateMessage中调用它的DelayEvent.
但 是有时候我们的窗口中的PreTranlateMessage不会被调用,那怎么办?

1. 调用CreateWindowEx创建自己的ToolTip窗口,并作相应的设置

    
::InitCommonControls();
      hToolTip = ::CreateWindowEx(0, “tooltips_class32”,
_T(""), TTS_ALWAYSTIP, 0, 0, 0, 0, hWnd, 0,
::AfxGetApp()->m_hInstance, 0);

      LONG delayTime;
      delayTime = 20000;
  
      ::SendMessage(hToolTip, TTM_SETMAXTIPWIDTH, 0,
240);
      ::SendMessage(hToolTip, TTM_SETDELAYTIME, (ttDelayReshow
& ttDelayMask), delayTime);

2.  为需要显示ToolTip的字窗口添加ToolTip

  
TOOLINFO ti;
   ti.cbSize = sizeof(ti);
   
   if( !GetToolTipInfo(hSubWnd, ti) )
   {
        ti.cbSize = sizeof(ti);
        ti.uFlags = TTF_SUBCLASS |
TTF_IDISHWND;
        ti.hwnd = hParent;
        ti.uId = (long)hSubWnd;
        ti.lpszText = NULL;
        ::SendMessage(hToolTip, TTM_ADDTOOL,
0, (LPARAM)&ti);

}

3. 设置需要显示的ToolTip文本

  TOOLINFO
ti;
    if( GetToolTipInfo(hSubWnd, ti) )
    {

       
ti.lpszText = new TCHAR[strText.GetLength()+1]; //
之前在strText 中插入相应的位置换行符
        wcscpy(ti.lpszText,
strText.GetBuffer());
        ::SendMessage( hToolTip,
TTM_UPDATETIPTEXT, 0, (LPARAM)&ti );
        delete[] ti.lpszText;

   
}

 

其中GetToolTipInfo可以这样实现:

 
nItems = ::SendMessage(hToolTip, TTM_GETTOOLCOUNT, 0, 0);

 
for( i = 0; i<nItems; i++ )
  {
     if( ::SendMessage(hToolTip, TTM_ENUMTOOLS, i,
(LPARAM)&ti) )
    {
        if( ti.uId == (long)hWnd)
        {
            return TRUE;
        }
    }
  }

  return FALSE;

原文地址:https://www.cnblogs.com/caoshenghe/p/1844947.html