www.gusucode.com > VC++从EML文档或txt中提取出Email邮件地址-源码程序 > VC++从EML文档或txt中提取出Email邮件地址-源码程序\code\EAPDlg.cpp

    //Download by http://www.NewXing.com
// EAPDlg.cpp : implementation file
//

#include "stdafx.h"
#include "EAP.h"
#include "EAPDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CEAPDlg dialog

CEAPDlg::CEAPDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CEAPDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CEAPDlg)
	m_tip = _T("");
	m_path = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CEAPDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEAPDlg)
	DDX_Control(pDX, IDC_LIST1, m_list);
	DDX_Text(pDX, IDC_STATIC1, m_tip);
	DDX_Text(pDX, IDC_EDIT1, m_path);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CEAPDlg, CDialog)
	//{{AFX_MSG_MAP(CEAPDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
	ON_BN_CLICKED(IDC_QUCHONG, OnQuchong)
	ON_BN_CLICKED(IDC_OUTPUT, OnOutput)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEAPDlg message handlers

BOOL CEAPDlg::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
	GetDlgItem(IDOK)->EnableWindow(false);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CEAPDlg::OnBrowse() 
{
	// TODO: Add your control notification handler code here
	BROWSEINFO bi;
	char dispname[MAX_PATH],path[MAX_PATH];
	ITEMIDLIST *pidl;
	//
	bi.hwndOwner=m_hWnd;
	bi.pidlRoot=0; 
	bi.pszDisplayName=dispname;
	bi.lpszTitle="请选择提取路径:";
	bi.ulFlags=BIF_RETURNONLYFSDIRS|BIF_EDITBOX|BIF_DONTGOBELOWDOMAIN ;
	bi.lpfn=0;
	bi.lParam=0;
	bi.iImage=0;
	if(pidl=SHBrowseForFolder(&bi))
	{
		SHGetPathFromIDList(pidl,path);
        m_path=CString(path);
		if(!m_path.IsEmpty())
			GetDlgItem(IDOK)->EnableWindow();
		UpdateData(false);
	}
}

void CEAPDlg::OnQuchong() 
{
	// TODO: Add your control notification handler code here
	if(m_list.GetCount() == 0)
	{
		AfxMessageBox("当前记录为空,不能进行此操作!");
		return;
	}
	
	CString bufstri,bufstrj,tmpstri,tmpstrj;
	int count = m_list.GetCount();
	int i,j,n;
	for(i=0,j=1;i<count,j<count;i++,j++)
	{
		n = m_list.GetTextLen( i );
		m_list.GetText( i, tmpstri.GetBuffer(n) );			
		bufstri = tmpstri.GetBuffer(0);
		
		n = m_list.GetTextLen( j );
		m_list.GetText( j, tmpstrj.GetBuffer(n) );
		bufstrj = tmpstrj.GetBuffer(0);
		
		if(bufstri == bufstrj)
		{
			m_list.DeleteString(j);
			count --;
			i --;
			j --;
		}
		tmpstri.ReleaseBuffer();
		tmpstrj.ReleaseBuffer();
	}
	//		m_list.ResetContent();
	
}

void CEAPDlg::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData(TRUE);
	m_tip = "正在工作......";
	UpdateData(FALSE);
	BrowseDir(m_path);
	m_tip = "已完成!";
	UpdateData(FALSE);
//	CDialog::OnOK();
}

void CEAPDlg::BrowseDir(CString strDir)
{
	CString flagstr(" \"<>\r\n[]{}:;=,'?#!`^$*%()");
	int bufsize = m_list.GetCount();
	CString *Word = new CString [bufsize];
	for(int i=0;i<bufsize;i++)
	{
		m_list.GetText(i,Word[i]);
	}
	
	CFileFind finder;
	CString szDir = strDir;
	
	if(szDir.Right(1) != "\\")
		szDir += "\\";

	szDir += "*.*";
	
	BOOL res = finder.FindFile(szDir);
	while( res )
	{
		res = finder.FindNextFile();
		if(finder.IsDirectory() && !finder.IsDots())
		{
			CString strPath = finder.GetFilePath();
			CString strFileName = finder.GetFileName();
			BrowseDir( strPath );
		}
		else if(!finder.IsDirectory() && !finder.IsDots())
		{
			CString strFileName = finder.GetFileName();
			CString strFilePathName = finder.GetFilePath();
			CString name = strFileName.Right(4);
			if((name.Find(".eml") != -1) || 
				(name.Find(".txt") != -1) || 
				(name.Find(".dbx") != -1) )
			{
				CFile file;
				if(!file.Open(strFilePathName,CFile::modeRead)) continue;
				CString fstring = "";
				char *readbuf ;
				readbuf = fstring.GetBuffer(file.GetLength());
				file.Read(readbuf ,file.GetLength());
				file.Close();
				fstring = readbuf;
				CString s1 = "",s2 = "",s3 = "",s4 = "";
				
				int pos1=0,pos2=0,pos3=0,pos4=0;
				while((pos1 = fstring.Find("@")) != -1)
				{
					s1 = fstring.Left(pos1);
					fstring.Delete(0,pos1+1);
					s1.MakeReverse();
					if((pos2=s1.FindOneOf(flagstr)) != -1)
					{
						s2 = s1.Left(pos2);
						s2.MakeReverse();
					}
					if((pos3=fstring.FindOneOf(flagstr)) != -1)
					{
						s3 = fstring.Left(pos3);
					}
					if(s2!="" && (s3.Find(".")!=-1))
					{
						s4 = s2 + "@" + s3;
						m_list.AddString(s4);
					}
				}
			}
		}
	}
	finder.Close();
	delete []Word;
}

void CEAPDlg::OnOutput() 
{
	// TODO: Add your control notification handler code here
	int count = m_list.GetCount();

	CFileDialog dlg(FALSE,_T("txt"),"*.txt",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
		_T("文本文件|*.txt|"));
	dlg.m_ofn.Flags |=OFN_NOCHANGEDIR;
	CString lpszFile;
	CString bufString=_T(""),tmpstr=_T("");
	if (dlg.DoModal() == IDOK)
	{
		lpszFile = dlg.GetPathName();
		CFile File;
		int n;
		File.Open(lpszFile,CFile::modeCreate|CFile::modeWrite);
		for(int i=0;i<count;i++)
		{
			n = m_list.GetTextLen( i );
			m_list.GetText( i, tmpstr.GetBuffer(n) );			
			bufString += tmpstr.GetBuffer(0);
			bufString += "\r\n";
			tmpstr.ReleaseBuffer();
		}
		File.Write(bufString,bufString.GetLength());
		File.Close();
		
		if(MessageBox("导出成功!\n\n要查看导出的文件吗?","导出信息",MB_YESNO|MB_ICONQUESTION)==IDYES)
		{
			CString cmdline = "Notepad.exe "+lpszFile;
			WinExec(cmdline,SW_SHOW);
		}
	}
}