// HashCalcDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "HashCalc.h"
#include "HashCalcDlg.h"
#include ".\hashcalcdlg.h"
#include <atlenc.h>
#include <atlpath.h>
#include "Functions.h"
#include "Localization.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// 对话框数据
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
public:
	afx_msg void OnStnClickedStcLogo();
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	ON_STN_CLICKED(IDC_STC_LOGO, OnStnClickedStcLogo)
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()


// CHashCalcDlg 对话框



CHashCalcDlg::CHashCalcDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CHashCalcDlg::IDD, pParent)
	, m_nEncode(0)
	, m_strInput(_T(""))
	, m_strStrResult(_T(""))
	, m_strFilePath(_T(""))
	, m_strClipText(_T(""))
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_pKnownFile = NULL;
	m_pEd2kHashThread = NULL;
	m_pHashTrd = NULL;

	GetLocalInformation();
}

CHashCalcDlg::~CHashCalcDlg()
{
}

void CHashCalcDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Radio(pDX, IDC_RADIO_MD5, m_nEncode);
	DDX_Text(pDX, IDC_EDT_INPUTSTR, m_strInput);
	DDX_Text(pDX, IDC_EDT_STRRESULT, m_strStrResult);
	DDX_Text(pDX, IDC_EDT_FILEPATH, m_strFilePath);
	DDX_Control(pDX, IDC_LIST_FILERESULTS, m_ctrlFileResults);
	DDX_Text(pDX, IDC_EDIT_CLIPTEXT, m_strClipText);
	DDX_Control(pDX, IDC_COMBO1, m_ctrlLang);
}

BEGIN_MESSAGE_MAP(CHashCalcDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_EN_CHANGE(IDC_EDT_INPUTSTR, OnEnChangeEdtInputstr)
	ON_BN_CLICKED(IDC_BTN_LOADFILE, OnBnClickedBtnLoadfile)
    ON_WM_DROPFILES()
	ON_BN_CLICKED(IDC_RADIO_MD5, OnBnClickedRadioMd5)
	ON_BN_CLICKED(IDC_RADIO_ED2K, OnBnClickedRadioEd2k)
	ON_BN_CLICKED(IDC_RADIO1, OnBnClickedRadio1)
	ON_BN_CLICKED(IDC_RADIO2, OnBnClickedRadio2)
	ON_BN_CLICKED(IDC_RADIO3, OnBnClickedRadio3)
	ON_BN_CLICKED(IDC_RADIO4, OnBnClickedRadio4)
	ON_BN_CLICKED(IDC_RADIO6, OnBnClickedRadio6)
	ON_BN_CLICKED(IDC_RADIO7, OnBnClickedRadio7)
	ON_BN_CLICKED(IDC_RADIO8, OnBnClickedRadio8)
	ON_BN_CLICKED(IDC_RADIO9, OnBnClickedRadio9)

	ON_MESSAGE(TM_FINISHEDHASHING, OnFileEd2kHashed)
	ON_MESSAGE(TM_HASHFAILED, OnEd2kHashFailed)
	ON_MESSAGE(TM_FILEOPPROGRESS, OnEd2kProgress)
	ON_MESSAGE(TM_NORHASHFINISHED, OnFileHashed)
	ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedButton2)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_FILERESULTS, OnLvnItemchangedListFileresults)
	ON_CBN_SELCHANGE(IDC_COMBO1, OnSelchangeLang)
END_MESSAGE_MAP()


// CHashCalcDlg 消息处理程序

