📄 phonedlg.cpp
字号:
((CEdit*)GetDlgItem(IDC_NAME))->SetFocus();
((CEdit*)GetDlgItem(IDC_NAME))->SetSel(0,-1);
}
else //刚才处于编辑状态,现在要进行存储
{
UpdateData(true);
m_name.TrimLeft(" ");
m_name.TrimRight(" ");
m_sex.TrimLeft(" ");
m_sex.TrimRight(" ");
m_mail.TrimLeft(" ");
m_mail.TrimRight(" ");
m_phone.TrimLeft(" ");
m_phone.TrimRight(" ");
m_note.TrimLeft(" ");
m_note.TrimRight(" ");
if(m_name=="")
{ MessageBox("请输入姓名!");
GetDlgItem(IDC_NAME)->SetFocus();
}
else if(m_sex!="男" && m_sex!="女")
{
MessageBox("请选择性别!");
GetDlgItem(IDC_SEX)->SetFocus();
}
else
{
if(m_mail=="")
m_mail=="未填";
if(m_phone=="")
m_phone="未填";
if(m_note=="")
m_note="未填";
CFriend *frd=new CFriend(m_name,m_sex,m_age,m_mail,m_phone,m_note);
m_frdList.AddTail(frd);
CMyDlg::WriteFiles();
GetDlgItem(IDC_MAN)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_WOMAN)->ShowWindow(SW_HIDE);
EditState=false;
MessageBox("添加成功!");
}
}
}
void CMyDlg::ListRefresh()
{
curPos=m_frdList.GetHeadPosition();
CMyDlg::ReadFile();
int i=0; //行
m_clc.DeleteAllItems();
while(curPos!=NULL)
{
// MessageBox("abc");
CFriend *frd=m_frdList.GetAt(curPos);
m_clc.InsertItem(i,"村中山"); //见鬼了,我也不知道为什么要加这句,反正不加就是出不来
m_clc.SetItemText(i,0,frd->name);
m_clc.SetItemText(i,1,frd->sex);
CString temp;
temp.Format("%d",frd->age);
m_clc.SetItemText(i,2,temp);
m_clc.SetItemText(i,3,frd->email);
m_clc.SetItemText(i,4,frd->phone);
m_clc.SetItemText(i,5,frd->note);
m_frdList.GetNext(curPos);
i++;
// MessageBox("abc");
}
CMyDlg::Hello();
UpdateData(false);
}
void CMyDlg::WriteFiles()
{
CString text;
POSITION tempPos;
tempPos=m_frdList.GetHeadPosition();
CString inttoa;
while(tempPos!=NULL)
{
text+=(m_frdList.GetAt(tempPos))->name+" ";
text+=(m_frdList.GetAt(tempPos))->sex+" ";
inttoa.Format("%d",m_frdList.GetAt(tempPos)->age);
text+=inttoa+" ";
text+=(m_frdList.GetAt(tempPos))->email+" ";
text+=(m_frdList.GetAt(tempPos))->phone+" ";
text+=(m_frdList.GetAt(tempPos))->note+" ";
text+="\n";
m_frdList.GetNext(tempPos);
}
// MessageBox(text);
CFile file("friends.txt",CFile::modeCreate | CFile::modeWrite);
file.Write(text,strlen(text));
file.Close();
CMyDlg::ListRefresh();
}
void CMyDlg::OnDel()
{
// TODO: Add your control notification handler code here
if(EditState==true)
{
MessageBox("请选择您要删除的人");
EditState=false;
CMyDlg::OnFirst();
}
else if(QueryState==true)
{
MessageBox("您当前处于查询状态,请选择'显示全部'退出查询状态在进行删除操作");
}
else
{
//MessageBox("还没做!");
if(curPos==NULL)
{
MessageBox("没有记录被选中!");
}
else
{
POSITION temp=curPos;
CFriend *frd=m_frdList.GetAt(temp);
m_frdList.RemoveAt(temp);
delete frd;
CMyDlg::WriteFiles();
CMyDlg::ListRefresh();
CMyDlg::OnFirst();
}
}
}
void CMyDlg::GetPos()
{
if(m_clc.GetItemCount()==0)
{
curPos=NULL;
}
else if(m_clc.GetFirstSelectedItemPosition()==NULL)
{
curPos=NULL;
}
else
{
CString temp;
temp.Format("%d",m_clc.GetFirstSelectedItemPosition()-1);
int selected=atoi(temp);
curPos=m_frdList.GetHeadPosition();
while(selected!=0)
{
m_frdList.GetNext(curPos);
selected--;
}
}
// MessageBox(m_frdList.GetAt(curPos)->name);
}
void CMyDlg::OnMan()
{
// TODO: Add your control notification handler code here
SetDlgItemText(IDC_SEX,"男");
}
void CMyDlg::OnWoman()
{
// TODO: Add your control notification handler code here
SetDlgItemText(IDC_SEX,"女");
}
void CMyDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
if(MessageBox("你确定要退出吗?","退出确认",MB_OKCANCEL)!=IDOK)
return;
CDialog::OnClose();
}
void CMyDlg::OnQuery()
{
// TODO: Add your control notification handler code here
CString str;
GetDlgItemText(IDC_QUERY,str);
static CRect rectLarge;
static CRect rectSmall;
CRect tempRect;
GetWindowRect(tempRect);
if(rectLarge.IsRectNull())
{
CRect rectSeparator;
GetWindowRect(&rectLarge);
GetDlgItem(IDC_SEPARATOR)->GetWindowRect(&rectSeparator);
rectSmall.left=rectLarge.left;
rectSmall.top=rectLarge.top;
rectSmall.right=rectLarge.right;
rectSmall.bottom=rectSeparator.bottom;
}
if(str=="查询<<")
{
SetWindowPos(NULL,tempRect.left,tempRect.top,rectSmall.Width(),rectSmall.Height(),SWP_NOZORDER);
}
else
{
SetWindowPos(NULL,tempRect.left,tempRect.top,rectLarge.Width(),this->tempRect.Height(),SWP_NOZORDER);
}
if(str=="查询<<")
{
SetDlgItemText(IDC_QUERY,"查询>>");
}
else
{
SetDlgItemText(IDC_QUERY,"查询<<");
}
}
void CMyDlg::OnQquery()
{
// TODO: Add your control notification handler code here
EditState=false;
GetDlgItem(IDC_MAN)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_WOMAN)->ShowWindow(SW_HIDE);
if(QueryItem=="")
MessageBox("请选择查询项目");
else if(QueryItem=="姓名" || QueryItem=="年龄"
|| QueryItem=="电话" || QueryItem=="邮箱"
|| QueryItem=="备注")
{
QueryState=true;
UpdateData(true);
m_QueryText.TrimLeft(" ");
m_QueryText.TrimRight(" ");
this->FindFiles(QueryItem,m_QueryText);
}
else //按照性别查询
{
QueryState=true;
int i=((CComboBox*)GetDlgItem(IDC_SEXLIST))->GetCurSel();
if(i==CB_ERR)
{
MessageBox("请选择性别!");
}
else
{
CString str;
((CComboBox*)GetDlgItem(IDC_SEXLIST))->GetLBText(i,str);
this->FindFiles(QueryItem,str);
}
}
}
void CMyDlg::OnQname()
{
// TODO: Add your control notification handler code here
((CEdit*)GetDlgItem(IDC_QUERYTEXT))->EnableWindow(true);
((CComboBox*)GetDlgItem(IDC_SEXLIST))->EnableWindow(false);
QueryItem="姓名";
CString temp="中包含以下内容:";
CString temp2=QueryItem+temp;
SetDlgItemText(IDC_TIP,temp2);
((CEdit *)GetDlgItem(IDC_QUERYTEXT))->SetFocus();
((CEdit *)GetDlgItem(IDC_QUERYTEXT))->SetSel(0,-1);
}
void CMyDlg::OnQage()
{
// TODO: Add your control notification handler code here
((CEdit*)GetDlgItem(IDC_QUERYTEXT))->EnableWindow(true);
((CComboBox*)GetDlgItem(IDC_SEXLIST))->EnableWindow(false);
QueryItem="年龄";
CString temp="为:";
CString temp2=QueryItem+temp;
SetDlgItemText(IDC_TIP,temp2);
((CEdit *)GetDlgItem(IDC_QUERYTEXT))->SetFocus();
((CEdit *)GetDlgItem(IDC_QUERYTEXT))->SetSel(0,-1);
}
void CMyDlg::OnQmail()
{
// TODO: Add your control notification handler code here
((CEdit*)GetDlgItem(IDC_QUERYTEXT))->EnableWindow(true);
((CComboBox*)GetDlgItem(IDC_SEXLIST))->EnableWindow(false);
QueryItem="邮箱";
CString temp="中包含以下内容:";
CString temp2=QueryItem+temp;
SetDlgItemText(IDC_TIP,temp2);
((CEdit *)GetDlgItem(IDC_QUERYTEXT))->SetFocus();
((CEdit *)GetDlgItem(IDC_QUERYTEXT))->SetSel(0,-1);
}
void CMyDlg::OnQphone()
{
// TODO: Add your control notification handler code here
((CEdit*)GetDlgItem(IDC_QUERYTEXT))->EnableWindow(true);
((CComboBox*)GetDlgItem(IDC_SEXLIST))->EnableWindow(false);
QueryItem="电话";
CString temp="中包含以下内容:";
CString temp2=QueryItem+temp;
SetDlgItemText(IDC_TIP,temp2);
}
void CMyDlg::OnQnote()
{
// TODO: Add your control notification handler code here
((CEdit*)GetDlgItem(IDC_QUERYTEXT))->EnableWindow(true);
((CComboBox*)GetDlgItem(IDC_SEXLIST))->EnableWindow(false);
QueryItem="备注";
CString temp="中包含以下内容:";
CString temp2=QueryItem+temp;
SetDlgItemText(IDC_TIP,temp2);
((CEdit *)GetDlgItem(IDC_QUERYTEXT))->SetFocus();
((CEdit *)GetDlgItem(IDC_QUERYTEXT))->SetSel(0,-1);
}
void CMyDlg::OnQsex()
{
// TODO: Add your control notification handler code here
((CEdit*)GetDlgItem(IDC_QUERYTEXT))->EnableWindow(false);
((CComboBox*)GetDlgItem(IDC_SEXLIST))->EnableWindow(true);
QueryItem="性别";
SetDlgItemText(IDC_TIP,"");
}
void CMyDlg::FindFiles(CString FindItem, CString Text)
{
this->ReadFile();
if(FindItem=="姓名")
{
while(curPos!=NULL)
{
CString temp=m_frdList.GetAt(curPos)->name;
POSITION tempPos=curPos;
m_frdList.GetNext(curPos);
if( (m_frdList.GetAt(tempPos)->name).Find(Text)==-1)
{
CFriend *frd=m_frdList.GetAt(tempPos);
m_frdList.RemoveAt(tempPos);
delete frd;
}
}
this->ListRefreshForFind();
}//end of FindItem
else if(FindItem=="性别")
{
while(curPos!=NULL)
{
CString temp=m_frdList.GetAt(curPos)->name;
POSITION tempPos=curPos;
m_frdList.GetNext(curPos);
if( (m_frdList.GetAt(tempPos)->sex).Find(Text)==-1)
{
CFriend *frd=m_frdList.GetAt(tempPos);
m_frdList.RemoveAt(tempPos);
delete frd;
}
}
this->ListRefreshForFind();
}//end of FindItem
else if(FindItem=="邮箱")
{
while(curPos!=NULL)
{
CString temp=m_frdList.GetAt(curPos)->name;
POSITION tempPos=curPos;
m_frdList.GetNext(curPos);
if( (m_frdList.GetAt(tempPos)->email).Find(Text)==-1)
{
CFriend *frd=m_frdList.GetAt(tempPos);
m_frdList.RemoveAt(tempPos);
delete frd;
}
}
this->ListRefreshForFind();
}//end of FindItem
else if(FindItem=="电话")
{
while(curPos!=NULL)
{
CString temp=m_frdList.GetAt(curPos)->name;
POSITION tempPos=curPos;
m_frdList.GetNext(curPos);
if( (m_frdList.GetAt(tempPos)->phone).Find(Text)==-1)
{
CFriend *frd=m_frdList.GetAt(tempPos);
m_frdList.RemoveAt(tempPos);
delete frd;
}
}
this->ListRefreshForFind();
}//end of FindItem
else if(FindItem=="备注")
{
while(curPos!=NULL)
{
CString temp=m_frdList.GetAt(curPos)->name;
POSITION tempPos=curPos;
m_frdList.GetNext(curPos);
if( (m_frdList.GetAt(tempPos)->note).Find(Text)==-1)
{
CFriend *frd=m_frdList.GetAt(tempPos);
m_frdList.RemoveAt(tempPos);
delete frd;
}
}
this->ListRefreshForFind();
}//end of FindItem
else if(FindItem=="年龄")
{
if(!(this->IsNumber(Text)))
{
MessageBox("请输入正确的年龄!");
((CEdit *)GetDlgItem(IDC_QUERYTEXT))->SetFocus();
((CEdit *)GetDlgItem(IDC_QUERYTEXT))->SetSel(0,-1);
return;
}
while(curPos!=NULL)
{
CString temp=m_frdList.GetAt(curPos)->name;
POSITION tempPos=curPos;
m_frdList.GetNext(curPos);
if(m_frdList.GetAt(tempPos)->age!=atoi(Text))
{
CFriend *frd=m_frdList.GetAt(tempPos);
m_frdList.RemoveAt(tempPos);
delete frd;
}
}
this->ListRefreshForFind();
}
else MessageBox("系统出错,请重新启动");
}
void CMyDlg::ListRefreshForFind()
{
m_clc.DeleteAllItems();
int i=0;
curPos=m_frdList.GetHeadPosition();
while(curPos!=NULL)
{
// MessageBox("abc");
CFriend *frd=m_frdList.GetAt(curPos);
m_clc.InsertItem(i,"村中山"); //见鬼了,我也不知道为什么要加这句,反正不加就是出不来
m_clc.SetItemText(i,0,frd->name);
m_clc.SetItemText(i,1,frd->sex);
CString temp;
temp.Format("%d",frd->age);
m_clc.SetItemText(i,2,temp);
m_clc.SetItemText(i,3,frd->email);
m_clc.SetItemText(i,4,frd->phone);
m_clc.SetItemText(i,5,frd->note);
m_frdList.GetNext(curPos);
i++;
// MessageBox("abc");
}
UpdateData(false);
}
void CMyDlg::OnShowall()
{
// TODO: Add your control notification handler code here
EditState=false;
QueryState=false;
GetDlgItem(IDC_MAN)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_WOMAN)->ShowWindow(SW_HIDE);
this->ReadFile();
this->ListRefresh();
}
bool CMyDlg::IsNumber(CString text)
{
for(int i=0;i<text.GetLength();i++)
{
if(text[i]<48 || text[i]>57)
return false;
}
return true;
}
void CMyDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
SYSTEMTIME st;
GetSystemTime(&st);
CString temp;
temp.Format("%d",st.wYear);
time=temp+".";
temp.Format("%d",st.wMonth);
time=time+temp+".";
temp.Format("%d",st.wDay);
time=time+temp+" ";
temp.Format("%d",(st.wHour+8)%24);
time=time+temp+":";
temp.Format("%d",st.wMinute);
time=time+temp+":";
temp.Format("%d",st.wSecond);
time=time+temp;
temp.Format("%d",(st.wDayOfWeek+6)%7+1);
time=time+" "+"星期"+temp;
//MessageBox(time);
SetDlgItemText(IDC_TIME,time);
CDialog::OnTimer(nIDEvent);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -