In the VC, the icon to the tree control is added to the tooltip, I never see the tool tips of the icon in any application. Sometimes I don't understand what I don't understand throughout the entire help document. If you can add tool tips to the icon in your own program, you will definitely increase the friendliness of the interface. In this paper, the tree control is used as an example, and the mechanisms provided in the VC are described in detail to implement the method of the icon tool prompt. ---- Step 1: Enable controls to display tooltips - call EnableTooltips (true) Make a window to display tooltips. Where is this code inserted? In the PRESUBCLASSWINDOW () of the class. Because no matter how the control is created, the MFC calls this function. And other functions are not necessarily called. Take OnCreate () as an example, if you call Create () or createEx (), onCreate () will be called, and if a control is created from the dialog resource, onCreate () will not be called. Implementation code as follows: void ctreectrlx :: PRESUBCLASSWINDOW () {cTreeCtrl :: PRESUBCLASSWINDOW (); enabletooltips (true);} ---- Step 2: Heavy Duty Duty OntoolHitTest () ---- MFC call function to determine Whether a point should be displayed. MSDN recommends that if the mouse falls at the point where the tooltip is displayed, the value 1 is returned. This is not completely correct. This function should return different values to distinguish the area in which you should display the prompt should be displayed. ---- In this function, this article only decesses the case where the mouse falls on the node icon or node status icon. The reader can add tool tips to the other elements of the tree according to their own situation. In both cases, the area of the icon is calculated, and the UID of the ToolInfo is set to the handle of the tree node where the mouse is located. Note that although the same ID is used for the node icon and node status icon, the return value is not the same. Different return values force the MFC update tooltip. ---- Although we can give tool tips in this function, because each movement of the mouse call this function, too much processing is not a good note, so we have handled what tips should be displayed in other functions. The problem.
Class declaration code as follows: // Overrides // ClassWizard generated virtual function overrides // {{AFX_VIRTUAL (CTreeCtrlX) protected: ... virtual int OnToolHitTest (CPoint point, TOOLINFO * pTI) const; //}} AFX_VIRTUAL implementation code as follows: int CTreeCtrlX :: OnToolHitTest (CPoint point, TOOLINFO * pTI) const {RECT rect; UINT nFlags; HTREEITEM hitem = HitTest (point, & nFlags); if (nFlags & TVHT_ONITEMICON) {CImageList * pImg = GetImageList (TVSIL_NORMAL) ImageInfo ImageInfo; Pimg-> GetImageInfo (0, & ImageInfo); GetItemRect (Hitem, & Rect, true); Rect.right = Rect.Lep - 2; Rect.Lcimage.right 2); PTI- > hwnd = m_hWnd; pTI-> uId = (UINT) hitem; pTI-> lpszText = LPSTR_TEXTCALLBACK; pTI-> rect = rect; return pTI-> uId;} else if (nFlags & TVHT_ONITEMSTATEICON) {CImageList * pImg = GetImageList ( TVSIL_NORMAL); IMAGEINFO imageinfo; pImg-> GetImageInfo (0, & imageinfo); GetItemRect (hitem, & rect, TRUE); rect.right = rect.left - (imageinfo.rcImage.right 2); pImg = GetImageList (TVSIL_STATE); Rect.Lep = Rect.right - ImageInfo.rcimage .right; pti-> hwnd = m_hwnd; pti-> uid = (uint) HITEM; PTI-> lpsztext = lpstr_textCallback; PTI-> Rect = Rect; // Return to Node icon Different value Return PTI-> UID * 2 } return -1;} ---- Step 3: Process the TTN_NeedText message; ---- Add a function to process the TTN_NeedText message notification. This message is issued when the tool processing control needs to know what information should be displayed. Since the last step we gave the lpsztext to be lpstr_textcallback, we have to handle this message VC ClassWizard does not support this message being mapped, so only the mapping mechanism we add this message is added to Message_Map. We have to handle two versions of this message, TTN_NeedTexta and TTN_NeedTexta.
Code Mapping message is as follows: BEGIN_MESSAGE_MAP (CTreeCtrlX, CTreeCtrl) // {{AFX_MSG_MAP (CTreeCtrlX) ... //}} AFX_MSG_MAPON_NOTIFY_EX_RANGE (TTN_NEEDTEXTW, 0, 0xFFFF, OnToolTipText) ON_NOTIFY_EX_RANGE (TTN_NEEDTEXTA, 0, 0xFFFF, OnToolTipText) END_MESSAGE_MAP () The following code is added to the class declaration: protected: // {{AFX_MSG (CTREECTRLX) ... //}} AFX_MSGAFX_MSG BOOL ONTOOLTIPTEXT (UINT ID, NMHDR * PNMHDR, LRESULT * PRESULT); DECLARE_MESSAGE_MAP () - - Discussion now This function itself is implemented. In order to adapt to different language character sets, the ANSI character set and Unicode character set must be processed, and the process will be somewhat different. Here, the Tooltip message generated by the itself of the tree control is not processed, and the principle of filtering out the above message is the ID of the message generated by the tree control itself is the handle of the tree control window, and there is a TTF_IDishWnd flag. Depending on the mouse location, you can determine the tooltip that should give a node icon or status icon. This article shows some meaningful tips as shown in the picture of the author, and the reader should add some meaningful tips when doing this. Of course, this article assumes that the control includes a node icon and a status icon. If not included, be careful not to calculate the error when calculating the mouse position.