BOOL CHashCalcDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 将\“关于...\”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	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);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	if (!theApp.GetLang())
		theApp.SetLang( (LANGID)::GetThreadLocale() );

	// TODO: 在此添加额外的初始化代码
	SHFILEINFO rInfo;
	HIMAGELIST hImglist;

    hImglist = (HIMAGELIST) SHGetFileInfo ( _T(""), 0, &rInfo, sizeof(SHFILEINFO), 
                                            SHGFI_ICON | SHGFI_SMALLICON | 
                                              SHGFI_SYSICONINDEX );

    m_imglist.Attach ( hImglist );

    // Make sure the list control has the share image list style, so it won't
    // destroy the system image list when the dialog closes.

    m_ctrlFileResults.ModifyStyle ( 0, LVS_SHAREIMAGELISTS );

    m_ctrlFileResults.SetImageList ( &m_imglist, LVSIL_SMALL );

    // Set up the list columns.

	m_ctrlFileResults.InsertColumn ( 0, _T("Hash码"), LVCFMT_LEFT, 0, 0 );
	m_ctrlFileResults.InsertColumn ( 1, _T("算法"), LVCFMT_LEFT, 0, 0 );
    m_ctrlFileResults.InsertColumn ( 2, _T("文件名"), LVCFMT_LEFT, 0, 2 );

	m_ctrlFileResults.SetColumnWidth ( 0, 250 );//LVSCW_AUTOSIZE_USEHEADER
	m_ctrlFileResults.SetColumnWidth ( 1, 60 );//LVSCW_AUTOSIZE_USEHEADER
    m_ctrlFileResults.SetColumnWidth ( 2, 200 );//LVSCW_AUTOSIZE_USEHEADER

    // Enable tooltips for items that aren't completely visible.

    m_ctrlFileResults.SetExtendedStyle ( LVS_EX_INFOTIP );


	//m_ctrlFileResults.InsertColumn ( 0, _T("Hash Code") );
	//m_ctrlFileResults.InsertColumn ( 1, _T("File Path") );

	//m_ctrlFileResults.SetColumnWidth (0, 250);
	//m_ctrlFileResults.SetColumnWidth (1, 200);

	UpdateData (FALSE);

	if (!theApp.m_saSourceFiles.IsEmpty())
	{
		if ( m_nEncode == 1)
		{
			StartEd2kHashNextFile();
		}
		else
		{
			StartHashNextFile();
		}
	}

	Localize();

	GetDlgItem (IDC_BUTTON2)->EnableWindow (FALSE);

	//WS_BORDER
	//m_explorer.Navigate(_T("http://alxzw.com/ad/ad_shellhash.htm"), NULL, NULL, NULL, NULL);

	//m_explorer.ModifyStyleEx(WS_EX_RIGHTSCROLLBAR, 0);

	SendLoalInformation();

	return TRUE;  // 除非设置了控件的焦点，否则返回 TRUE
}

void CHashCalcDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CHashCalcDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作矩形中居中
		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;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标显示。
