CStringtemp;
m_userSet.GetFieldValue("user_id",temp);
m_list.InsertItem(i,temp);
m_userSet.GetFieldValue("user_name",temp);
m_list.SetItemText(i,1,temp);
m_userSet.GetFieldValue("user_password",temp);
m_list.SetItemText(i,2,temp);
m_userSet.GetFieldValue("user_level",temp);
m_list.SetItemText(i,3,temp);
m_userSet.MoveNext();
}
m_userSet.Close();
}
voidCUserDlg:
:
ResetButton()
{
m_strUser.EnableWindow(false);
m_strPassword.EnableWindow(false);
m_cLevel.EnableWindow(false);
m_bConfirm.EnableWindow(false);
m_bAdd.EnableWindow(true);
m_bDel.EnableWindow(true);
m_bEdit.EnableWindow(true);
}
然后初始化对话框OnInitDialog方法:
BOOLCUserDlg:
:
OnInitDialog()
{
CDialog:
:
OnInitDialog();//初始化CList控件和header
LONGstyles;
styles=GetWindowLong(m_list.m_hWnd,GWL_STYLE);
SetWindowLong(m_list.m_hWnd,GWL_STYLE,styles|LVS_REPORT);
m_list.InsertColumn(0,"用户编号");
m_list.InsertColumn(1,"用户名");
m_list.InsertColumn(2,"密码");
m_list.InsertColumn(3,"用户权限");
//设置header宽度
RECTrectList;
m_list.GetWindowRect(&rectList);
intwid=rectList.right-rectList.left-4;
for(inti=0;i<4;i++)
m_list.SetColumnWidth(i,wid/4);
m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT);
RefreshList();
m_cLevel.AddString("管理员");
m_cLevel.AddString("教师");
m_cLevel.AddString("学生");
m_cLevel.SelectString(0,"管理员");
ResetButton();
returnTRUE;
}
接着为各个控件添加响应的函数:
voidCUserDlg:
:
OnClickListUser(NMHDR*pNMHDR,LRESULT*pResult)
{
//TODO:
Addyourcontrolnotificationhandlercodehere
introw=m_list.GetSelectionMark();
CStrings;
s=m_list.GetItemText(row,1);
m_strUser.SetWindowText(s);
s=m_list.GetItemText(row,2);
m_strPassword.SetWindowText(s);
s=m_list.GetItemText(row,3);
if(s=="0")m_cLevel.SelectString(0,"管理员");
elseif(s=="1")m_cLevel.SelectString(1,"教师");
elseif(s=="2")m_cLevel.SelectString(0,"学生");
*pResult=0;
}
voidCUserDlg:
:
OnUserAdd()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
m_strUser.EnableWindow(true);
m_strUser.SetWindowText("");
m_strPassword.EnableWindow(true);
m_strPassword.SetWindowText("");
m_cLevel.EnableWindow(true);
m_cLevel.SelectString(0,"管理员");
m_userSet.Open();
m_userSet.AddNew();
m_bConfirm.EnableWindow(true);
m_bAdd.EnableWindow(false);
m_bEdit.EnableWindow(false);
m_bDel.EnableWindow(false);
}
voidCUserDlg:
:
OnUserEdit()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
introw=m_list.GetSelectionMark();
CStrings,strSQL;
s=m_list.GetItemText(row,0);
if(s=="")
{MessageBox("请选择一行!
");
}
else
{
//定义查询条件
strSQL.Format("user_id=%s",s);
m_userSet.Open();
m_userSet.m_strFilter=strSQL;
m_userSet.Requery();
m_userSet.Edit();
m_bConfirm.EnableWindow(true);
m_bAdd.EnableWindow(false);
m_bEdit.EnableWindow(false);
m_bDel.EnableWindow(false);
m_strUser.EnableWindow(true);
m_strPassword.EnableWindow(true);
m_cLevel.EnableWindow(true);
}
}
voidCUserDlg:
:
OnUserDel()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
introw=m_list.GetSelectionMark();
CStrings,strSQL;
s=m_list.GetItemText(row,0);
if(s=="")
{MessageBox("请选择一行!
");
}
else
{
if(MessageBox("确定要删除记录吗?
","删除查询",MB_OKCANCEL|MB_ICONQUESTION)==IDOK)
{
strSQL.Format("user_id=%s",s);
m_userSet.Open();
m_userSet.m_strFilter=strSQL;
m_userSet.Requery();
m_userSet.Edit();
//设置active_status="N"
m_userSet.m_active_status="N";
m_userSet.Update();
m_userSet.Close();
RefreshList();
}
}
}
voidCUserDlg:
:
OnUserOk()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
CStringusername,password,level;
m_strUser.GetWindowText(username);
m_strPassword.GetWindowText(password);
if(username=="")
{
MessageBox("请输入用户名");
}
elseif(password=="")
{
MessageBox("请输入密码");
}
else
{
//获取下拉列表中的值
inti=m_cLevel.GetCurSel();
if(i==0)
level="0";
if(i==1)
level="1";
if(i==2)
level="2";
m_userSet.m_user_name=username;
m_userSet.m_user_password=password;
m_userSet.m_user_level=level;
m_userSet.m_active_status="Y";
m_userSet.Update();
m_userSet.Close();
RefreshList();
ResetButton();
}
}
voidCUserDlg:
:
OnCancel()
{
//TODO:
Addextracleanuphere
if(m_userSet.IsOpen())
m_userSet.Close();
ResetButton();
CDialog:
:
OnCancel();
}
再在头文件中添加相关操作的函数关于用户管理的窗口就基本完成里。
其他窗口方法类似。