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

📄 md5projdlg.cpp

📁 MD5算法的C++版本
💻 CPP
字号:
// MD5ProjDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MD5Proj.h"
#include "MD5ProjDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMD5ProjDlg dialog

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

void CMD5ProjDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMD5ProjDlg)
	DDX_Control(pDX, IDC_PROGRESS, m_Pos);
	DDX_Check(pDX, IDC_CHECK_UPPER, m_nUpper);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMD5ProjDlg, CDialog)
	//{{AFX_MSG_MAP(CMD5ProjDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_CBN_SELCHANGE(IDC_COMBO_TYPE, OnSelchangeComboType)
	ON_EN_CHANGE(IDC_EDIT_DATA, OnChangeEditData)
	ON_BN_CLICKED(IDC_BTN_CALCULATION, OnBtnCalculation)
	ON_BN_CLICKED(IDC_BTN_BROWSE, OnBtnBrowse)
	ON_WM_DROPFILES()
	ON_BN_CLICKED(IDC_CHECK_UPPER, OnCheckUpper)
	ON_BN_CLICKED(IDC_BTN_ABOUT, OnBtnAbout)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMD5ProjDlg message handlers

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

	// 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
	
	((CComboBox*)GetDlgItem(IDC_COMBO_TYPE))->AddString("文件");
	((CComboBox*)GetDlgItem(IDC_COMBO_TYPE))->AddString("字符串");
	((CComboBox*)GetDlgItem(IDC_COMBO_TYPE))->SetCurSel(0);

	AdjustWindow(true);
	
	GetDlgItem(IDC_PROGRESS)->ShowWindow(SW_HIDE);

	m_nUpper = 1;
	UpdateData(false);

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

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

void CMD5ProjDlg::AdjustWindow(bool bFlag)
{
	RECT	rcEdit;
	RECT	rcButton;
	::GetWindowRect(GetDlgItem(IDC_EDIT_RESULT)->m_hWnd, &rcEdit);
	::GetWindowRect(GetDlgItem(IDC_BTN_CALCULATION)->m_hWnd, &rcButton);

	if(!bFlag)
	{
		GetDlgItem(IDC_EDIT_DATA)->SetWindowPos(NULL, 0, 0, 
			(rcEdit.right - rcEdit.left) + (rcButton.right - rcButton.left), 
			rcEdit.bottom - rcEdit.top, 
			SWP_NOMOVE | SWP_NOOWNERZORDER);
		GetDlgItem(IDC_BTN_BROWSE)->ShowWindow(SW_HIDE);
		((CEdit*)GetDlgItem(IDC_EDIT_DATA))->SetReadOnly(false);
		GetDlgItem(IDC_BTN_CALCULATION)->EnableWindow(false);
		::DragAcceptFiles(m_hWnd, false);
	}
	else
	{
		GetDlgItem(IDC_EDIT_DATA)->SetWindowPos(NULL, 0, 0, 
				rcEdit.right - rcEdit.left, 
				rcEdit.bottom - rcEdit.top, 
				SWP_NOMOVE | SWP_NOOWNERZORDER);
		GetDlgItem(IDC_BTN_BROWSE)->ShowWindow(SW_SHOW);
		((CEdit*)GetDlgItem(IDC_EDIT_DATA))->SetReadOnly(true);
		::DragAcceptFiles(m_hWnd, true);
	}
	GetDlgItem(IDC_COMBO_TYPE)->SetWindowPos(NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOSIZE );
}

void CMD5ProjDlg::OnSelchangeComboType() 
{
	if(((CComboBox*)GetDlgItem(IDC_COMBO_TYPE))->GetCurSel() == 0)
		AdjustWindow(true); 
	else if(((CComboBox*)GetDlgItem(IDC_COMBO_TYPE))->GetCurSel() == 1)
		AdjustWindow(false);
	SetDlgItemText(IDC_EDIT_DATA, "");
	SetDlgItemText(IDC_EDIT_RESULT, "");	
}

void CMD5ProjDlg::OnFileProcessing(int nProgress)
{
	m_Pos.SetPos(nProgress);
	if(nProgress == 100)
	{
		GetDlgItem(IDC_PROGRESS)->ShowWindow(SW_HIDE);
		GetDlgItem(IDC_CHECK_UPPER)->ShowWindow(SW_SHOW);
		//GetDlgItem(IDC_BTN_CALCULATION)->EnableWindow(false);
	}
}

void CMD5ProjDlg::OnChangeEditData() 
{
	if(((CComboBox*)GetDlgItem(IDC_COMBO_TYPE))->GetCurSel() == 0)
		return ;

	CString strInput;
	GetDlgItemText(IDC_EDIT_DATA, strInput);

	string strMD5 = m_MD5.MD5String((unsigned char*)(LPCTSTR)strInput, strInput.GetLength(), m_bUpper);
	SetDlgItemText(IDC_EDIT_RESULT, strMD5.c_str());
}