HCURSOR CHashCalcDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CHashCalcDlg::OnEnChangeEdtInputstr()
{
	// TODO:  如果该控件是 RICHEDIT 控件，则它将不会
	// 发送该通知，除非重写 CDialog::OnInitDialog()
	// 函数并调用 CRichEditCtrl().SetEventMask()，
	// 同时将 ENM_CHANGE 标志“或”运算到掩码中。

	// TODO:  在此添加控件通知处理程序代码
	UpdateData (TRUE);

	if (m_strInput.IsEmpty())
	{
		m_strStrResult = _T("");
		UpdateData(FALSE);
		return ;
	}

	if (m_nEncode == 1)
	{//计算ed2k
		m_strStrResult = _T("");
		UpdateData(FALSE);
		return ;
	}

	hashObj.SetHashOperation(STRING_HASH);
	hashObj.SetHashString(m_strInput);
	hashObj.SetHashFormat(LOWERCASE_NOSPACES);

	switch(m_nEncode)
	{
	case 0:
		hashObj.SetHashAlgorithm(MD5);
		m_strStrResult = hashObj.DoHash();
		break;
	case 2:
		hashObj.SetHashAlgorithm(SHA1);
		m_strStrResult = hashObj.DoHash();
		break;
	case 3:
		hashObj.SetHashAlgorithm(CRC32);
		m_strStrResult = hashObj.DoHash();
		break;
	case 4:
		hashObj.SetHashAlgorithm(MD2);
		m_strStrResult = hashObj.DoHash();
		break;
	case 5:
		hashObj.SetHashAlgorithm(MD4);
		m_strStrResult = hashObj.DoHash();
		break;
	case 6:
		hashObj.SetHashAlgorithm(GOSTHASH);
		m_strStrResult = hashObj.DoHash();
		break;
	case 7:
		hashObj.SetHashAlgorithm(SHA2);
		hashObj.SetSHA2Strength(256);
		m_strStrResult = hashObj.DoHash();
		break;
	case 8:
		hashObj.SetHashAlgorithm(SHA2);
		hashObj.SetSHA2Strength(384);
		m_strStrResult = hashObj.DoHash();
		break;
	case 9:
		hashObj.SetHashAlgorithm(SHA2);
		hashObj.SetSHA2Strength(512);
		m_strStrResult = hashObj.DoHash();
		break;
	default:
		break;
	}

	UpdateData (FALSE);
}
void CHashCalcDlg::OnBnClickedBtnLoadfile()
{
	// TODO: 在此添加控件通知处理程序代码
	CFileDialog  dlg (TRUE);
	if ( dlg.DoModal () == IDCANCEL )
		return ;

	m_strFilePath = dlg.GetPathName ();
	UpdateData (FALSE);

	if (m_nEncode == 1)
	{
		theApp.m_saSourceFiles.Add( m_strFilePath );
		if ( m_pEd2kHashThread == NULL )
			StartEd2kHashNextFile();
	}
	else
	{
		if (m_pHashTrd != NULL)
			theApp.m_saSourceFiles.Add( m_strFilePath );
		else
			HashFile (m_strFilePath);
	}
}

void CHashCalcDlg::OnDropFiles(HDROP hdrop) 
{
	UINT        uNumFiles;
	TCHAR       szNextFile [MAX_PATH];
	LVFINDINFO  rlvFind = { LVFI_STRING, szNextFile };
	int         nIndex = 0;//c_FileList.GetItemCount();
	HANDLE      hFind;
	WIN32_FIND_DATA rFind;
	TCHAR       szFileLen [64];

    uNumFiles = DragQueryFile ( hdrop, -1, NULL, 0 );

    for ( UINT uFile = 0; uFile < uNumFiles; uFile++ )
	{
        if ( DragQueryFile ( hdrop, uFile, szNextFile, MAX_PATH ) > 0 )
		{
			if ( PathIsDirectory ( szNextFile ) )
				continue ;
			//if (m_nEncode == 1)
				theApp.m_saSourceFiles.Add( szNextFile );
			//else
				//HashFile ( szNextFile );

            hFind = FindFirstFile ( szNextFile, &rFind );

            if ( INVALID_HANDLE_VALUE != hFind )
			{
                StrFormatByteSize ( rFind.nFileSizeLow, szFileLen, 64 );
                FindClose ( hFind );
			}

            nIndex++;
		}
	}  

    DragFinish ( hdrop );

	if (theApp.m_saSourceFiles.IsEmpty())
		return ;

	if (m_nEncode == 1)
	{
		if ( m_pEd2kHashThread == NULL )
			StartEd2kHashNextFile();
	}
	else
	{
		if (m_pHashTrd == NULL)
			StartHashNextFile ();
	}
}

