?? familytreedlg.cpp
字號(hào):
// FamilytreeDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Familytree.h"
#include "FamilytreeDlg.h"
#include "FileOpenAndSaveDlg.h"
#include "AddInfoDlg.h"
#include "DelInfoDlg.h"
#include "ModifyInfoDlg.h"
#include "RelationsDlg.h"
#include "PersonalInfoDlg.h"
#include "BirthdayDlg.h"
#include "SearchGenerationDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFamilytreeDlg dialog
CFamilytreeDlg::CFamilytreeDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFamilytreeDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CFamilytreeDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CFamilytreeDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFamilytreeDlg)
DDX_Control(pDX, IDC_LIST_PEDIGREE, m_peList);
DDX_Control(pDX, IDC_TREE_PEDIGREE, m_peTree);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CFamilytreeDlg, CDialog)
//{{AFX_MSG_MAP(CFamilytreeDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_COMMAND(IDC_HELP_PEDIGREE, OnHelpFamilytree)
ON_COMMAND(IDC_FILE_EXIT, OnFileExit)
ON_COMMAND(IDC_FILE_OPEN, OnFileOpen)
ON_COMMAND(IDC_FILE_SAVE, OnFileSave)
ON_COMMAND(IDC_PEDIGREE_ADD, OnFamilytreeAdd)
ON_COMMAND(IDC_PEDIGREE_DELETE, OnFamilytreeDelete)
ON_COMMAND(IDC_PEDIGREE_MODIFY, OnFamilytreeModify)
ON_NOTIFY(TVN_SELCHANGED, IDC_TREE_PEDIGREE, OnSelchangedTreeFamilytree)
ON_COMMAND(IDC_PEDIGREE_RELATIONS, OnFamilytreeRelations)
ON_COMMAND(IDC_PEDIGREE_PERSONAL_INFO, OnFamilytreePersonalInfo)
ON_COMMAND(IDC_PEDIGREE_IGENERATION_INFO, OnFamilytreeIgenerationInfo)
ON_COMMAND(IDC_ADD, OnAdd)
ON_COMMAND(IDC_DELETE, OnDelete)
ON_COMMAND(IDC_MODIFY, OnModify)
ON_COMMAND(IDC_PEDIGREE_SORT, OnFamilytreeSort)
ON_COMMAND(IDC_FILE_SAVEAS, OnFileSaveas)
ON_COMMAND(IDC_FILE_NEW, OnFileNew)
ON_BN_CLICKED(IDC_BIRTHDAY, OnBirthday)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFamilytreeDlg message handlers
BOOL CFamilytreeDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 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
savepath[0]='\0'; //初始保存路徑置空
IsFamilytreeModified=false; //初始家譜沒(méi)有被修改
InitListCtrl(); //初始化列表控件(準(zhǔn)備顯示信息)
GetDlgItem(IDC_ADD)->EnableWindow(FALSE);
GetDlgItem(IDC_DELETE)->EnableWindow(FALSE);
GetDlgItem(IDC_MODIFY)->EnableWindow(FALSE);
GetDlgItem(IDC_PEDIGREE_IGENERATION_INFO)->EnableWindow(FALSE);
GetDlgItem(IDC_PEDIGREE_SORT)->EnableWindow(FALSE);
GetDlgItem(IDC_PEDIGREE_RELATIONS)->EnableWindow(FALSE);
GetDlgItem(IDC_PEDIGREE_PERSONAL_INFO)->EnableWindow(FALSE);
GetDlgItem(IDC_BIRTHDAY)->EnableWindow(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CFamilytreeDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CFamilytreeDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CFamilytreeDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CFamilytreeDlg::OnHelpFamilytree()
{
// TODO: Add your command handler code here
CAboutDlg dlg;
if(dlg.DoModal()==FALSE)
return;
}
void CFamilytreeDlg::OnFileExit() //結(jié)束家譜文件
{
// TODO: Add your command handler code here
if(IsFamilytreeModified){ //如家譜被修改,給出保存提示
SaveTip();
IsFamilytreeModified=false;
}
MessageBox("謝謝您使用本程序!由于作者對(duì)于MFC類(lèi)的開(kāi)發(fā)經(jīng)驗(yàn)不足,BUG難免有之,還請(qǐng)多多包涵:)","徐昕實(shí)話實(shí)說(shuō)",MB_OK);
PostMessage(WM_QUIT,0,0); //發(fā)送退出消息
}
void CFamilytreeDlg::OnFileOpen()
{
// TODO: Add your command handler code here
if(IsFamilytreeModified){ //如家譜被修改,給出保存提示
SaveTip();
IsFamilytreeModified=false;
}
CFileOpenAndSaveDlg dlg(true); //打開(kāi)文件對(duì)話框
if(dlg.DoModal()==IDCANCEL)
return;
UpdateData(false);
if(strcmp(dlg.m_filePath,"")==0){
AfxMessageBox("文件名不能為空!");
return;
}
int result;
result=operFamilytree.CreateFamilytree(dlg.m_filePath); //根據(jù)用戶指定的文件名建立家譜
if(result!=OK) //建立家譜時(shí)有錯(cuò)誤
{
switch(result){
case NOT_ENOUGH_MEMORY: //內(nèi)存不足
AfxMessageBox("內(nèi)存不足!");
break;
case READ_FILE_ERROR: //讀文件錯(cuò)誤
AfxMessageBox("文件無(wú)法打開(kāi)!");
break;
case FILE_DATA_NOT_PRACTICAL: //文件數(shù)據(jù)不合實(shí)際
AfxMessageBox("家譜日期數(shù)據(jù)不合實(shí)際!");
break;
case FILE_DATA_ERROR: //文件數(shù)據(jù)錯(cuò)誤(一個(gè)人的父親的名字在家譜中從未出現(xiàn))
AfxMessageBox("家譜成員關(guān)系有誤!");
break;
case PEDIGREE_EMPTY: //家譜沒(méi)有根結(jié)點(diǎn)
AfxMessageBox("家譜沒(méi)有根結(jié)點(diǎn)!");
break;
}
return;
}
strcpy(savepath,dlg.m_filePath); //修改保存文件路徑為當(dāng)前打開(kāi)文件路徑
RefreshTree(); //刷新樹(shù)型控件
RefreshList(); //刷新列表控件
BirthdayTip(); //提示今天是家譜中哪些人的生日
GetDlgItem(IDC_ADD)->EnableWindow(TRUE); //家譜控制按鈕可用
GetDlgItem(IDC_DELETE)->EnableWindow(TRUE);
GetDlgItem(IDC_MODIFY)->EnableWindow(TRUE);
GetDlgItem(IDC_PEDIGREE_IGENERATION_INFO)->EnableWindow(TRUE);
GetDlgItem(IDC_PEDIGREE_SORT)->EnableWindow(TRUE);
GetDlgItem(IDC_PEDIGREE_RELATIONS)->EnableWindow(TRUE);
GetDlgItem(IDC_PEDIGREE_PERSONAL_INFO)->EnableWindow(TRUE);
GetDlgItem(IDC_BIRTHDAY)->EnableWindow(TRUE);
}
void CFamilytreeDlg::OnFileSave() //保存家譜
{
// TODO: Add your command handler code here
if(operFamilytree.GetRoot()==0)
return;
int saveResult;
saveResult=operFamilytree.SaveFamilytree(savepath); //保存
while(saveResult==WRITE_FILE_ERROR){
AfxMessageBox(CString("找不到文件 ")+CString(savepath)+CString("該文件可能已被刪除."));
CFileOpenAndSaveDlg dlg(false); //保存文件對(duì)話框
if(dlg.DoModal()==IDCANCEL)
return;
UpdateData(false);
if(strcmp(dlg.m_filePath,"")==0){
AfxMessageBox("文件名不能為空!");
return;
}
strcpy(savepath,dlg.m_filePath);
saveResult=operFamilytree.SaveFamilytree(savepath); //保存
}
IsFamilytreeModified=false;
}
void CFamilytreeDlg::OnFileSaveas() //家譜另存為
{
// TODO: Add your command handler code here
CFileOpenAndSaveDlg dlg(false); //保存文件對(duì)話框
if(dlg.DoModal()==IDCANCEL)
return;
UpdateData(false);
if(strcmp(dlg.m_filePath,"")==0){
AfxMessageBox("文件名不能為空!");
return;
}
strcpy(savepath,dlg.m_filePath);
int saveResult;
saveResult=operFamilytree.SaveFamilytree(savepath); //保存
if(saveResult==WRITE_FILE_ERROR)
IsFamilytreeModified=true;
else
IsFamilytreeModified=false;
}
void CFamilytreeDlg::DisplayFamilytree(Person& pNode) //顯示家譜
{
HTREEITEM hRootItem,hItem;
hRootItem=m_peTree.GetRootItem();
TVINSERTSTRUCT tvInsert;
tvInsert.hInsertAfter=0;
tvInsert.item.mask = TVIF_TEXT;
tvInsert.item.cchTextMax = MAX_CHARNUM;
tvInsert.item.pszText=_T(pNode->info.name);
if(pNode){
if(pNode->parent){
FindInTree(hRootItem,hItem,pNode->parent->info.name);
tvInsert.hParent=hItem;
}
else
tvInsert.hParent=0;
m_peTree.InsertItem(&tvInsert);
DisplayFamilytree(pNode->child);
DisplayFamilytree(pNode->sibling);
}
}
void CFamilytreeDlg::FindInTree(HTREEITEM& hRootItem,HTREEITEM& hItem,char* name) //按名字查找
{
HTREEITEM hParent,hCurrent;
hParent=hCurrent=hRootItem;
hItem=0;
while (hParent != NULL) //hParent有效時(shí)
{
// Get the text for the item. Notice we use TVIF_TEXT because
// we want to retrieve only the text, but also specify TVIF_HANDLE
// because we're getting the item by its handle.
TVITEM item;
TCHAR szText[MAX_CHARNUM];
item.hItem = hParent;
item.mask = TVIF_TEXT | TVIF_HANDLE;
item.pszText = szText;
item.cchTextMax = MAX_CHARNUM;
// If we successfuly retrieved an item, and the item's text
// equals the given string "name", return the item's handle.
BOOL bWorked =m_peTree.GetItem(&item);
if (bWorked && strcmp(item.pszText,name)==0){ //如果此項(xiàng)等于name,則保存其句柄
hItem=hParent;
return;
}
hCurrent=m_peTree.GetNextItem(hParent,TVGN_CHILD); //得到hParent項(xiàng)的第一個(gè)子項(xiàng)
FindInTree(hCurrent,hItem,name); //遞歸搜索s
hParent=m_peTree.GetNextItem(hParent, TVGN_NEXT); //查找hParent項(xiàng)的下一個(gè)兄弟項(xiàng)
}
}
void CFamilytreeDlg::OnFamilytreeAdd()
{
// TODO: Add your command handler code here
CAddInfoDlg dlg;
if(dlg.DoModal()==IDCANCEL) //彈出對(duì)話框
return;
UpdateData(FALSE);
Person addnode;
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -