first mfc - yamaumima/hello.github.io GitHub Wiki
BEGIN_MESSAGE_MAP(CFirstMFCDlg, CDialog)
//{{AFX_MSG_MAP(CFirstMFCDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_ADD, OnAdd)
ON_BN_CLICKED(IDC_DEL, OnDel)
ON_BN_CLICKED(IDC_MOD, OnMod)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFirstMFCDlg message handlers
//WM_INITDIALOG 当对话框还未显示出来的时候
BOOL CFirstMFCDlg::OnInitDialog()
{
CDialog::OnInitDialog();
CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LIST);
pList ->InsertColumn(0,"工号",LVCFMT_LEFT,120);
pList ->InsertColumn(1,"姓名",0,160);
pList ->InsertColumn(2,"工资",0,160);
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CFirstMFCDlg::OnAdd()
{
CString str;
this ->GetDlgItemText(IDC_NUMB,str);
CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LIST);
int nCount = pList ->GetItemCount();
pList ->InsertItem(nCount,str);
GetDlgItemText(IDC_NAME,str);
pList ->SetItemText(nCount,1,str);
GetDlgItemText(IDC_SALA,str);
pList ->SetItemText(nCount,2,str);
}
void CFirstMFCDlg::OnDel()
{
CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LIST);
//int nSel = pList ->GetSelectionMark();
POSITION pos = pList ->GetFirstSelectedItemPosition();
int nSel = pList ->GetNextSelectedItem(pos);
if(nSel < 0)
{
AfxMessageBox("请先选中一行再删除!");
return;
}
if(IDYES==AfxMessageBox("确认删除选中的数据吗?",MB_YESNO))
pList ->DeleteItem(nSel);
}
void CFirstMFCDlg::OnMod()
{
CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LIST);
//int nSel = pList ->GetSelectionMark();
POSITION pos = pList ->GetFirstSelectedItemPosition();
int nSel = pList ->GetNextSelectedItem(++pos);
if(nSel < 0)
{
AfxMessageBox("请先选中一行再修改!");
return;
}
CString str;
GetDlgItemText(IDC_NUMB,str);
if(IDNO==AfxMessageBox("确定要修改 " + str + " 的数据吗?",MB_YESNO))
return;
GetDlgItemText(IDC_NAME,str);
pList ->SetItemText(nSel,1,str);
GetDlgItemText(IDC_SALA,str);
pList ->SetItemText(nSel,2,str);
}