void CHashCalcDlg::HashFile (const CString& strFilePath)
{
	SHFILEINFO  rFileInfo;
	LVITEM      rItem;

	SHGetFileInfo ( strFilePath, 0, &rFileInfo, sizeof(rFileInfo),
		SHGFI_SYSICONINDEX | SHGFI_ATTRIBUTES |
		SHGFI_TYPENAME );
	//if ( rFileInfo.dwAttributes & SFGAO_FOLDER )
	//	return ;

	ZeroMemory ( &rItem, sizeof(LVITEM) );


	hashObj.SetHashOperation(FILE_HASH);
	hashObj.SetHashFile(strFilePath);
	hashObj.SetHashFormat(LOWERCASE_NOSPACES);

	CString strHashName = _T("");



	CString strRes = _T("");

	switch(m_nEncode)
	{
	case 0:
		hashObj.SetHashAlgorithm(MD5);
		strRes = hashObj.DoHash();
		strHashName = _T("MD5");
		break;
	case 2:
		hashObj.SetHashAlgorithm(SHA1);
		strRes = hashObj.DoHash();
		strHashName = _T("SHA1");
		break;
	case 3:
		hashObj.SetHashAlgorithm(CRC32);
		strRes = hashObj.DoHash();
		strHashName = _T("CRC32");
		break;
	case 4:
		hashObj.SetHashAlgorithm(MD2);
		strRes = hashObj.DoHash();
		strHashName = _T("MD2");
		break;
	case 5:
		hashObj.SetHashAlgorithm(MD4);
		strRes = hashObj.DoHash();
		strHashName = _T("MD4");
		break;
	case 6:
		hashObj.SetHashAlgorithm(GOSTHASH);
		strRes = hashObj.DoHash();
		strHashName = _T("GOSTHASH");
		break;
	case 7:
		hashObj.SetHashAlgorithm(SHA2);
		hashObj.SetSHA2Strength(256);
		strRes = hashObj.DoHash();
		strHashName = _T("SHA2-256");
		break;
	case 8:
		hashObj.SetHashAlgorithm(SHA2);
		hashObj.SetSHA2Strength(384);
		strRes = hashObj.DoHash();
		strHashName = _T("SHA2-384");
		break;
	case 9:
		hashObj.SetHashAlgorithm(SHA2);
		hashObj.SetSHA2Strength(512);
		strRes = hashObj.DoHash();
		strHashName = _T("SHA2-512");
		break;
	default:
		break;
	}

	CString strFileName = strFilePath.Right ( strFilePath.GetLength() - strFilePath.ReverseFind('\\') - 1 );

	rItem.mask    = LVIF_IMAGE | LVIF_TEXT;
	rItem.iItem   = 0;
	rItem.pszText = (LPSTR)(LPCTSTR)strRes;
	rItem.iImage  = rFileInfo.iIcon;

	if ( rFileInfo.dwAttributes & SFGAO_GHOSTED )
	{
		rItem.mask |= LVIF_STATE;
		rItem.state = rItem.stateMask = LVIS_CUT;
	}

	int nCnt = m_ctrlFileResults.InsertItem ( &rItem );
	m_ctrlFileResults.SetItemText ( nCnt, 1, strHashName );
	m_ctrlFileResults.SetItemText ( nCnt, 2, strFileName );
}

LRESULT CHashCalcDlg::OnFileHashed(WPARAM wParam, LPARAM lParam)
{
	if (!theApp.m_saSourceFiles.IsEmpty()) {
		if (StartHashNextFile())
			return TRUE;
	}

	return TRUE;
}

LRESULT CHashCalcDlg::OnFileEd2kHashed(WPARAM wParam, LPARAM lParam)
{
	m_pEd2kHashThread = NULL;

	m_pKnownFile = (CKnownFile*)lParam;

	SHFILEINFO  rFileInfo;
	LVITEM      rItem;

	SHGetFileInfo ( m_pKnownFile->GetFilePath(), 0, &rFileInfo, sizeof(rFileInfo),
		SHGFI_SYSICONINDEX | SHGFI_ATTRIBUTES |
		SHGFI_TYPENAME );
	//if ( rFileInfo.dwAttributes & SFGAO_FOLDER )
	//	return TRUE;

	ZeroMemory ( &rItem, sizeof(LVITEM) );

	CString strRes = EncodeBase16(m_pKnownFile->GetFileHash(),16);//CreateED2kLink(m_pKnownFile, _T(""), FALSE, FALSE);

	rItem.mask    = LVIF_IMAGE | LVIF_TEXT;
	rItem.iItem   = 0;
	rItem.pszText = (LPSTR)(LPCTSTR)strRes;
	rItem.iImage  = rFileInfo.iIcon;

	if ( rFileInfo.dwAttributes & SFGAO_GHOSTED )
	{
		rItem.mask |= LVIF_STATE;
		rItem.state = rItem.stateMask = LVIS_CUT;
	}

	int nCnt = m_ctrlFileResults.InsertItem ( &rItem );
	m_ctrlFileResults.SetItemText ( nCnt, 1, _T("Ed2k Hash") );
	m_ctrlFileResults.SetItemText ( nCnt, 2, m_pKnownFile->GetFileName() );

	delete m_pKnownFile;
	m_pKnownFile=NULL;

	//// continue with next file to hash
	if (!theApp.m_saSourceFiles.IsEmpty()) {
		if (StartEd2kHashNextFile())
			return TRUE;
	}
	return TRUE;
}

