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
在類定義中添加變量說明:
- class CWndYour:xxx
- {
- CToolTipCtrl m_tt;
- }
在OnCreate中添加需要顯示Tip的子窗口
- CWndYour::OnCreate(....)
- {
- EnableToolTips(TRUE);
- m_tt.Create(this);
- m_tt.Activate(TRUE);
- CWnd* pW=GetDlgItem(IDC_CHECK1);//得到窗口指針
- m_tt.AddTool(pW,"Check1"); //添加
- }
在BOOL PreTranslateMessage(MSG* pMsg)中添加代碼
- BOOL CWndYour::PreTranslateMessage(MSG* pMsg)
- {
- {
- m_tt.RelayEvent(pMsg);
- }
- return CParentClass::PreTranslateMessage(pMsg);
- }
這樣當鼠標移動到相應的子窗口上時會顯示出相應的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 ),下面的代碼可以根據傳入的參數判定應該顯示的內容。
- BOOL CWndYour::SetTipText( UINT id, NMHDR * pTTTStruct, LRESULT * pResult )
- {
- TOOLTIPTEXT *pTTT = (TOOLTIPTEXT *)pTTTStruct;
- UINT nID =pTTTStruct->idFrom; //得到相應窗口ID,有可能是HWND
- if (pTTT->uFlags & TTF_IDISHWND) //表明nID是否爲HWND
- {
- nID = ::GetDlgCtrlID((HWND)nID);//從HWND得到ID值,當然你也可以通過HWND值來判斷
- switch(nID)
- case(IDC_YOUR_CONTROL1)
- strcpy(pTTT->lpszText,your_string1);//設置
- return TRUE;
- break;
- case(IDC_YOUR_CONTROL2) //設置相應的顯示字串
- return TRUE;
- break;
- }
- return(FALSE);
- }
作者: 聞怡洋 [email protected]
原文:
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);
}