程序功能定义MFC数组类CArray,在一个对话框的编辑框中输入数据按确定后在另一个对话框的列表控件中显示。
1、建一个my的MFC对话框项目文件。
2、在资源视图中插入一个对话框Dialog1。
3、在my对话框中添加一个IDC_LIST1列表控件,变量名m_ctrlType,一个IDC_BUTTON1按钮。IDC_LIST1列表属性如图。
4、在Dialog1对话框中添加三个编辑框IDC_EDIT1,IDC_EDIT2,IDC_EDIT3变量名m_strTypeName、 m_strBrand、 m_strRemark。
5、my.h中添加代码
#include <afxtempl.h>
class CProductPara
{
public:
union
{
struct
{
char m_strTypeName[24];
char m_strBrand[24];
char m_strRemark[64];
};
char len[1024];
};
};
class CMyApp : public CWinApp
{
public:
CArray<CProductPara,CProductPara> m_allPara;
CMyApp();
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyApp)
public:
virtual BOOL InitInstance();
//}}AFX_VIRTUAL
DECLARE_MESSAGE_MAP()
};
extern class CMyApp theApp;
6、myDlg.cpp中添加代码
#include "TypeParaDlg.h"
BOOL CMyDlg::OnInitDialog()
{
CDialog::OnInitDialog();
DWORD dwExStyles = m_ctrlType.GetExtendedStyle();
m_ctrlType.SetExtendedStyle(
dwExStyles |
LVS_EX_GRIDLINES |
LVS_EX_FULLROWSELECT |
LVS_EX_FLATSB |
LVS_EX_ONECLICKACTIVATE
);
LVCOLUMN cloumn;
int i;
cloumn.mask = LVCF_TEXT|LVCF_WIDTH;
cloumn.pszText = "型号名称";
cloumn.cx = 90;
i = m_ctrlType.InsertColumn(0,&cloumn);
cloumn.mask = LVCF_TEXT|LVCF_WIDTH;
cloumn.pszText = "产品商标";
cloumn.cx = 90;
i = m_ctrlType.InsertColumn(1,&cloumn);
cloumn.pszText = " 备 注";
cloumn.cx = 160;
i = m_ctrlType.InsertColumn(2,&cloumn);
int nItem;
LV_ITEM item;
item.mask = LVIF_TEXT|LVIF_IMAGE;
char buf[255];
for(i = 0; i < theApp.m_allPara.GetSize() ; i ++)
{
nItem = m_ctrlType.GetItemCount();
item.iItem = nItem;
item.iSubItem = 0;
sprintf(buf,"%s",theApp.m_allPara[i].m_strTypeName);
item.pszText = buf;
item.iImage = 0;
m_ctrlType.InsertItem(&item);
item.iSubItem = 1;
sprintf(buf,"%s",theApp.m_allPara[i].m_strBrand);
item.pszText = buf;
m_ctrlType.SetItem(&item);
item.iSubItem = 2;
sprintf(buf,"%s",theApp.m_allPara[i].m_strRemark);
item.pszText = buf;
m_ctrlType.SetItem(&item);
}
void CMyDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CTypeParaDlg dlg;
CProductPara paraBuf;
memset(¶Buf,0,sizeof(CProductPara));
dlg.m_pPara = ¶Buf;
if(dlg.DoModal() == IDOK)
{
int nItem;
if(m_nTypeIndex > -1)
nItem = m_nTypeIndex + 1;
else
nItem = theApp.m_allPara.GetSize();
theApp.m_allPara.InsertAt(nItem,paraBuf);
LV_ITEM item;
item.mask = LVIF_TEXT;
item.iItem = nItem;
item.iSubItem = 0;
item.pszText = theApp.m_allPara[nItem].m_strTypeName;
item.iImage = 0;
m_ctrlType.InsertItem(&item);
item.iSubItem = 1;
item.pszText = theApp.m_allPara[nItem].m_strBrand;
m_ctrlType.SetItem(&item);
item.iSubItem = 2;
item.pszText = theApp.m_allPara[nItem].m_strRemark;
m_ctrlType.SetItem(&item);
}
}
7、TypeParaDlg.h中添加代码
class CTypeParaDlg : public CDialog
{
// Construction
public:
CProductPara * m_pPara;
BOOL UpdatePara(BOOL);
...
//{{AFX_MSG(CTypeParaDlg)
virtual BOOL OnInitDialog();
virtual void OnOK();
// NOTE: the ClassWizard will add member functions here
//}}AFX_MSG
}
8、TypeParaDlg.cpp中添加代码
BOOL CTypeParaDlg::OnInitDialog()
{
CDialog::OnInitDialog();
UpdatePara(FALSE);
// UpdatePara(1);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
BOOL CTypeParaDlg::UpdatePara(BOOL bUpdate)
{
if(m_pPara == NULL)
return TRUE;
if(bUpdate)
{
if(!UpdateData())
return FALSE;
sprintf(m_pPara->m_strTypeName,"%s", m_strTypeName);
sprintf(m_pPara->m_strRemark,"%s", m_strRemark);
sprintf(m_pPara->m_strBrand,"%s", m_strBrand);
}
else
{
m_strTypeName = m_pPara->m_strTypeName;
m_strRemark = m_pPara->m_strRemark;
m_strBrand = m_pPara->m_strBrand;
UpdateData(FALSE);
}
return TRUE;
}
void CTypeParaDlg::OnOK()
{
// TODO: Add extra validation here
if(!UpdatePara(TRUE))
return;
CDialog::OnOK();
UpdateData(TRUE);
}
运行程序
源码下载