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

📄 addresslistdlg.cpp

📁 用vc++实现的通讯录程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// AddressListDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AddressList.h"
#include "AddressListDlg.h"
#include "GroupInfo.h"
#include "People.h"
#include "AppendNewGroupDialog.h"
#include "QueryPeopleDialog.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)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAddressListDlg dialog

CAddressListDlg::CAddressListDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAddressListDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAddressListDlg)
	m_address = _T("");
	m_company = _T("");
	m_mail = _T("");
	m_memory = _T("");
	m_mobile = _T("");
	m_msn = _T("");
	m_phoneInHome = _T("");
	m_phoneInCompany = _T("");
	m_qq = _T("");
	m_sex = 0;
	m_name = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CAddressListDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAddressListDlg)
	DDX_Control(pDX, IDC_TREE_LIST, m_tree_list);
	DDX_Text(pDX, IDC_EDIT_ADDRESS, m_address);
	DDX_Text(pDX, IDC_EDIT_COMPANY, m_company);
	DDX_Text(pDX, IDC_EDIT_MAIL, m_mail);
	DDX_Text(pDX, IDC_EDIT_MEMORY, m_memory);
	DDX_Text(pDX, IDC_EDIT_MOBILE, m_mobile);
	DDX_Text(pDX, IDC_EDIT_MSN, m_msn);
	DDX_Text(pDX, IDC_EDIT_PHONEATHOME, m_phoneInHome);
	DDX_Text(pDX, IDC_EDIT_PHONEINCOMPANY, m_phoneInCompany);
	DDX_Text(pDX, IDC_EDIT_QQ, m_qq);
	DDX_Radio(pDX, IDC_RADIO_MAN, m_sex);
	DDX_Text(pDX, IDC_EDIT_NAME, m_name);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAddressListDlg, CDialog)
	//{{AFX_MSG_MAP(CAddressListDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_APPEND, OnButtonAppend)
	ON_NOTIFY(TVN_SELCHANGED, IDC_TREE_LIST, OnSelchangedTreeList)
	ON_NOTIFY(NM_CLICK, IDC_TREE_LIST, OnClickTreeList)
	ON_BN_CLICKED(IDC_BUTTON_REMOVE, OnButtonRemove)
	ON_BN_CLICKED(IDC_BUTTON_APPENDGROUP, OnButtonAppendgroup)
	ON_BN_CLICKED(IDC_BUTTON_EDIT, OnButtonEdit)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAddressListDlg message handlers

BOOL CAddressListDlg::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
	
	InitTreeList();
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CAddressListDlg::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 CAddressListDlg::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 CAddressListDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CAddressListDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	CDialog::OnCancel();
}

/* 增加按钮 */
void CAddressListDlg::OnButtonAppend() 
{
	// 输入合法
	if(CheckInputInfo())
	{
		// 取得当前选定组
		int nGroupId = GetCurrentSelGroupId();

		// 取得界面数据
		INTERFACECTRLDATA interfaceCtrlData;
		SwitchData(interfaceCtrlData, false);

		CDatabase database;
		database.Open(_T("address"));
		
		// 生成插入指令
		CString strSql;
		if(nGroupId == -1)
		{
			CString strFormat = "INSERT INTO people(fullName, sex, company, \
				phoneInCompany, homeAddress, phineInHome, mobile, mail, qq, \
				msn, memory) VALUES('%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')";
			strSql.Format(strFormat, 
				interfaceCtrlData.strName,		
				interfaceCtrlData.bSex,		
				interfaceCtrlData.strCompany,
				interfaceCtrlData.strPhoneInCompany, 
				interfaceCtrlData.strHomeAddress,
				interfaceCtrlData.strPhoneInHome,
				interfaceCtrlData.strMobile,
				interfaceCtrlData.strMail,
				interfaceCtrlData.strQq,
				interfaceCtrlData.strMsn,
				interfaceCtrlData.strMemory); 
		}
		else
		{
			CString strFormat = "INSERT INTO people(groupId, fullName, sex, company, \
				phoneInCompany, homeAddress, phineInHome, mobile, mail, qq, \
				msn, memory) VALUES(%d, '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')";
			strSql.Format(strFormat, 
				nGroupId,
				interfaceCtrlData.strName,		
				interfaceCtrlData.bSex,		
				interfaceCtrlData.strCompany,
				interfaceCtrlData.strPhoneInCompany, 
				interfaceCtrlData.strHomeAddress,
				interfaceCtrlData.strPhoneInHome,
				interfaceCtrlData.strMobile,
				interfaceCtrlData.strMail,
				interfaceCtrlData.strQq,
				interfaceCtrlData.strMsn,
				interfaceCtrlData.strMemory); 
		}   

		// 执行插入
		database.ExecuteSQL(strSql); 
		database.Close();

		// 清空界面显示
		INTERFACECTRLDATA temp;
		SwitchData(temp, true);

		// 刷新Tree
		RefurTree();
	}	
}

