承接专栏《MFC工控项目实例二十手动测试界面模拟量输入实时显示》
对于禁止使用的删除、参数按钮,在选中列表控件选项时切换为能够使用。
1、在TypDlg.h文件中添加代码
#include "ShadeButtonST.h"
#include "BtnST.h"
class CTypDlg : public CDialog
{
public:
...
CShadeButtonST m_bAdd;
CShadeButtonST m_bDel;
CShadeButtonST m_bParameter;
CShadeButtonST m_bReturn;
CListCtrl m_ctrlType;
...
}
2、在TypDlg.cpp文件中添加代码
void CTypDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, m_ctrlType);
DDX_Control(pDX, IDC_BUTTON1, m_bAdd);
DDX_Control(pDX, IDC_BUTTON2, m_bDel);
DDX_Control(pDX, IDC_BUTTON3, m_bParameter);
DDX_Control(pDX, IDCANCEL, m_bReturn);
}
BEGIN_MESSAGE_MAP(CTypDlg, CDialog)
//{{AFX_MSG_MAP(CTypDlg)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BOOL CTypDlg::OnInitDialog()
{
CDialog::OnInitDialog();
m_bAdd.SetShade(CShadeButtonST::SHS_HARDBUMP);
m_bDel.SetShade(CShadeButtonST::SHS_HARDBUMP);
m_bParameter.SetShade(CShadeButtonST::SHS_HARDBUMP);
m_bReturn.SetShade(CShadeButtonST::SHS_HARDBUMP);
...
}
void CTypDlg::OnButton2()
{
if(m_nTypeIndex < theApp.m_nProductSel)
theApp.m_nProductSel--;
theApp.m_allPara.RemoveAt(m_nTypeIndex);
m_ctrlType.DeleteItem(m_nTypeIndex);
GetDlgItem(IDC_BUTTON2)->EnableWindow(0);
GetDlgItem(IDC_BUTTON3)->EnableWindow(0);
}
void CTypDlg::OnButton3()
{
CTypData 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);
}
}
void CTypDlg::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;
}
void CTypDlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
GetDlgItem(IDC_BUTTON2)->EnableWindow(true);
GetDlgItem(IDC_BUTTON3)->EnableWindow(true);
}
void CTypDlg::OnDblclkList1(NMHDR* pNMHDR, LRESULT* pResult)
{
OnButton3() ;
*pResult = 0;
}
运行程序