MFC 列表控件修改实例(源码下载)

发布于:2024-05-05 ⋅ 阅读:(30) ⋅ 点赞:(0)

1、本程序基于前期我的博客文章《MFC下拉菜单打钩图标存取实例(源码下载)》
2、程序功能选中列表控件某一项,修改这一项的按钮由禁止变为可用,双击这个按钮弹出对话框可对这一项的记录数据进行修改,点击确定保存修改数据。
3、首先在主界面添加一个修改参数按钮。在这里插入图片描述
4、在myDlg.h 文件
class CMyDlg : public CDialog中添加代码

public:
void UpdateButton(void);
protected:
virtual LRESULT DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam);

5、选中列表控件,右击选择“事件…”,在弹出的事件句柄中选择“LVN_ITEMCHANGED”,如下图所示。
在这里插入图片描述

void CMyDlg::OnItemchangedList1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	POSITION pos = m_ctrlType.GetFirstSelectedItemPosition();
	if(pos != NULL)
	{
		int nItem = m_ctrlType.GetNextSelectedItem(pos);
		if(m_nTypeIndex != nItem)
		{
			m_nTypeIndex = nItem;
		}
	}
	else
		m_nTypeIndex = -1;	
	*pResult = 0;
}

6、在myDlg.cpp 文件中添加代码

LRESULT CMyDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	// TODO: Add your specialized code here and/or call the base class
	static int nTypeIndex = -2;
	if(::IsWindow(m_ctrlType.m_hWnd))
	{
		if(m_nTypeIndex != nTypeIndex)
		{
			nTypeIndex = m_nTypeIndex;
			UpdateButton();
		}
	}
	return CDialog::DefWindowProc(message, wParam, lParam);
}

void CMyDlg::UpdateButton()
{
	GetDlgItem(IDC_BUTTON3)->EnableWindow(m_nTypeIndex > -1);
}

7、在myDlg.cpp 文件中参数按钮中添加代码

void CMyDlg::OnButton3() 
{
	CTypeParaDlg dlg;
	dlg.m_pPara = &theApp.m_allPara[m_nTypeIndex];

	if(dlg.DoModal() == IDOK)
	{

    	LV_ITEM item;
		item.mask = LVIF_TEXT|LVIF_IMAGE;
		item.iItem = m_nTypeIndex;
		item.iSubItem = 0;
		item.pszText = theApp.m_allPara[m_nTypeIndex].m_strTypeName;
		item.iImage = 0;
		m_ctrlType.SetItem(&item);
		item.iSubItem = 1;
		item.pszText = theApp.m_allPara[m_nTypeIndex].m_strBrand;
		m_ctrlType.SetItem(&item);
		item.iSubItem = 2;
		item.pszText = theApp.m_allPara[m_nTypeIndex].m_strRemark;
		m_ctrlType.SetItem(&item);
	}	
}

运行程序
在这里插入图片描述
源码下载