c++鼠标在listbox上移动时,移动到某项上面是,怎么样才能让它选中

2025-03-22 22:22:01
推荐回答(1个)
回答1:

假设你所使用的是VS,即MFC库

1、建立一个新的类,并继承自CListBox

2、为这个类添加新的消息处理机制(WM_MOUSEMOVE)

3、把这个类应用于你所需要使用的地方(例如:《MFC Windows程序设计》中第7章的FontView程序),代码如下:

class CPKIListBox : public CListBox
{
public:
 DECLARE_MESSAGE_MAP()
 afx_msg void OnMouseMove(UINT nFlags, CPoint point);
};

BEGIN_MESSAGE_MAP(CPKIListBox, CListBox)
 ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()

void CPKIListBox::OnMouseMove(UINT nFlags, CPoint point)
{
 // TODO: 在此添加消息处理程序代码和/或调用默认值
 BOOL outSide = TRUE;
 this->SetCurSel(this->ItemFromPoint(point,outSide));
 CListBox::OnMouseMove(nFlags, point);
}