2010-04-26 84 views

回答

5

是的,這個工作,實際上我做這個完全相同的東西,也在一個線圖圖表,但是有一些缺點/評論。消息處理有點不可思議,根據文檔不會發送一些消息,並且需要一些解決方法來保持控件獨立(不需要父級的幫助來反映通知)。

你要做的就是聲明一個變量在您的CWnd派生類

CToolTipCtrl m_ToolTipCtrl; 
CString m_ToolTipContent; 

然後做這個的OnCreate:

m_ToolTipCtrl.Create(this, TTS_ALWAYSTIP); 
m_ToolTipCtrl.Activate(TRUE); 

另外,您還可以設置延遲時間:

m_ToolTipCtrl.SetDelayTime(TTDT_AUTOPOP, -1); 
m_ToolTipCtrl.SetDelayTime(TTDT_INITIAL, 0); 
m_ToolTipCtrl.SetDelayTime(TTDT_RESHOW, 0); 

當你想顯示你的工具提示(推測在OnMouseMove()),使用

m_ToolTipCtrl.Pop(); 

但這隻適用於UNICODE構建。所以如果你仍然在MBCS上(像我一樣),你只能在延遲一段時間後才顯示工具提示。 使用此設置你的提示文本(也的OnMouseMove):

// Not using CToolTipCtrl::AddTool() because 
// it redirects the messages to the parent 
TOOLINFO ti = {0}; 
ti.cbSize = sizeof(TOOLINFO); 
ti.uFlags = TTF_IDISHWND; // Indicate that uId is handle to a control 
ti.uId = (UINT_PTR)m_hWnd; // Handle to the control 
ti.hwnd = m_hWnd;   // Handle to window 
// to receive the tooltip-messages 
ti.hinst = ::AfxGetInstanceHandle(); 
ti.lpszText = LPSTR_TEXTCALLBACK; 
ti.rect = <rectangle where, when the mouse is over it, the tooltip should be shown>; 
m_ToolTipCtrl.SendMessage(TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &ti); 
m_ToolTipCtrl.Activate(TRUE); 

m_ToolTipContent = "my tooltip content"; 

此外,你需要處理TTNNeedText:

// The build-agnostic one doesn't work for some reason. 
ON_NOTIFY_EX(TTN_NEEDTEXTA, 0, OnTTNNeedText) 
ON_NOTIFY_EX(TTN_NEEDTEXTW, 0, OnTTNNeedText) 

BOOL GraphCtrlOnTTNNeedText(UINT id, NMHDR* pTTTStruct, LRESULT* pResult) 
{ 
    TOOLTIPTEXT* pTTT = (TOOLTIPTEXT*)pTTTStruct; 
    //pTTT->lpszText = "some test text"; 
    //pTTT->lpszText = m_ToolTipContent; 
    strncpy_s(pTTT->lpszText, 80, m_ToolTipContent, _TRUNCATE); 

    return TRUE; 
} 

你必須修改此一點,閱讀文檔的功能和信息,讓你的項目能夠工作,但是可以做到。

+0

@Roel 我上線 'm_ToolTipCtrl.Create(這一點,TTS_ALWAYSTIP)除外;' 在MyCustomCtrl:在OnCreate() 還有什麼除了這個代碼中,我應該怎麼辦? 很難找到關於定製控件中包含的工具提示主題的任何內容。 – 2010-04-27 09:31:36

+0

嗯不知道。嘗試在.Create()之前放置一個:: IsWindow(m_hWnd),它應該是可以的,但也許是錯誤的。你在OnCreate()的開頭是否調用了CWnd :: OnCreate?你會得到什麼樣的例外?調試器在哪裏闖入? – Roel 2010-04-27 09:42:44

+0

是的,我打電話的CWnd :: m_ToolTipCtrl.Create(本) 它打破了上 'ttCtrl->創建(這一點,TTS_ALWAYSTIP)之前創建();' 有消息 '未處理的異常在TestApp 0x00418c29。 exe:0xC0000005:訪問衝突讀取位置0x00000000.' ,因爲它獲取NULL指針讀取。 如果我在'CWnd :: Create'和'MyCustomCtrl :: Create'之間交換呼叫順序,我得到同樣的錯誤 – 2010-04-27 09:52:23