almost all of the controls in MFC have information hints, and only the listbox does not have such a function, every time we move the mouse to the control of the ListBox, nothing is not very discouraged Ah, so I wrote today a simple hint of the listbox control, To achieve that effect.
The idea is simple: we first construct our own MyListBox to inherit the ListBox control, and then add a CToolTipCtrl control (the carrier that displays the information) in our own mylistbox. This way we must provide an interface to create the CToolTipCtrl control. Secondly, after the control is created, it needs an interface to set up the information. Then, when our mouse moves, it displays different content as the mouse is positioned, so it needs a mouse move event, The information is set when the mouse moves. The code is as follows: Lists the code for the CPP file
BOOL Ctiplistbox::createtooltip ()
{
if (NULL = = m_tooltipmessage)
{
m_tooltipmessage = new CToolTipCtrl ();
if (m_tooltipmessage->create (this, tts_alwaystip| Tts_noprefix))
{
m_tooltipmessage->activate (TRUE);
m_tooltipmessage->setdelaytime (100);
m_tooltipmessage->setmaxtipwidth (500) ;
M_tooltipmessage->addtool (this);// Help control ToolTip
return TRUE;
}
}
return FALSE;
}
void Ctiplistbox::settipmessage (CString &message)
{
if (message. IsEmpty ())
{
return;
}
if (M_tooltipmessage->getsafehwnd () = NULL)
{
M_tooltipmessage->updatetiptext (message, this); Update the characters to display
}
Else
{
if (CreateToolTip ())
{
M_tooltipmessage->addtool (this, message);
}
}
M_tooltipmessage->activate (TRUE);
}
void Ctiplistbox::onmousemove (UINT nflags, CPoint Point)
{
CPoint pt;//Current mouse position
GetCursorPos (&PT);
ScreenToClient (&PT);//Convert to client area coordinates
CRect size of rect;//control
GetClientRect (&rect);
BOOL inout;
CString message;
if (rect. PtInRect (PT))
{
int select = Itemfrompoint (PT, inout);//What piece of information is located on the mouse
GetText (SELECT, message);
Settipmessage (message);
}
}
BOOL Ctiplistbox::P retranslatemessage (msg* pMsg)
{
if (M_tooltipmessage->getsafehwnd () = NULL)
{
if (pmsg->message = = wm_mousemove)
{
M_tooltipmessage->relayevent (PMSG);
SendMessage (WM_MOUSEMOVE);
}
}
Return CWnd::P retranslatemessage (PMSG);
}