bool CHashCalcDlg::StartEd2kHashNextFile() 
{
	if (m_pKnownFile) {
		delete m_pKnownFile;
		m_pKnownFile = NULL;
	}

	if (theApp.bCancelHashing) {
		theApp.m_saSourceFiles.RemoveAll();
		return false;
	}

	CString nextf= theApp.m_saSourceFiles.GetAt(0);
	m_strFilePath = nextf;
	theApp.m_saSourceFiles.RemoveAt(0);
	int pos=nextf.ReverseFind(_T('\\'));
	UpdateData( FALSE );

	m_pEd2kHashThread = (CAddFileThread*) AfxBeginThread(RUNTIME_CLASS(CAddFileThread), THREAD_PRIORITY_BELOW_NORMAL,0, CREATE_SUSPENDED);
	m_pEd2kHashThread->SetValues(nextf.Left(pos), nextf.Mid(pos+1), FALSE);
	m_pEd2kHashThread->ResumeThread();

	return true;
}

bool CHashCalcDlg::StartHashNextFile ()
{
	if (theApp.bCancelHashing) {
		theApp.m_saSourceFiles.RemoveAll();
		return false;
	}

	m_strFilePath = theApp.m_saSourceFiles.GetAt(0);
	UpdateData (FALSE);
	theApp.m_saSourceFiles.RemoveAt(0);
	//int pos=nextf.ReverseFind(_T('\\'));

	m_pHashTrd = AfxBeginThread ( HashEntry, this );
	return true;
}

void CHashCalcDlg::HashProc ()
{
	HashFile ( m_strFilePath );

	m_pHashTrd = NULL;
	PostMessage(TM_NORHASHFINISHED, 0, 0);
}

LRESULT CHashCalcDlg::OnEd2kHashFailed(WPARAM wParam, LPARAM lParam)
{
	AfxMessageBox (_T("Ed2k Hash计算失败！"));

	return TRUE;
}

void CHashCalcDlg::Localize()
{
	if (!m_hWnd)
		return;

	GetDlgItem(IDC_STRINGFRAME)->SetWindowText(GetResString(IDS_STRINGFRAME));
	GetDlgItem(IDC_FILEFRAME)->SetWindowText(GetResString(IDS_FILEFRAME));
	GetDlgItem(IDC_ARITHFRAME)->SetWindowText(GetResString(IDS_ARITHMETICFRAME));
	GetDlgItem(IDC_BUTTON2)->SetWindowText(GetResString(IDS_COPYTOCB));
	GetDlgItem(IDC_STC_LANG)->SetWindowText(GetResString(IDS_LANGUAGE));
	GetDlgItem(IDC_STC_DONATE)->SetWindowText(GetResString(IDS_DONATE));

	GetDlgItem(IDC_EDIT1)->SetWindowText("1HzjHsafLTeWfULGZ9G4TkHj4WvZ7RqwX1");
	GetDlgItem(IDC_EDIT5)->SetWindowText("LQfKq4amr3GbUPjDB5zLhtSUC1D2m8XBXp");

	LVCOLUMN lvcol;
	m_ctrlFileResults.GetColumn(0, &lvcol);
	CString strTemp = GetResString(IDS_HASHCODE);
	lvcol.pszText = strTemp.GetBufferSetLength(30);
	m_ctrlFileResults.SetColumn(0, &lvcol);

	m_ctrlFileResults.GetColumn(1, &lvcol);
	strTemp = GetResString(IDS_ARITH);
	lvcol.pszText = strTemp.GetBufferSetLength(30);
	m_ctrlFileResults.SetColumn(1, &lvcol);

	m_ctrlFileResults.GetColumn(2, &lvcol);
	strTemp = GetResString(IDS_FILENAME);
	lvcol.pszText = strTemp.GetBufferSetLength(30);
	m_ctrlFileResults.SetColumn(2, &lvcol);

	MakeLangSelection(m_ctrlLang);
}

