For this week's spot the bug contest, we have a simple C++ application that displays a list of employees in a listview control. The MFC class library has been used in this project. MFC, as you may know, is supported on Windows Mobile Pocket PC and Smartphone projects created with Visual Studio 2005.
Upon startup, our application loads up some employee information (loadData), initializes a list view control (initializeListCtrl) and then displays the list of employees (populateListCtrl).
1void CEmployeeDlg::loadData()
2{
3 CEmployee* pEmp1 = new CEmployee();
4 pEmp1->id = 100;
5 pEmp1->fullName = _T("Kevin Johnson");
6 pEmp1->emailAddress = _T("kevin@acme.com");
7 m_employeeList.Add(pEmp1);
8
9
10 CEmployee* pEmp2 = new CEmployee();
11 pEmp2->id = 200;
12 pEmp2->fullName = _T("Jim Alchin");
13 pEmp2->emailAddress = _T("jim@acme.com");
14 m_employeeList.Add(pEmp2);
15
16 CEmployee* pEmp3 = new CEmployee();
17 pEmp3->id = 300;
18 pEmp3->fullName = _T("Robbie Bach");
19 pEmp3->emailAddress = _T("robbie@acme.com");
20 m_employeeList.Add(pEmp3);
21}
22
23void CEmployeeDlg::populateListCtrl()
24{
25 for (int i=0; i<m_employeeList.GetSize(); i++)
26 {
27 CEmployee* pEmp = m_employeeList[i];
28
29 // insert employee into the list control
30 int index = m_wndListCtrl.InsertItem(
31 0, pEmp->fullName, 0);
32
33 // set id so we can later uniquely identify
34 // which employee a listitem refers to
35 m_wndListCtrl.SetItemData(index, pEmp->id);
36 }
37}
38
39void CEmployeeDlg::initializeListCtrl()
40{
41 // Create the imagelist and assign it to the listview control
42 m_imageList.Create(32, 32, ILC_COLOR|ILC_MASK, 1, 1);
43
44 m_wndListCtrl.SetImageList(&m_imageList, LVSIL_NORMAL);
45
46 // Add an icon to the imagelist
47 HICON hIcon =
48 (HICON)LoadImage(AfxGetResourceHandle(),
49 MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 32, 32, 0);
50
51 m_imageList.Add(hIcon);
52}
53
54
2{
3 CEmployee* pEmp1 = new CEmployee();
4 pEmp1->id = 100;
5 pEmp1->fullName = _T("Kevin Johnson");
6 pEmp1->emailAddress = _T("kevin@acme.com");
7 m_employeeList.Add(pEmp1);
8
9
10 CEmployee* pEmp2 = new CEmployee();
11 pEmp2->id = 200;
12 pEmp2->fullName = _T("Jim Alchin");
13 pEmp2->emailAddress = _T("jim@acme.com");
14 m_employeeList.Add(pEmp2);
15
16 CEmployee* pEmp3 = new CEmployee();
17 pEmp3->id = 300;
18 pEmp3->fullName = _T("Robbie Bach");
19 pEmp3->emailAddress = _T("robbie@acme.com");
20 m_employeeList.Add(pEmp3);
21}
22
23void CEmployeeDlg::populateListCtrl()
24{
25 for (int i=0; i<m_employeeList.GetSize(); i++)
26 {
27 CEmployee* pEmp = m_employeeList[i];
28
29 // insert employee into the list control
30 int index = m_wndListCtrl.InsertItem(
31 0, pEmp->fullName, 0);
32
33 // set id so we can later uniquely identify
34 // which employee a listitem refers to
35 m_wndListCtrl.SetItemData(index, pEmp->id);
36 }
37}
38
39void CEmployeeDlg::initializeListCtrl()
40{
41 // Create the imagelist and assign it to the listview control
42 m_imageList.Create(32, 32, ILC_COLOR|ILC_MASK, 1, 1);
43
44 m_wndListCtrl.SetImageList(&m_imageList, LVSIL_NORMAL);
45
46 // Add an icon to the imagelist
47 HICON hIcon =
48 (HICON)LoadImage(AfxGetResourceHandle(),
49 MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 32, 32, 0);
50
51 m_imageList.Add(hIcon);
52}
53
54
这段代码是修改过的,原来的代码中有一个小Bug
// Create the imagelist and assign it to the listview control
m_imageList.Create(32, 32, ILC_COLOR32|ILC_MASK, 1, 1);
^
smartphone2003中的不支持ILC_COLOR32