Creating list view a-la "Programs" list on Smartphone 2002
By Yaroslav Goncharov, July 05, 2002.
Download
Introduction
There is a standard list view on Smartphone 2002 that is used in several standard applications.
For example, you will see it if you press the "Programs" soft key on the Home screen. This list
contains 3 columns for each item: an accelerator digit, an icon and item text. The list looks
good and has comfortable keyboard navigation.
CSPListViewCtrl is a control that looks like "Programs" list. This article describes
this control and shows how to use it.
Two ways to create a similar control
First of all, we will try to investigate what control is used in the "Programs" list. Remote Spy++
shows that this control has "MS_VIRTUAL_LIST_VIEW_CE_1.0" window class. The same window class is used in the "Call History"
dialog. It seems that Microsoft uses this undocumented control in the standard applications.
So, there are at least 2 ways to continue: try to create documentation for this undocumented control
(for example, using a reverse assembler on the control window procedure) or emulate the desired behavior with
the standard list view control. I have chosen the second approach as the safest one.
CSPListControl is derived from CListViewCtrl WTL control and adds its own methods for initialization and items operations.
Creating the CSPListControl control
First of all, add SPListViewCtrl.cpp and SPListViewCtrl.h to your project. Your WTL project should include the header for CListViewCtrl (for example, in stdafx.h):
#include
The control can be created in the resource editor or using CSPListViewCtrl::Create function that calls CreateWindow
Windows API. To support the Large system font call CSPListViewCtrl::SetFontSize before a control window creation.
If the control is created in the resource editor it should have the following flags: WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_NOCOLUMNHEADER | LVS_SINGLESEL | WS_TABSTOP.
Also it is necessary to call CSPListViewCtrl::InitControl from OnInitDialog handler of the dialog class.
If you use Create method it is enough to pass the parent window and the desired rectangle to it.
In both cases you will need to create an image list and associate it with the control. The following code snippet shows
the example of the control creating using Create method.
// in the header file
...
#include "SPListViewCtrl.h"
...
private:
CSPListViewCtrl m_list;
...
// in OnCreate message handler
...
RECT rc;
GetClientRect(&rc);
m_list.Create(m_hWnd, rc);
m_imageList.Create(IDB_IMAGELIST, 16, 3, 0);
m_list.SetImageList(m_imageList, LVSIL_SMALL);
...
Items operations
You can use AddItem to add an item and DeleteItem to delete an item. InsertItem is not implemented in
this version of the control. For example:
for (int i = 0; i < 10; i++) {
CString strItem;
strItem.Format(_T("Item #%d"), i);
m_list.AddItem(strItem, i % 3);
}
m_list.DeleteItem(3);
m_list.DeleteItem(7);
m_list.SelectItem(0);
Handling an item launching
If user clicks the Action button or an accelerator key the SPL_EXECUTE message will be sent to the parent window.
wParam will contain the index of the chosen item. The following code fragment shows how to handle this message
in the parent window.
BEGIN_MSG_MAP(CMainWnd)
...
MESSAGE_HANDLER(SPL_EXECUTE, OnExecute)
...
END_MSG_MAP()
LRESULT OnExecute(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
CString str;
str.Format(_T("Item number %d (zero-based index)"), wParam);
MessageBox(str, _T("OnExecute"), MB_ICONINFORMATION | MB_OK);
return 0;
}
Conclusion
CSPListView control looks quite similar to the "Programs" list control. You will save your time
and achieve standard look and feel if you will use this control in your WTL application.
Discuss
Discuss this article.
Here you can write your comments and read comments of other developers.
|