⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 familytreedlg.cpp

📁 数据结构——家谱的实现 本人的课程设计 MFC
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// 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;			//初始家谱没有被修改
	InitListCtrl();						//初始化列表控件(准备显示信息)
    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()                       //结束家谱文件
{
	// TODO: Add your command handler code here
	if(IsFamilytreeModified){							//如家谱被修改,给出保存提示
		SaveTip();
		IsFamilytreeModified=false;
	}
	MessageBox("谢谢您使用本程序!由于作者对于MFC类的开发经验不足,BUG难免有之,还请多多包涵:)","徐昕实话实说",MB_OK);
	PostMessage(WM_QUIT,0,0);						//发送退出消息
}

void CFamilytreeDlg::OnFileOpen()
{
	// TODO: Add your command handler code here
	if(IsFamilytreeModified){				//如家谱被修改,给出保存提示
		SaveTip();
		IsFamilytreeModified=false;
	}
	
	CFileOpenAndSaveDlg dlg(true);					//打开文件对话框
	if(dlg.DoModal()==IDCANCEL)
		return;
	UpdateData(false);							
	if(strcmp(dlg.m_filePath,"")==0){
		AfxMessageBox("文件名不能为空!");
		return;
	}
	int result;
	result=operFamilytree.CreateFamilytree(dlg.m_filePath);		//根据用户指定的文件名建立家谱
	if(result!=OK)									            //建立家谱时有错误
	{
		switch(result){
		case NOT_ENOUGH_MEMORY:						//内存不足
			AfxMessageBox("内存不足!");
			break;
		case READ_FILE_ERROR:						//读文件错误
			AfxMessageBox("文件无法打开!");
			break;
		case FILE_DATA_NOT_PRACTICAL:				//文件数据不合实际
			AfxMessageBox("家谱日期数据不合实际!");
			break;			
		case FILE_DATA_ERROR:						//文件数据错误(一个人的父亲的名字在家谱中从未出现)
			AfxMessageBox("家谱成员关系有误!");
			break;
		case PEDIGREE_EMPTY:                        //家谱没有根结点
			AfxMessageBox("家谱没有根结点!");
			break;
		}
		return;
	}
	strcpy(savepath,dlg.m_filePath);	//修改保存文件路径为当前打开文件路径
	RefreshTree();						//刷新树型控件
	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);						//保存文件对话框
		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);		//保存文件对话框
	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有效时
	{
		// 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){		//如果此项等于name,则保存其句柄
			hItem=hParent;
			return;
		}
		hCurrent=m_peTree.GetNextItem(hParent,TVGN_CHILD);	//得到hParent项的第一个子项
		FindInTree(hCurrent,hItem,name);					//递归搜索s
		hParent=m_peTree.GetNextItem(hParent, TVGN_NEXT);	//查找hParent项的下一个兄弟项
	}
}

void CFamilytreeDlg::OnFamilytreeAdd() 
{
	// TODO: Add your command handler code here
	CAddInfoDlg dlg;					
	if(dlg.DoModal()==IDCANCEL)							//弹出对话框
		return;
	UpdateData(FALSE);						
	Person addnode;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -