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

运行程序

源码下载

相关推荐
孙同学_18 分钟前
【C++】AVL树
c++·redis
君宝41 分钟前
Linux ALSA架构:PCM_OPEN流程 (二)
java·linux·c++
island13141 小时前
【C++框架#5】Elasticsearch 安装和使用
开发语言·c++·elasticsearch
岁忧2 小时前
(LeetCode 每日一题) 3541. 找到频率最高的元音和辅音 (哈希表)
java·c++·算法·leetcode·go·散列表
小六子成长记2 小时前
【C++】:list容器全面解析(超详细)
c++·windows·list
IT灰猫3 小时前
C++轻量级配置管理器升级版
开发语言·c++·设计模式·配置管理·ini解析
大白同学4214 小时前
【C++】C++11介绍(Ⅱ)
开发语言·c++
油炸自行车5 小时前
【Qt】编写Qt自定义Ui控件步骤
开发语言·c++·qt·ui·自定义ui控件·qt4 自定义ui控件
呱呱巨基7 小时前
C/C++ 内存管理
c++·笔记·学习
半桔7 小时前
【网络编程】TCP 服务器并发编程:多进程、线程池与守护进程实践
linux·服务器·网络·c++·tcp/ip