建立一对话框,加入列表控件,设置其变量为 m_ListView
BOOL CMy0000000000000Dlg::OnInitDialog() { CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } }
// Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here m_ListView.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES); int nItem = 0; m_ListView.InsertColumn(nItem++, "学号", LVCFMT_LEFT, 50); m_ListView.InsertColumn(nItem++, "姓名", LVCFMT_LEFT, 60); m_ListView.InsertColumn(nItem++, "姓别", LVCFMT_LEFT, 50); m_ListView.InsertColumn(nItem++, "专业", LVCFMT_LEFT, 70); m_ListView.InsertColumn(nItem++, "年级", LVCFMT_LEFT, 70); m_ListView.InsertColumn(nItem++, "班级", LVCFMT_LEFT, 70); for(int i=0; i<5; i++) { CString csTemp; csTemp.Format("学号%i", i); nItem = m_ListView.InsertItem(0xFFFFFF, csTemp); csTemp.Format("姓名%i", i); int nSub = 1; m_ListView.SetItem(nItem, nSub++, 1, csTemp, NULL, 0, 0, 0); csTemp.Format("男"); m_ListView.SetItem(nItem, nSub++, 1, csTemp, NULL, 0, 0, 0); csTemp.Format("专业%i", i); m_ListView.SetItem(nItem, nSub++, 1, csTemp, NULL, 0, 0, 0); csTemp.Format("年级%i", i); m_ListView.SetItem(nItem, nSub++, 1, csTemp, NULL, 0, 0, 0); csTemp.Format("班级%i", i); m_ListView.SetItem(nItem, nSub++, 1, csTemp, NULL, 0, 0, 0); } return TRUE; // return TRUE unless you set the focus to a control }
..................
对列表控件加入单击事件。 void CMy0000000000000Dlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult) { // TODO: Add your control notification handler code here CString IP1; CString IP2; CString IP3; NMLISTVIEW* pp= (NMLISTVIEW*)pNMHDR; int i1=pp->iItem; //iItem, 行号 int i2= pp->iSubItem; //iSubItem 子项目,列号
CString str; str.Format("%d,%d", i1,i2); AfxMessageBox(str);
if (i2==1 && i1!=-1) { IP1=m_ListView.GetItemText(i1,i2); ::MessageBox(NULL,IP1,"文件内容",0); } if (i2==2 && i1!=-1) { IP2=m_ListView.GetItemText(i1,i2); ::MessageBox(NULL,IP2,"文件内容",0); } if (i2==3 && i1!=-1) { IP3=m_ListView.GetItemText(i1,i2); ::MessageBox(NULL,IP3,"文件内容",0); } *pResult = 0; }
|