CComboBox控件的下拉列表框,用鼠标在该ListBox上滑动的时候显示出当前具有focus的item项的索引

 
// 在CComboBox控件的下拉列表中滑动鼠标,会显示当前focus的item项,同时会将对应的item项以蓝色标记出来。通过spy++可以看到父窗口CComboBox控件收到一个WM_CTLCOLORLISTBOX消息。这里示例是得到该item项的索引值,同时将它显示到主对话框窗口的标题栏上。
// 需要重写CComboBox类,添加WindowProc虚函数,在虚函数中加入如下代码。
LRESULT CNewComboBox::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	// TODO: Add your specialized code here and/or call the base class
	if(WM_CTLCOLORLISTBOX == message)
	{
		HWND hListBox = (HWND)lParam;
		
		CListBox* pListBox = (CListBox*)FromHandle(hListBox);
		ASSERT(pListBox);
		int nCount = pListBox->GetCount();

		if(CB_ERR != nCount)
		{			
			CPoint pt;
			GetCursorPos(&pt);
			pListBox->ScreenToClient(&pt);
			
			CRect rc;
			for(int i=0; i<nCount; i++)
			{
				pListBox->GetItemRect(i, &rc);
				if(rc.PtInRect(pt))
				{
					CString str;
					str.Format(_T("nIndex = %d"), i);
					AfxGetMainWnd()->SetWindowText(str);
					break;
				}
			}
		}
		
	}
	return CComboBox::WindowProc(message, wParam, lParam);
}

你可能感兴趣的:(CComboBox控件的下拉列表框,用鼠标在该ListBox上滑动的时候显示出当前具有focus的item项的索引)