void CMD5ProjDlg::OnBtnCalculation() 
{
	CString strText;
	GetDlgItemText(IDC_BTN_CALCULATION, strText);
	if(strText == "计算(&C)")
	{
		GetDlgItem(IDC_PROGRESS)->ShowWindow(SW_SHOW);
		GetDlgItem(IDC_CHECK_UPPER)->ShowWindow(SW_HIDE);
		SetDlgItemText(IDC_BTN_CALCULATION, "停止(&S)");
		m_Pos.SetPos(0);
		void* p = m_Thunk.thiscall(this, Thunk::GetAddr(&CMD5ProjDlg::ThreadProc));
		HANDLE hThread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)p, 0, 0, 0);
		CloseHandle(hThread);
	}
	else if(strText == "停止(&S)")
	{
		m_MD5.FileTerminate();
		GetDlgItem(IDC_PROGRESS)->ShowWindow(SW_HIDE);
		GetDlgItem(IDC_CHECK_UPPER)->ShowWindow(SW_SHOW);
		SetDlgItemText(IDC_BTN_CALCULATION, "计算(&C)");
	}
	SetDlgItemText(IDC_EDIT_RESULT, "");
}

DWORD CMD5ProjDlg::ThreadProc(LPVOID lpParameter)
{
	if(m_FileName.empty())
		return 0;

	FunctionAddr Addr;
	Addr.SetAddr(this, &CMD5ProjDlg::OnFileProcessing);
	string strMD5 = m_MD5.MD5File(m_FileName.c_str(), m_bUpper, Addr);
	if(!strMD5.empty())
		SetDlgItemText(IDC_EDIT_RESULT, strMD5.c_str());
	else
	{
		SetDlgItemText(IDC_EDIT_RESULT, m_MD5.GetError().c_str());
		GetDlgItem(IDC_PROGRESS)->ShowWindow(SW_HIDE);
	}
	SetDlgItemText(IDC_BTN_CALCULATION, "计算(&C)");

	return 0;
}

void CMD5ProjDlg::OnBtnBrowse() 
{
	SetDlgItemText(IDC_EDIT_DATA, "");
	SetDlgItemText(IDC_EDIT_RESULT, "");
	vector<string> Files = m_FileDlg.GetOpenFileName(m_hWnd);
	if(Files.size() != 0)
	{
		m_FileName = Files[0];
		SetDlgItemText(IDC_EDIT_DATA, m_FileName.c_str());
		GetDlgItem(IDC_BTN_CALCULATION)->EnableWindow(true);
	}
	else
	{
		m_FileName = "";
		SetDlgItemText(IDC_EDIT_DATA, m_FileName.c_str());
		GetDlgItem(IDC_BTN_CALCULATION)->EnableWindow(false);
	}
}

void CMD5ProjDlg::OnCheckUpper() 
{
	UpdateData();
	m_bUpper = m_nUpper == 0 ? false : true;

	CString strInput;
	GetDlgItemText(IDC_EDIT_RESULT, strInput);
	if(!strInput.IsEmpty())
	{
		// 小写
		if(m_bUpper)
			SetDlgItemText(IDC_EDIT_RESULT, CharUpper((LPSTR)(LPCTSTR)strInput));

		// 大写
		else
			SetDlgItemText(IDC_EDIT_RESULT, CharLower((LPSTR)(LPCTSTR)strInput));
	}
}

void CMD5ProjDlg::OnBtnAbout() 
{
	CAboutDlg dlg;
	dlg.DoModal();
}

void CMD5ProjDlg::OnDropFiles(HDROP hDropInfo) 
{
	SetDlgItemText(IDC_EDIT_DATA, "");
	SetDlgItemText(IDC_EDIT_RESULT, "");
	POINT pt;
	::DragQueryPoint(hDropInfo,&pt);
	int filenumber = (int)::DragQueryFile(hDropInfo,(UINT)0xFFFFFFFF,(LPTSTR)NULL,(UINT)NULL);
	char NameBuf[256] = {0};
	::DragQueryFile(hDropInfo, 0, NameBuf, sizeof(NameBuf));
	m_FileName = NameBuf;
	if(!m_FileName.empty())
	{
		SetDlgItemText(IDC_EDIT_DATA, m_FileName.c_str());
		GetDlgItem(IDC_BTN_CALCULATION)->EnableWindow(true);
	}
	CDialog::OnDropFiles(hDropInfo);
}

BOOL CMD5ProjDlg::PreTranslateMessage(MSG* pMsg) 
{ 
	if(pMsg->message == WM_KEYDOWN)   
	{   
		if(pMsg->wParam == VK_RETURN)   
		{   
			//return   FALSE;	//对话框内部控件可以接收到回车消息
			return   TRUE;//对话框内部控件不可以接收到回车消息
		}
	}
	return CDialog::PreTranslateMessage(pMsg);
}

⌨️ 快捷键说明

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