/* 检查界面输入数据 */
bool CAddressListDlg::CheckInputInfo()
{
	// 取得界面数据
	INTERFACECTRLDATA interfaceCtrlData;
	SwitchData(interfaceCtrlData, false);

	// 姓名
	if(interfaceCtrlData.strName.IsEmpty() 
		|| interfaceCtrlData.strName.GetLength() == 0)
	{
		MessageBox("请输入姓名!", "警告", MB_OK | MB_ICONWARNING);
		return false;
	}

	// 性别,无需检查

	// 其它项至少输入一项
	bool bInputFlag = false;
	// 工作单位
	if(interfaceCtrlData.strCompany.GetLength() > 0)
	{
		bInputFlag = true;
		return bInputFlag;
	}

	// 办公电话
	if(interfaceCtrlData.strPhoneInCompany.GetLength() > 0)
	{
		bInputFlag = true;
		return bInputFlag;
	}

	// 住址
	if(interfaceCtrlData.strHomeAddress.GetLength() > 0)
	{
		bInputFlag = true;
		return bInputFlag;
	}

	// 住宅电话
	if(interfaceCtrlData.strPhoneInHome.GetLength() > 0)
	{
		bInputFlag = true;
		return bInputFlag;
	}

	// 手机
	if(interfaceCtrlData.strMobile.GetLength() > 0)
	{
		bInputFlag = true;
		return bInputFlag;
	}

	// 邮箱
	if(interfaceCtrlData.strMail.GetLength() > 0)
	{
		bInputFlag = true;
		return bInputFlag;
	}

	// QQ
	if(interfaceCtrlData.strQq.GetLength() > 0)
	{
		bInputFlag = true;
		return bInputFlag;
	}

	// MSN
	if(interfaceCtrlData.strMsn.GetLength() > 0)
	{
		bInputFlag = true;
		return bInputFlag;
	}

	MessageBox("请至少输入一项联系方式!", "警告" , MB_OK | MB_ICONWARNING);
	return bInputFlag;
}

/**********************************************************************
 * 功  能:	与界面交换数据 
 * 参  数:	INTERFACECTRLDATA&		数据
 *			bool					取数据(false)或更新界面(true)标志
 * 返回值:	void
 *********************************************************************/
void CAddressListDlg::SwitchData(INTERFACECTRLDATA &info, bool flag)
{
	if(flag)
	{// 更新界面
		m_address = info.strHomeAddress;
		m_company = info.strCompany;
		m_mail = info.strMail;
		m_memory = info.strMemory;
		m_mobile = info.strMobile;
		m_msn = info.strMsn;
		m_name = info.strName;
		m_phoneInHome = info.strPhoneInHome;
		m_phoneInCompany = info.strPhoneInCompany;  
		m_qq = info.strQq; 

		if(info.bSex)
		{
			m_sex = 0;  // 男
		}
		else
		{
			m_sex = 1;  // 女
		}

		UpdateData(FALSE);
	}
	else
	{// 取数据
		UpdateData(TRUE);
		info.strHomeAddress = m_address;		
		info.strHomeAddress.TrimLeft();
		info.strHomeAddress.TrimRight(); 

		info.strCompany = m_company;
		info.strCompany.TrimLeft();
		info.strCompany.TrimRight(); 

		info.strMail = m_mail;
		info.strMail.TrimLeft();
		info.strMail.TrimRight();  

		info.strMemory = m_memory;
		info.strMemory.TrimLeft();
		info.strMemory.TrimRight();

		info.strMobile = m_mobile;
		info.strMobile.TrimLeft();
		info.strMobile.TrimRight();

		info.strMsn = m_msn;
		info.strMsn.TrimLeft();
		info.strMsn.TrimRight();

		info.strName = m_name;
		info.strName.TrimLeft();
		info.strName.TrimRight();

		info.strPhoneInHome = m_phoneInHome;
		info.strPhoneInHome.TrimLeft();
		info.strPhoneInHome.TrimRight();

		info.strPhoneInCompany = m_phoneInCompany; 
		info.strPhoneInCompany.TrimLeft();
		info.strPhoneInCompany.TrimRight();

		info.strQq = m_qq;
		info.strQq.TrimLeft();
		info.strQq.TrimRight();

		if(m_sex == 0)
		{
			info.bSex = true;
		}
		else
		{
			info.bSex = false;
		}
			
	}
}

⌨️ 快捷键说明

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