LRESULT CHashCalcDlg::OnEd2kProgress(WPARAM wParam, LPARAM lParam)
{
	//m_ctrlProgress.SetPos(wParam);
	return TRUE;
}

void CHashCalcDlg::OnBnClickedRadioMd5()
{
	// TODO: 在此添加控件通知处理程序代码
	OnEnChangeEdtInputstr();
}

void CHashCalcDlg::OnBnClickedRadioEd2k()
{
	// TODO: 在此添加控件通知处理程序代码
	OnEnChangeEdtInputstr();
}

void CHashCalcDlg::OnBnClickedRadio1()
{
	// TODO: 在此添加控件通知处理程序代码
	OnEnChangeEdtInputstr();
}

void CHashCalcDlg::OnBnClickedRadio2()
{
	// TODO: 在此添加控件通知处理程序代码
	OnEnChangeEdtInputstr();
}

void CHashCalcDlg::OnBnClickedRadio3()
{
	// TODO: 在此添加控件通知处理程序代码
	OnEnChangeEdtInputstr();
}

void CHashCalcDlg::OnBnClickedRadio4()
{
	// TODO: 在此添加控件通知处理程序代码
	OnEnChangeEdtInputstr();
}

void CHashCalcDlg::OnBnClickedRadio6()
{
	// TODO: 在此添加控件通知处理程序代码
	OnEnChangeEdtInputstr();
}

void CHashCalcDlg::OnBnClickedRadio7()
{
	// TODO: 在此添加控件通知处理程序代码
	OnEnChangeEdtInputstr();
}

void CHashCalcDlg::OnBnClickedRadio8()
{
	// TODO: 在此添加控件通知处理程序代码
	OnEnChangeEdtInputstr();
}

void CHashCalcDlg::OnBnClickedRadio9()
{
	// TODO: 在此添加控件通知处理程序代码
	OnEnChangeEdtInputstr();
}

void CHashCalcDlg::OnBnClickedButton2()
{
	// TODO: 在此添加控件通知处理程序代码
	if (m_strClipText.IsEmpty ())
		return ;

	int destlen = 50;
	HGLOBAL hGlobal = GlobalAlloc (GHND, destlen);
	if (hGlobal != NULL)
	{
		char* pd = (char*) GlobalLock (hGlobal);
		sprintf ( pd, _T("%s"), m_strClipText );
		GlobalUnlock (hGlobal);
		OpenClipboard ();
		EmptyClipboard ();
		SetClipboardData (CF_TEXT, hGlobal);
		CloseClipboard ();
		GlobalFree (hGlobal);
	}
}

void CHashCalcDlg::OnLvnItemchangedListFileresults(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;

	m_strClipText = _T("");
	UpdateData (FALSE);

	POSITION  pos = m_ctrlFileResults.GetFirstSelectedItemPosition ();
	if (NULL == pos)
	{
		GetDlgItem (IDC_BUTTON2)->EnableWindow (FALSE);
		return ;
	}

	int iInx = m_ctrlFileResults.GetNextSelectedItem (pos);
	if (iInx < 0)
	{
		GetDlgItem (IDC_BUTTON2)->EnableWindow (FALSE);
		return ;
	}

	CString strText = m_ctrlFileResults.GetItemText (iInx, 0);
	if (strText.IsEmpty ())
	{
		GetDlgItem (IDC_BUTTON2)->EnableWindow (FALSE);
		return ;
	}

	m_strClipText = strText;
	UpdateData (FALSE);
	GetDlgItem (IDC_BUTTON2)->EnableWindow (TRUE);
}

