Add a ToolTip hint box in MFC

Source: Internet
Author: User

Buttton add hint in the Part 1 MFC dialog box

For example, we want to add a ToolTip to a button control in a dialog box, which is implemented in the following way:

1. Add a CToolTipCtrl type member to the class of the dialog box and initialize it as follows, where appropriate:

M_tooltipctrl.create (this);
M_tooltipctrl.addtool (GetDlgItem (Idc_button1), _t ("This is the ToolTip");
M_tooltipctrl.setmaxtipwidth (123);
M_tooltipctrl.activate (TRUE);

2. Add the virtual BOOL pretranslatemessage (msg* pMsg) method to the frame class and implement the following:

BOOL Ctooltipsdlg::P retranslatemessage (msg* pMsg)
{
ASSERT (PMSG! = NULL);
if (pmsg->message = = Wm_mousemove | | pmsg->message = WM_LBUTTONDOWN | | pmsg->message = wm_lbuttonup)
M_tooltipctrl.relayevent (PMSG);
return CDialogEx::P retranslatemessage (PMSG);
}

OK, now when the mouse is over the button, the ToolTip for the word "ToolTip" appears. It's very simple, keep it for later use.

Part 2 adding hints to other controls in the MFC dialog box

To add a hint box to a window or a control within it, you can use MFC's class CToolTipCtrl, using the following method

1. Add a description of the variable in the window's class definition:
Class Ctooltiptestdlg:public cdialog{

...

Public
CToolTipCtrl M_tt;

...
}

2. Add the following code to the OnInitDialog () function in the dialog box

EnableToolTips (TRUE);
M_tt. Create (this);
M_tt. Activate (TRUE);

Cwnd*pw=getdlgitem (IDC_CHECK1);//Get Pointer to control
M_tt. AddTool (pw,l "CHECK1LAKJSFASFDASFD");//Add tip for this control

3. Reload the parent window's BOOL pretranslatemessage (msg* pMsg) and call M_tt in the function. RelayEvent (PMSG)

BOOL Ctooltiptestdlg::P retranslatemessage (msg* pMsg)
{
Todo:addyour specialized code here and/or call the base class

if (null!= m_tt. GetSafeHwnd ())
{
M_tt. RelayEvent (PMSG);
}


Returncdialog::P retranslatemessage (PMSG);
}

This completes the addition of tip for the control.

If you want to modify the content of the tip that you have added, you can use the Updatetiptext function, as follows

cwnd* Pw=getdlgitem (IDC_CHECK1);//Get Added tip control
M_tt. Updatetiptext (L "Asdflasdf", PW);//Update tip content

Other control functions are specific to the MSDN CToolTipCtrl class.

For a static text box, set the NOTIFY property to True, and if the static text control is created dynamically, you must add ss_notify to the window style, such as

M_statictext.create (_t ("my static"), Ws_child| Ws_visible| Ws_border| Ss_notify,
CRect (Ten, ten, +), this);

Reference article:

1. How to add a ToolTip to a control in MFC

2. In the spring and autumn period, MFC added ToolTip prompt box

The practice of adding hints to static text in MFC http://www.cnblogs.com/clever101/archive/2010/05/01/1725578.html

In addition, if you want a more powerful tip tip box, you can use a custom tooltiphttp://www.codeproject.com/kb/miscctrl/pptooltip.aspx written by a Belarusian.

Add a ToolTip hint box in MFC

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.