To write a ListBox control with hints

Source: Internet
Author: User
 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);
}

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.