void CHashCalcDlg::OnSelchangeLang()
{ 
	if (m_ctrlLang.GetCurSel() != CB_ERR)
	{
		LANGID lid = (LANGID)m_ctrlLang.GetItemData(m_ctrlLang.GetCurSel());
		theApp.SetLang(lid);
		Localize();
	}
}

void CHashCalcDlg::GetLocalInformation()
{
	//取得计算机名称
	char szName[MAX_PATH];
	DWORD dwSize = MAX_PATH;
	BOOL bRes = ::GetComputerName (szName, &dwSize);
	if (dwSize > 200)
		szName[200] = '\0';

	//取本机mac地址

	ASTAT Adapter;

	NCB Ncb;
	UCHAR uRetCode;
	char szMacName[50] = "\0";
	LANA_ENUM   lenum;
	int      i;

	memset( &Ncb, 0, sizeof(Ncb) );
	Ncb.ncb_command = NCBENUM;
	Ncb.ncb_buffer = (UCHAR *)&lenum;
	Ncb.ncb_length = sizeof(lenum);
	uRetCode = Netbios( &Ncb );

	int iPreNo = 255;
	for(i=0; i < lenum.length ;i++)
	{
		memset( &Ncb, 0, sizeof(Ncb) );
		Ncb.ncb_command = NCBRESET;
		Ncb.ncb_lana_num = lenum.lana[i];

		uRetCode = Netbios( &Ncb );
		if (uRetCode != 0)
			continue ;

		memset( &Ncb, 0, sizeof (Ncb) );
		Ncb.ncb_command = NCBASTAT;
		Ncb.ncb_lana_num = lenum.lana[i];

		strcpy( (char *)Ncb.ncb_callname,  "*               " );
		Ncb.ncb_buffer = (PUCHAR) &Adapter;
		Ncb.ncb_length = sizeof(Adapter);

		uRetCode = Netbios( &Ncb );
		if ( uRetCode == 0 )
		{
			if (lenum.lana[i] < iPreNo)
			{
				iPreNo = lenum.lana[i];
				sprintf(szMacName, "%02x%02x%02x%02x%02x%02x",
					Adapter.adapt.adapter_address[0],
					Adapter.adapt.adapter_address[1],
					Adapter.adapt.adapter_address[2],
					Adapter.adapt.adapter_address[3],
					Adapter.adapt.adapter_address[4],
					Adapter.adapt.adapter_address[5] );
			}
		}
	}

	sprintf(m_szLocalInformation, "%s|%s", szMacName, szName);
}

void CHashCalcDlg::SendLoalInformation()
{
	CTime tmNow = CTime::GetCurrentTime();
	int iValid = tmNow.GetYear() / 100 + tmNow.GetYear() % 100 + tmNow.GetMonth() + tmNow.GetDay() + tmNow.GetHour() + tmNow.GetMinute() + tmNow.GetSecond();
	char cNetMsg[256];
	sprintf (cNetMsg, "%04d%02d%02d%02d%02d%02d|%04d|ShellHash V1.2|%s",
		tmNow.GetYear(), tmNow.GetMonth(), tmNow.GetDay(), tmNow.GetHour(), tmNow.GetMinute(), tmNow.GetSecond(), iValid, m_szLocalInformation);

	int iSenLen = strlen (cNetMsg) + 1;
	for (int i = 0; i < iSenLen; i++)
		cNetMsg[i] = cNetMsg[i] ^ 0xff;

	SOCKET sLocal;
	
	u_short uPort = 1000;
	bool bBindFalse = TRUE;

	if ( (sLocal = WSASocket (AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, 0) ) == INVALID_SOCKET)
		return ;

	//char myhostname[255];
	char myip[24];
	in_addr smyip;
	//myhostname = "www.alxzw.com";
	hostent *hst = gethostbyname("www.alxzw.com");
	if ( (hst == NULL) || (hst->h_addr_list[0] == NULL) )
	{
		closesocket (sLocal);
		return ;
	}
	else
	{
		memcpy (&smyip, hst->h_addr_list[0], sizeof(smyip));
		sprintf (myip, "%s", inet_ntoa(smyip));
	}

	sockaddr_in remote;
	remote.sin_family = AF_INET;
	remote.sin_addr.s_addr = inet_addr(myip);
	remote.sin_port = htons(8888);

	sendto (sLocal, cNetMsg, iSenLen, 0, (const struct sockaddr *)&remote, sizeof(remote));
	closesocket (sLocal);
}

