CToolTipCtrl使用详细解说 - zhoubl668的专栏:远帆,梦之帆! - C...

来源:百度文库 编辑:神马文学网 时间:2024/04/19 09:37:02

ToolTip是Win32中一个通用控件,MFC中为其生成了一个类CToolTipCtrl,总的说来其使用方法是较简单的,下面讲一下它的一般用法和高级用法。

一般用法步骤:
1. 添加CToolTipCtrl成员变量 m_tt。
2. 在父窗口中调用EnableToolTips(TRUE);
3. 在窗口的OnCreate(或者其他适当的位置)中向ToolTip中添加需要显示Tip的子窗口,并同时指定相应的显示字串CToolTipCtrl::AddTool(pWnd,”string to display”)。
4. 重载父窗口的 BOOL PreTranslateMessage(MSG* pMsg) ,在函数中调用 m_tt.RelayEvent(pMsg)。

下面假设在窗口CWndYour中使用CToolTipCtrl

在类定义中添加变量说明:

  1. class CWndYour:xxx
  2. {
  3.  CToolTipCtrl m_tt;
  4. }

在OnCreate中添加需要显示Tip的子窗口

  1. CWndYour::OnCreate(....)
  2. {
  3.  EnableToolTips(TRUE);
  4.  m_tt.Create(this);
  5.  m_tt.Activate(TRUE);
  6.  
  7.  CWnd* pW=GetDlgItem(IDC_CHECK1);//得到窗口指针
  8.  m_tt.AddTool(pW,"Check1"); //添加
  9. }

在BOOL PreTranslateMessage(MSG* pMsg)中添加代码

  1. BOOL CWndYour::PreTranslateMessage(MSG* pMsg)
  2. {
  3.  {
  4.   m_tt.RelayEvent(pMsg);
  5.  }
  6.  return CParentClass::PreTranslateMessage(pMsg);
  7. }

这样当鼠标移动到相应的子窗口上时会显示出相应的ToolTip。

动态改变ToolTip的显示内容的方法及步骤:

1.上面所讲的1、2、4步骤。
2.在增加ToolTip时不指定显示的字串,而是使用LPSTR_TEXTCALLBACK。
3.在窗口中增加消息映射 ON_NOTIFY_EX( TTN_NEEDTEXT, 0, SetTipText )。
4.在窗口中增加一个函数用于动态提供显示内容,其原型为 BOOL SetTipText( UINT id, NMHDR * pTTTStruct, LRESULT * pResult ),下面的代码可以根据传入的参数判定应该显示的内容。

  1. BOOL CWndYour::SetTipText( UINT id, NMHDR * pTTTStruct, LRESULT * pResult )
  2. {
  3.  TOOLTIPTEXT *pTTT = (TOOLTIPTEXT *)pTTTStruct;   
  4.  UINT nID =pTTTStruct->idFrom; //得到相应窗口ID,有可能是HWND
  5.  if (pTTT->uFlags & TTF_IDISHWND)    //表明nID是否为HWND
  6.  {
  7.   nID = ::GetDlgCtrlID((HWND)nID);//从HWND得到ID值,当然你也可以通过HWND值来判断
  8.   switch(nID)
  9.   case(IDC_YOUR_CONTROL1)       
  10.    strcpy(pTTT->lpszText,your_string1);//设置
  11.    return TRUE;
  12.   break;
  13.   case(IDC_YOUR_CONTROL2)   //设置相应的显示字串
  14.    return TRUE;
  15.   break;
  16.  }
  17.  return(FALSE);
  18. }

作者: 闻怡洋 wyy_cq@188.net
原文: http://hi.baidu.com/fateyeah/blog/item/fc7c07b37ab250a7d9335aa7.html

/////////////////////////////////////////////////////////////////////////////////////////////////////////////

如何操纵CToolTipCtrl来给自己的控件添加tool tip呢?MSDN给出了答案。

创建并操纵一个CToolTipCtrl
创建一个CToolTipCtrl的对象.

调用Create函数来创建windows通用提示控件并使之与CToolTipCtrl对象产生关联。

调用AddTool函数来把tool tip control注册到一个tool上,这样存储在tool tip中的信息就能在光标悬浮在这个tool上的时候显示出来。

调用SetToolInfo来设置tool tip为这个tool所保留的信息。

调用SetToolRect来设置该tol的一个新的范围矩形。

调用HitTest函数来判断一个点是否在某个给定tool的范围矩形之内,如果是的话,就返回这个tool的信息。

调用GetToolCount来得到一个tool tip所关联到的tool的个数。

// Create and associate a tooltip control to the tab control of
// CMyPropertySheet.  CMyPropertySheet is a CPropertySheet-derived
// class.
BOOL CMyPropertySheet::OnInitDialog()
...{
   BOOL bResult = CPropertySheet::OnInitDialog();
  
   // Create a tooltip control.  m_ToolTipCtrl is a member variable
   // of type CToolTipCtrl* in CMyPropertySheet class.  It is
   // initialized to NULL in the constructor, and destroyed in the
   // destructor of CMyPropertySheet class.
   m_ToolTipCtrl = new CToolTipCtrl;// 第一步,创建对象
   if (!m_ToolTipCtrl->Create(this)) //第二步,调用Create函数
   ...{
      TRACE("Unable To create ToolTip ");          
      return bResult;
   }

   // Associate the tooltip control to the tab control
   // of CMyPropertySheet.
   CTabCtrl* tab = GetTabControl();
   tab->SetToolTips(m_ToolTipCtrl);
   // Get the bounding rectangle of each tab in the tab control of the
   // property sheet. Use this rectangle when registering a tool with
   // the tool tip control.  IDS_FIRST_TOOLTIP is the first ID string
   // resource that contains the text for the tool.
   int count = tab->GetItemCount();
   int id = IDS_FIRST_TOOLTIP;
   for (int i = 0; i < count; i++)
   ...{
      id += i;
      CRect r;
      tab->GetItemRect(i, &r);
      VERIFY(m_ToolTipCtrl->AddTool(tab, id, &r, id));
   }

   // Activate the tooltip control.
   m_ToolTipCtrl->Activate(TRUE);

   return bResult;
}

// Override PreTranslateMessage() so RelayEvent() can be
// called to pass a mouse message to CMyPropertySheet's
// tooltip control for processing.
BOOL CMyPropertySheet::PreTranslateMessage(MSG* pMsg)
...{
   if (NULL != m_ToolTipCtrl)           
      m_ToolTipCtrl->RelayEvent(pMsg);
  
   return CPropertySheet::PreTranslateMessage(pMsg);
}