/************************************************************************/
/*
邮件地址管理程序 主窗口模块
北京交通大学 计科1101 李高锋 11281009 */
/************************************************************************/
#include "stdafx.h"
#include "EMailManager.h"
#include "EMailManagerDlg.h"
#include "AddGeneral.h"
#include "MemberOfGroup.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
extern CRecordset m_rec;
extern CDatabase m_db;
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()
/////////////////////////////////////////////////////////////////////////////
// CEMailManagerDlg dialog
CEMailManagerDlg::CEMailManagerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CEMailManagerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CEMailManagerDlg)
m_srh = _T("");
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CEMailManagerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEMailManagerDlg)
DDX_Control(pDX, IDC_LIST_ALL, m_ListAll);
DDX_Text(pDX, IDC_EDIT_SRH, m_srh);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CEMailManagerDlg, CDialog)
//{{AFX_MSG_MAP(CEMailManagerDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_ADDGENERAL, OnButtonAddgeneral)
ON_BN_CLICKED(IDC_BUTTON_ADDGROUP, OnButtonAddgroup)
ON_BN_CLICKED(IDC_BUTTON_DEL, OnButtonDel)
ON_BN_CLICKED(IDC_BUTTON_SRH, OnButtonSrh)
ON_NOTIFY(NM_CLICK, IDC_LIST_ALL, OnClickListAll)
ON_BN_CLICKED(IDC_BUTTON_ADDONETOGROUP, OnButtonAddonetogroup)
ON_BN_CLICKED(IDC_BUTTON_RESTORE, OnButtonRestore)
ON_BN_CLICKED(IDC_BUTTON_LISTMEM, OnButtonListmem)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEMailManagerDlg message handlers
BOOL CEMailManagerDlg::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);
}
}
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
m_ListAll.InsertColumn(0, "");
m_ListAll.InsertColumn(1, "邮箱地址");
m_ListAll.InsertColumn(2, "姓名");
m_ListAll.InsertColumn(3, "电话");
m_ListAll.InsertColumn(4, "邮箱类型");
m_ListAll.InsertColumn(5, "ID");
m_ListAll.SetColumnWidth(0, 0);
m_ListAll.SetColumnWidth(1, 250);
m_ListAll.SetColumnWidth(2, 150);
m_ListAll.SetColumnWidth(3, 200);
m_ListAll.SetColumnWidth(4, 120);
m_ListAll.SetColumnWidth(5, 0);
m_ListAll.SetExtendedStyle(m_ListAll.GetExtendedStyle() | LVS_EX_HEADERDRAGDROP | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
GetDlgItem(IDC_BUTTON_ADDONETOGROUP)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON_LISTMEM)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON_DEL)->EnableWindow(FALSE);
ShowDataInListCtrl("select * from Mail where Parent = 0 order by MailName desc");
return TRUE; // return TRUE unless you set the focus to a control
}
void CEMailManagerDlg::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 CEMailManagerDlg::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();
}
}
HCURSOR CEMailManagerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CEMailManagerDlg::OnButtonAddgeneral()
{
CAddGeneral AddGeneralDlg;
AddGeneralDlg.AddType = "个人邮箱";
AddGeneralDlg.m_info = "添加个人邮箱";
AddGeneralDlg.DoModal();
ShowDataInListCtrl("select * from Mail where Parent = 0 order by MailName desc");
}
void CEMailManagerDlg::OnButtonAddgroup()
{
CAddGeneral AddGeneralDlg;
AddGeneralDlg.AddType = "群组邮箱";
AddGeneralDlg.m_info = "添加群组邮箱";
AddGeneralDlg.DoModal();
ShowDataInListCtrl("select * from Mail where Parent = 0 order by MailName desc");
}
void CEMailManagerDlg::OnButtonDel()
{
int nItem = m_ListAll.GetSelectionMark();
CString Type = m_ListAll.GetItemText(nItem, 4);
CString ID = m_ListAll.GetItemText(nItem, 5);
CString Mail = m_ListAll.GetItemText(nItem, 1);
if (MessageBox( "您确定删除此邮箱吗?\n" + Mail, "邮箱管理---提示" , MB_YESNO) == IDNO)
{
return;
}
if (Type == "个人邮箱")
{
CString strSQL = "delete from Mail where ID = " + ID;
m_db.ExecuteSQL(LPCSTR(strSQL));
m_ListAll.DeleteItem(nItem);
}
else if (Type == "群组邮箱")
{
if (MessageBox( "这是一个群组邮箱,可能还包含一些成员邮箱。\n确定要删除此邮箱并移除其所有成员吗?", "邮箱管理---提示" , MB_YESNO) == IDNO)
{
return;
}
CString strSQL = "delete from Mail where ID = " + ID;
m_db.ExecuteSQL(LPCSTR(strSQL));
m_ListAll.DeleteItem(nItem);
strSQL = "delete from Mail where Parent = " + ID;
m_db.ExecuteSQL(LPCSTR(strSQL));
}
else
{
AfxMessageBox("出错了!可能是当前没有选中项!");
return;
}
}
void CEMailManagerDlg::OnButtonSrh()
{
UpdateData(TRUE);
if (m_srh == "")
{
AfxMessageBox("请输入要查找的邮箱地址!");
return;
}
CString strSQL = "select * from Mail where EMail = '" + m_srh + "' AND Parent = 0" ;
ShowDataInListCtrl(strSQL);
if (m_ListAll.GetItemCount() == 1)
{
CString Type = m_ListAll.GetItemText(0, 4);
CString ID = m_ListAll.GetItemText(0, 5);
if (Type != "群组邮箱")
{
return;
}
if (MessageBox( "您查找的邮箱是一个群组邮箱,需要显示其成员邮箱吗?", "邮箱管理---提示" , MB_YESNO) == IDNO)
{
return;
}
CMemberOfGroup MemDlg;
MemDlg.m_ParentId = ID;
MemDlg.DoModal();
}
else if (m_ListAll.GetItemCount() == 0)
{
AfxMessageBox("没有查询结果!");
return;
}
else
{
AfxMessageBox("出错了!");
}
}
void CEMailManagerDlg::ShowDataInListCtrl(CString strSQL)
{
GetDlgItem(IDC_BUTTON_DEL)->EnableWindow(FALSE);
BSTR bstrSQL=strSQL.AllocSysString();
m_rec.Open(CRecordset::dynaset,st