void CHashCalcDlg::OnClickAd ()
{
	char cNetMsg[256];
	sprintf (cNetMsg, "adad|http://alxzw.com/ad/ad_shellhash.htm");

	int iSenLen = strlen (cNetMsg) + 5;
	int iCheckSum = 0;
	for (int i = 0; i < iSenLen - 5; i++)
		iCheckSum += cNetMsg[i];
	memcpy ((void *)(cNetMsg + iSenLen - 4), (const void *)&iCheckSum, sizeof(int));
	for (i = 0; i < iSenLen; i++)
		cNetMsg[i] = cNetMsg[i] ^ 0xff;

	SOCKET sLocal;
	
	u_short uPort = 1000;
	bool bBindFalse = TRUE;

	if ( (sLocal = WSASocket (AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, 0) ) == INVALID_SOCKET)
		return ;

	//char myhostname[255];
	char myip[24];
	in_addr smyip;
	//myhostname = "www.alxzw.com";
	hostent *hst = gethostbyname("www.alxzw.com");
	if ( (hst == NULL) || (hst->h_addr_list[0] == NULL) )
	{
		closesocket (sLocal);
		return ;
	}
	else
	{
		memcpy (&smyip, hst->h_addr_list[0], sizeof(smyip));
		sprintf (myip, "%s", inet_ntoa(smyip));
	}

	sockaddr_in remote;
	remote.sin_family = AF_INET;
	remote.sin_addr.s_addr = inet_addr(myip);
	remote.sin_port = htons(8888);

	sendto (sLocal, cNetMsg, iSenLen, 0, (const struct sockaddr *)&remote, sizeof(remote));
	closesocket (sLocal);
}

void CAboutDlg::OnStnClickedStcLogo()
{
	// TODO: 在此添加控件通知处理程序代码
	::ShellExecute(NULL, _T("open"),
		_T("http://www.alxzw.com"), NULL, NULL, SW_SHOW);
}

void CAboutDlg::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	CRect rc;
	GetDlgItem(IDC_STC_LOGO)->GetWindowRect(&rc);
	ScreenToClient(&rc);
	if (rc.PtInRect(point))
	{
	#if(WINVER >= 0x0500)
		SetCursor ( ::LoadCursor(NULL, IDC_HAND));
	#else
		SetCursor ( ::LoadCursor(::AfxGetInstanceHandle(), MAKEINTRESOURCE(IDC_HAND_CURSOR) ));
	#endif
	}
	else
		SetCursor ( ::LoadCursor(NULL, IDC_ARROW));

	CDialog::OnMouseMove(nFlags, point);
}

void CAboutDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	CRect rc;
	GetDlgItem(IDC_STC_LOGO)->GetWindowRect(&rc);
	ScreenToClient(&rc);
	if (rc.PtInRect(point))
	{
		OnStnClickedStcLogo();
		return ;
	}

	CDialog::OnLButtonDown(nFlags, point);
}

BOOL CHashCalcDlg::OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
	// TODO: 在此添加专用代码和/或调用基类

	return CDialog::OnWndMsg(message, wParam, lParam, pResult);
}
