1、主对话框h文件:
CString m_Name;
CString m_othername;
CString m_phone1;
CString m_phone2;
CString m_mail;
CString m_address;
CString m_text;
CXTreeCtrl m_treeCtrl;
CDateTimeCtrl m_date;
CString m_new_name;
HTREEITEM hNewItem;
2、主对话框cpp:
添加分组:
CAdddlg dlg;
if (dlg.DoModal()==IDOK)
{
m_new_name=dlg.m_new_name;
HTREEITEM hNewItem = m_treeCtrl.InsertItem(m_new_name,0,0,TVI_ROOT,TVI_LAST); //添加新节点
m_treeCtrl.SelectItem(hNewItem); //选中新节点
m_treeCtrl.EnsureVisible(hNewItem);//确保新节点在可视范围内
}
子对话框:
UpdateData(TRUE);
CDialog::OnOK();
3、删除分组:
HTREEITEM hItem = m_treeCtrl.GetSelectedItem();
if(hItem ==NULL||m_treeCtrl.GetParentItem(hItem)!=NULL)
{
AfxMessageBox(_T("请选择一个分组!"));
return ;
}
else if(m_treeCtrl.GetItemText(hItem)==L"未分组")
{
AfxMessageBox(_T("未分组不能删除!"));
}
else
{
//HTREEITEM hmyItem = m_TreeCtrl.GetSelectedItem();
if (m_treeCtrl.ItemHasChildren(hItem))
{
HTREEITEM hNextItem;
HTREEITEM hChildItem = m_treeCtrl.GetChildItem(hItem);
while (hChildItem != NULL)
{
hNextItem = m_treeCtrl.GetNextItem(hChildItem, TVGN_NEXT);
CString strmove =m_treeCtrl.GetItemText(hChildItem);
m_treeCtrl.InsertItem(strmove,1,1,hNewItem,TVI_LAST);
m_treeCtrl.DeleteItem(hChildItem);
hChildItem = hNextItem;
}
}
m_treeCtrl.DeleteItem(hItem);
CButton *pBtn3 = (CButton *)GetDlgItem(IDC_DEL);
pBtn3->EnableWindow(FALSE);
}
4、OnInitDialog():
HICON icon[2];
icon[0]=AfxGetApp()->LoadIcon (IDI_ICON1);
icon[1]=AfxGetApp()->LoadIcon (IDI_ICON2);
CImageList *m_imagelist=new CImageList;
m_imagelist->Create(32,32,ILC_MASK | ILC_COLOR32 ,2,2);//前面的两个16是定义图标大小,后面两个数字更具需要自行改变
m_imagelist->Add(icon[0]); //把图标载入图像列表控件
m_imagelist->Add(icon[1]);
m_treeCtrl.SetImageList(m_imagelist,LVSIL_NORMAL);
hNewItem = m_treeCtrl.InsertItem(L"未分组", 0, 0, TVI_ROOT, TVI_FIRST);
CButton *pBtn1 = (CButton *)GetDlgItem(IDC_BUTTON_MODIFY);
pBtn1->EnableWindow(FALSE);
CButton *pBtn2 = (CButton *)GetDlgItem(IDC_BUTTON_DELETE);
pBtn2->EnableWindow(FALSE);
CButton *pBtn3 = (CButton *)GetDlgItem(IDC_DEL);
pBtn3->EnableWindow(FALSE);
CTime ct(2000, 1, 1, 0, 0, 0);
m_date.SetTime(&ct);
5、信息添加:
UpdateData(TRUE);
//AfxMessageBox(m_Name);
if (m_Name == L"")
{
AfxMessageBox(L"请输入姓名!");
return;
}
CString strfilepath;
strfilepath.Format(L".\\person//%s.ini", m_Name);
//AfxMessageBox(strfilepath);
::WritePrivateProfileString(L"Info", L"name", m_Name, strfilepath);
::WritePrivateProfileString(L"Info", L"othername", m_othername, strfilepath);
::WritePrivateProfileString(L"Info", L"phone1", m_phone1, strfilepath);
::WritePrivateProfileString(L"Info", L"phone2", m_phone2, strfilepath);
::WritePrivateProfileString(L"Info", L"mail", m_mail, strfilepath);
::WritePrivateProfileString(L"Info", L"address", m_address, strfilepath);
::WritePrivateProfileString(L"Info", L"text", m_text, strfilepath);
CTime tm;
m_date.GetTime(tm);
CString strtime;
strtime = tm.Format("%Y");
::WritePrivateProfileString(L"Info", L"year", strtime, strfilepath);
strtime = tm.Format("%m");
::WritePrivateProfileString(L"Info", L"month", strtime, strfilepath);
strtime = tm.Format("%d");
::WritePrivateProfileString(L"Info", L"day", strtime, strfilepath);
HTREEITEM ht = m_treeCtrl.InsertItem(m_Name, 1, 1, hNewItem, TVI_LAST);
//m_treeCtrl.SelectItem(ht);
//确保新节点在可视范围内
m_treeCtrl.EnsureVisible(ht);
6、修改:
HTREEITEM hItem = m_treeCtrl.GetSelectedItem();
if (hItem == NULL || m_treeCtrl.GetParentItem(hItem) == NULL)
{
AfxMessageBox(_T("请选择一个子节点!"));
return;
}
else
{
UpdateData(TRUE);
CString strfilepath;
strfilepath.Format(L".\\person//%s.ini", m_Name);
DeleteFile(strfilepath);
::WritePrivateProfileString(L"Info", L"name", m_Name, strfilepath);
::WritePrivateProfileString(L"Info", L"othername", m_othername, strfilepath);
::WritePrivateProfileString(L"Info", L"phone1", m_phone1, strfilepath);
::WritePrivateProfileString(L"Info", L"phone2", m_phone2, strfilepath);
::WritePrivateProfileString(L"Info", L"mail", m_mail, strfilepath);
::WritePrivateProfileString(L"Info", L"address", m_address, strfilepath);
::WritePrivateProfileString(L"Info", L"text", m_text, strfilepath);
CTime tm;
m_date.GetTime(tm);
CString strtime;
strtime = tm.Format("%Y");
::WritePrivateProfileString(L"Info", L"year", strtime, strfilepath);
strtime = tm.Format("%m");
::WritePrivateProfileString(L"Info", L"month", strtime, strfilepath);
strtime = tm.Format("%d");
::WritePrivateProfileString(L"Info", L"day", strtime, strfilepath);
}
7、删除:
HTREEITEM hItem = m_treeCtrl.GetSelectedItem();
if(hItem ==NULL||m_treeCtrl.GetParentItem(hItem)==NULL)
{
AfxMessageBox(_T("请选择一个子节点!"));
return ;
}
else
{
m_treeCtrl.DeleteItem(hItem);
CTime ct(2000,1,1,0,0,0);
m_date.SetTime(&ct);
UpdateData(FALSE);
}
m_Name.Format(L"");
m_othername.Format(L"");
m_phone1.Format(L"");
m_phone2.Format(L"");
m_mail.Format(L"");
m_text.Format(L"");
m_address.Format(L"");
CButton *pBtn1 = (CButton *)GetDlgItem(IDC_BUTTON_MODIFY);
pBtn1->EnableWindow(FALSE);
CButton *pBtn2 = (CButton *)GetDlgItem(IDC_BUTTON_DELETE);
pBtn2->EnableWindow(FALSE);
UpdateData(FALSE);
8、树形控件->属性->控制事件
1、NM_CLICK:
DWORD dwpos = GetMessagePos();
TVHITTESTINFO ht = { 0 };
ht.pt.x = GET_X_LPARAM(dwpos);
ht.pt.y = GET_Y_LPARAM(dwpos);
::MapWindowPoints(HWND_DESKTOP, pNMHDR->hwndFrom, &ht.pt, 1); //把屏幕坐标转换成控件坐标
TreeView_HitTest(pNMHDR->hwndFrom, &ht); //确定点击的是哪一项
//MessageBox(m_treeCtrl.GetItemText(ht.hItem));
if (m_treeCtrl.GetParentItem(ht.hItem) != NULL)
{
m_Name = m_treeCtrl.GetItemText(ht.hItem);
CString strFileName;
strFileName.Format(L".\\person//%s.ini", m_Name);
CFileFind fFind;
if (fFind.FindFile(strFileName) == TRUE)
{
GetPrivateProfileString(L"Info", L"name", L"", m_Name.GetBuffer(20), 20, strFileName);
GetPrivateProfileString(L"Info", L"othername", L"", m_othername.GetBuffer(20), 20, strFileName);
GetPrivateProfileString(L"Info", L"phone1", L"", m_phone1.GetBuffer(20), 20, strFileName);
GetPrivateProfileString(L"Info", L"phone2", L"", m_phone2.GetBuffer(20), 20, strFileName);
GetPrivateProfileString(L"Info", L"mail", L"", m_mail.GetBuffer(20), 20, strFileName);
GetPrivateProfileString(L"Info", L"address", L"", m_address.GetBuffer(20), 20, strFileName);
GetPrivateProfileString(L"Info", L"text", L"", m_text.GetBuffer(20), 20, strFileName);
int a, b, c;
a = ::GetPrivateProfileInt(L"Info", L"year", 2000, strFileName);
b = ::GetPrivateProfileInt(L"Info", L"month", 1, strFileName);
c = ::GetPrivateProfileInt(L"Info", L"day", 1, strFileName);
CTime ct(a, b, c, 0, 0, 0);
m_date.SetTime(&ct);
UpdateData(FALSE);
//AfxMessageBox(L"子节点");
CButton *pBtn1 = (CButton *)GetDlgItem(IDC_BUTTON4);
pBtn1->EnableWindow(TRUE);
CButton *pBtn2 = (CButton *)GetDlgItem(IDC_BUTTON5);
pBtn2->EnableWindow(TRUE);
CButton *pBtn3 = (CButton *)GetDlgItem(IDC_BUTTON2);
pBtn3->EnableWindow(FALSE);
return;
}
}
else
{
m_Name.Format(L"");
m_othername.Format(L"");
m_phone1.Format(L"");
m_phone2.Format(L"");
m_mail.Format(L"");
m_text.Format(L"");
m_address.Format(L"");
CTime ct(2000, 1, 1, 0, 0, 0);
m_date.SetTime(&ct);
UpdateData(FALSE);
//AfxMessageBox(m_address);
CButton *pBtn1 = (CButton *)GetDlgItem(IDC_BUTTON4);
pBtn1->EnableWindow(FALSE);
CButton *pBtn2 = (CButton *)GetDlgItem(IDC_BUTTON5);
pBtn2->EnableWindow(FALSE);
CButton *pBtn3 = (CButton *)GetDlgItem(IDC_BUTTON2);
pBtn3->EnableWindow(TRUE);
}
*pResult = 0;
通讯簿的制作
最新推荐文章于 2024-08-21 18:16:36 发布