Add a tooltip to the icon of the tree control in VC

Source: Internet
Author: User

I have never seen any icons in any application. Sometimes the entire help is checked.
The document does not understand what an icon means. If you can add a tool for the icon in your program
It will definitely make the interface more friendly. In this article, the tree control is used as an example to describe
Using the mechanism provided by MFC to implement the icon tooltip method.

---- Step 1: Enable the control to display tooltip

---- Call enabletooltips (true) to display a tooltip in a window. Where to insert
What is the best code? In presubclasswindow () of the class. Because no matter how a control is created
, MFC will call this function. Other functions may not be called. Take oncreate () as an example, for example
If you call create () or createex () to create a control, oncreate () is called.
The control is created from the dialog box resources, and oncreate () is not called.

The implementation code is as follows:
Void ctreectrlx: presubclasswindow ()
{
Ctreectrl: presubclasswindow ();

Enabletooltips (true );
}

---- Step 2: overload the virtual function ontoolhittest ()
---- MFC calls a function to determine whether a tooltip should be displayed at a certain point. Msdn recommends that you move the cursor
Return Value 1 at the point where the tooltip should be displayed. This is not completely correct. This function should return different
To distinguish different areas in the window that should display the prompt.

---- In this function, this article only deals with the mouse over the node icon or node status icon. Read
You can add a tooltip to other elements of the tree as needed. In both cases
Calculate the icon area, and set the toolinfo UID as the handle of the Tree node of the point where the mouse is located. Note:
The same ID is used for the node icon and node status icon, but the return value is different. Different
The returned value forces the prompt of the MFC update tool.

---- Although we can give a tooltip in this function, this is called every time the mouse moves.
Function, too much processing is not a good note, so we should display
Question.

The code in the class declaration is as follows:
// Overrides
// Classwizard generated
Virtual function overrides
// {Afx_virtual (ctreectrlx)
Protected:
...
Virtual int ontoolhittest
(Cpoint point, toolinfo * PTI) const;
//} Afx_virtual

The implementation code is as follows:
Int ctreectrlx: ontoolhittest
(Cpoint point, toolinfo * PTI) const
{
Rect;

Uint nflags;
Htreeitem hitem = hittest (point, & nflags );
If (nflags & tvht_onitemicon)
{
Cimagelist * pimg = getimagelist (tvsil_normal );
Imageinfo;
Pimg-> getimageinfo (0, & imageinfo );

Getitemrect (hitem, & rect, true );
Rect. Right = rect. Left-2;
Rect. Left-= (imageinfo. rcimage. 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;
Pimg-> getimageinfo (0, & imageinfo );

Getitemrect (hitem, & rect, true );
Rect. Right = rect. Left-
(Imageinfo. rcimage. Right + 2 );

Pimg = getimagelist (tvsil_state );
Rect. Left = rect. Right-imageinfo. rcimage. Right;

PTI-> hwnd = m_hwnd;
PTI-> uid = (uint) hitem;
PTI-> lpsztext = lpstr_textcallback;
PTI-> rect = rect;

// Return a value different from the node icon
Return PTI-> uid * 2;
}
Return-1;
}

---- Step 3: process the ttn_needtext message;
---- Add a function to process the ttn_needtext Message notification. When the tool processing control needs to know it should display
When the message is displayed, the message is sent. As we assigned a value to the lpsztext of toolinfo in the previous step
It is lpstr_textcallback. Therefore, classwizard of the VC does not support this
The message is mapped, so only the ing mechanism of this message is added to message_map.
. We have to process two versions of this message, ttn_needtexta and ttn_needtexta. Message
The ing code is as follows:

Begin_message_map (ctreectrlx, ctreectrl)
// {Afx_msg_map (ctreectrlx)
...
//} Afx_msg_map
On_policy_ex_range
(Ttn_needtextw, 0, 0 xFFFF, ontooltiptext)
On_policy_ex_range
(Ttn_needtexta, 0, 0 xFFFF, ontooltiptext)
End_message_map ()

The following code is added to the class declaration:
Protected:
// {Afx_msg (ctreectrlx)
...
//} Afx_msg
Afx_msg bool ontooltiptext
(Uint ID, nmhdr * pnmhdr, lresult * presult );
Declare_message_map ()

---- Now we will discuss the implementation of this function. To adapt to different language character sets, the ANSI character set and
Unicode character sets must be processed and the processing process may be different. The tree control is generated here.
The tooltip message of is not processed. The principle of filtering out the preceding messages is the ID of the message generated by the tree control.
Is the handle of the tree control window and has the ttf_idishwnd flag. It can be determined based on the mouse position
The node icon or the status chart icon tool prompt. This article shows some irrelevant
The readers should add some meaningful tips when doing this step. Of course, this document assumes that the control package
The node icon and status icon are included. If it is not included, do not calculate errors when calculating the mouse position.

Bool ctreectrlx: ontooltiptext
(Uint ID, nmhdr * pnmhdr, lresult * presult)
{
// The ANSI and Unicode formats need to be processed
Tooltiptexta * pttta = (tooltiptexta *) pnmhdr;
Tooltiptextw * ptttw = (tooltiptextw *) pnmhdr;
Cstring strtiptext;
Uint nid = pnmhdr-> idfrom;

// You do not have to process the tooltip message sent by the tree.
If (nid = (uint) m_hwnd &&
(Pnmhdr-> code = ttn_needtexta &&
Pttta-> uflags & ttf_idishwnd) |
(Pnmhdr-> code = ttn_needtextw &&
Ptttw-> uflags & ttf_idishwnd )))
Return false;

// Get the mouse position
Const MSG * pmessage;
Cpoint pt;
Pmessage = getcurrentmessage ();
Assert (pmessage );
PT = pmessage-> pt;
Screentoclient (& pt );

Uint nflags;
Htreeitem hitem = hittest (PT, & nflags );
If (nflags & tvht_onitemicon)
{
Int nimage, nselimage;
Getitemimage (htreeitem) NID, nimage, nselimage );
Switch (nimage)
{
Case 0:
Strtiptext = "Cross ";
Break;
Case 1:
Strtiptext = "plus sign ";
Break;
Case 2:
Strtiptext = "diamond ";
Break;
}
}
Else
{
If (getitemstate (htreeitem) NID,
Tvis_stateimagemask)> 12) = 2)
Strtiptext. Format ("this node is selected ");
Else
Strtiptext. Format ("this node is not selected ");
}

# Ifndef _ Unicode
If (pnmhdr-> code = ttn_needtexta)
Lstrcpyn (pttta-> sztext, strtiptext, 80 );
Else
_ Mbstowcsz (ptttw-> sztext, strtiptext, 80 );
# Else
If (pnmhdr-> code = ttn_needtexta)
_ Wcstombsz (pttta-> sztext, strtiptext, 80 );
Else
Lstrcpyn (ptttw-> sztext, strtiptext, 80 );
# Endif
* Presult = 0;

Return true; // Message Processing complete
}

---- This program has been debugged in Win9x and vc6.0.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.