65.9K
CodeProject is changing. Read more.
Home

Hot List Control - Another Kind of List Control

May 14, 2000

viewsIcon

93322

downloadIcon

2419

A control for selecting items from a list, with tool tips and mouse tracking

  • Download demo project - 26 Kb
  • Download source files - 6 Kb
  • This example shows the hot list control used in a CView class. A tool tip displays information about the highlighted item. George Washington is the currently selected item, but the mouse is hovering over Martin Van Buren, and display the tool tip assigned to Martin Van Buren.

    This example shows the hot list control used in a dialog box. The selected item is Chester Arthur, but the mouse is currently highlighting Abraham Lincoln. Scroll buttons at the top an bottom of the control allow the user to scroll this list.

    Description

    CHotListCtrl is not derived from CListCtrl. CHotListCtrl mimics the behavior of the TLookOut control developed by Peter Thornqvist for Borland's Delphi. Peter described the control as being similar to a control used in Microsoft Outlook. I do not use Microsoft Outlook, so I do not know if this is true, or how similar the control is to anything in Outlook.

    The control displays a list of items. For each item, you can specify the text for the item (from a string or resource ID), the tool tip text to display when the item is highlighted, and a dwData value for the item.

    When an item is selected, a WM_HOT_LIST_CHANGED (user-defined) message is sent to the parent window. As the mouse moves over each item in the list, that item is highlighted (drawn raised). The currently selected item is drawn pressed. There are scroll buttons at the top and bottom for lists that exceed the height of the control.

    CHotListCtrl is Unicode safe (to the best of my knowledge).

    Using the Control in a View

    1. Add a CHotListCtrl member to you CView derived class.
      class CHotsView : public CView
      {
      protected: // create from serialization only
      	CHotsView();
      	DECLARE_DYNCREATE(CHotsView)
      
      // Attributes
      public:
      	CHotsDoc* GetDocument();
      
      	CHotListCtrl m_HotListCtrl; // the Hot List Control
      
      
      [snip]
    2. On CView::InitialUpdate, create the control, and populate it with the desired list items.
      void CHotsView::OnInitialUpdate() 
      {
      	CView::OnInitialUpdate();
      	
      	if (::IsWindow(m_HotListCtrl.m_hWnd) == FALSE)
      	{
      		CRect rc;
      		GetClientRect(&rc);
      
      		m_HotListCtrl.Create(WS_CHILD | WS_VISIBLE, rc, this, IDC_HOTLIST_CTRL);
      		
      		m_HotListCtrl.AddItem("George Washington", IDS_PRES1, 0);
      		m_HotListCtrl.AddItem("John Adams", IDS_PRES2, 0);
      		m_HotListCtrl.AddItem("Thomas Jefferson", IDS_PRES3, 0);
      		m_HotListCtrl.AddItem("James Madison", IDS_PRES4, 0);
      		m_HotListCtrl.AddItem("James Monroe", IDS_PRES5, 0);
      		m_HotListCtrl.AddItem("John Quincy Adams", IDS_PRES6, 0);
      		m_HotListCtrl.AddItem("Andrew Jackson", IDS_PRES7, 0);
      		m_HotListCtrl.AddItem("Martin Van Buren", IDS_PRES8, 0);
      		m_HotListCtrl.AddItem("William Henry Harrison", IDS_PRES9, 0);
      		m_HotListCtrl.AddItem("John Tyler", IDS_PRES10, 0);
      	}
      }
      
    3. Handle WM_SIZE messages in your CView derived class to reposition the control.
      void CHotsView::OnSize(UINT nType, int cx, int cy) 
      {
      	CView::OnSize(nType, cx, cy);
      	
      	if (::IsWindow(m_HotListCtrl.m_hWnd))
      	{
      		CRect rc;
      		GetClientRect(&rc);
      		m_HotListCtrl.MoveWindow(&rc, TRUE);
      
      	}
      }
      

    Using the Control in a Dialog

    There is more than one way to use a custom control in a dialog, but this is the way I chose for this example.

    1. Add a button to your dialog resource. Make it the size you desire for the control.
    2. Use ClassWizard to add to your dialog class, a CButton data member for the button. Change the CButton declaration to CHotListCtrl.
      class CTestDlg : public CDialog
      {
      // Construction
      public:
      	CTestDlg(CWnd* pParent = NULL);   // standard constructor
      
      // Dialog Data
      	//{{AFX_DATA(CTestDlg)
      	enum { IDD = IDD_TEST_DIALOG };
      	CHotListCtrl	m_HotListCtrl;
      	//}}AFX_DATA
      
      [snip]
    3. Process your WM_INITDIALOG message for the dialog. In your WM_INITDIALOG handler, call CHotListCtrl::Initialize(), and populate the control with the desired list items.
      BOOL CTestDlg::OnInitDialog() 
      {
      	CDialog::OnInitDialog();
      	
      	// very important to call initialize here!
      	m_HotListCtrl.Initialize();
      
      	CRect rc;
      
      	m_HotListCtrl.AddItem("George Washington", 0, 0);
      	m_HotListCtrl.AddItem("Thomas Jefferson", 0, 0);
      	m_HotListCtrl.AddItem("John Adams", 0, 0);
      	m_HotListCtrl.AddItem("John Tyler", 0, 0);
      	m_HotListCtrl.AddItem("Millard Fillmore", 0, 0);
      	
      	return TRUE;  // return TRUE unless you set the focus to a control
      	              // EXCEPTION: OCX Property Pages should return FALSE
      }

    Filling CHotListCtrl

    Use the following functions to fill the list control:

    void CHotListCtrl::AddItem(CString strText, UINT nToolTipID, DWORD dwData);
    void CHotListCtrl::AddItem(UINT nIDText, UINT nToolTipID, DWORD dwData);
    
    strText is the text to display for the list item.
    nIDText is the resource string identifier of the text to display for the list item
    nToolTipID is the resource string identifier of the text to display for the list item.
    dwData is the data member for the list control item.

    CHotListCtrl Notifications

    When a new list control item is selected, the control sends a WM_HOT_LIST_CHANGED (user-defined) message is sent to the parent window, with the following parameters:

    wParam is the number of the newly selected item in the list (zero-based).
    lParam is the dwData member for the newly selected item.

    Don't forget to define WM_HOT_LIST_CHANGED somewhere in your application, such as stdafx.h.