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

1、本程序基于前期我的博客文章《MFC下拉菜单打钩图标存取实例(源码下载)》

2、程序功能选中列表控件某一项,修改这一项的按钮由禁止变为可用,双击这个按钮弹出对话框可对这一项的记录数据进行修改,点击确定保存修改数据。

3、首先在主界面添加一个修改参数按钮。

4、在myDlg.h 文件

class CMyDlg : public CDialog中添加代码

cpp 复制代码
public:
void UpdateButton(void);
protected:
virtual LRESULT DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam);

5、选中列表控件,右击选择"事件...",在弹出的事件句柄中选择"LVN_ITEMCHANGED",如下图所示。

cpp 复制代码
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 文件中添加代码

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 文件中参数按钮中添加代码

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);
	}	
}

运行程序

源码下载

相关推荐
Sunshine for you1 天前
C++中的职责链模式实战
开发语言·c++·算法
qq_416018721 天前
C++中的状态模式
开发语言·c++·算法
2401_884563241 天前
模板代码生成工具
开发语言·c++·算法
2401_831920741 天前
C++代码国际化支持
开发语言·c++·算法
m0_672703311 天前
上机练习第51天
数据结构·c++·算法
2401_851272991 天前
自定义内存检测工具
开发语言·c++·算法
☆5661 天前
C++中的命令模式
开发语言·c++·算法
仰泳的熊猫1 天前
题目2577:蓝桥杯2020年第十一届省赛真题-走方格
数据结构·c++·算法·蓝桥杯
灰色小旋风1 天前
力扣13 罗马数字转整数
数据结构·c++·算法·leetcode
2301_810160951 天前
C++与物联网开发
开发语言·c++·算法