您当前的位置:首页 > 计算机 > 编程开发 > VC/VC++

MFC 简单的MD5计算器

时间:03-17来源:作者:点击数:

一、简述

记--使用开源MD5计算代码+MFC实现简单的文件MD5计算器。

1、支持拖拽文件或目录。

2、支持拖拽多个文件或目录。

3、支持比较两个MD5值,不一致时指出第几个字符不一致。

4、支持输出文件全路径或仅文件名。

5、支持输出文件大小。

6、支持指定文件后缀。

例子打包:外链:https://wwi.lanzouq.com/b0c9zap6d密码:d49u

C语言开源MD5代码:https://github.com/chinaran/Compute-file-or-string-md5

二、效果

三、代码工程

四、源文件

MD5CalculatorDlg.h文件


// MD5CalculatorDlg.h : 头文件
//

#pragma once
#include "afxwin.h"

#
#define FILE_EXTENSION_MAX_LEN	6	//限制文件后缀长度

typedef struct {
	int isFullPath;//标识是否是全路径,1:全路径;0:仅文件名
	int isOutPutFileSize;//标识是否需要输出文件大小 1:输出,0:不输出
	CString fileExtension;//文件后缀
} OutputParam_t;

// CMD5CalculatorDlg 对话框
class CMD5CalculatorDlg : public CDialogEx
{
// 构造
public:
	CMD5CalculatorDlg(CWnd* pParent = NULL);	// 标准构造函数

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_MD5CALCULATOR_DIALOG };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV 支持
private:
	int ProcessOneFile(OutputParam_t &outputParam, int &index, const char* filePath, int filePathLen, CString &resultStr);
	void ProcessDir(OutputParam_t &outputParam, int &index, CString path, CString &resultStr);
	void GetOutputParam(OutputParam_t &outputParam);

// 实现
protected:
	HICON m_hIcon;
	void OnOK();
	// 生成的消息映射函数
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	DECLARE_MESSAGE_MAP()
public:
	CString mEditMD5Str;//MD5值
	CString mEditMD5ResultStr;//MD5计算结果
	CButton mCheckIsFullPath;//是否显示文件全路径
	CString mEditFileExtensionStr;//文件后缀,当为空时全部符合
	CButton mCheckOutPutFileSize;//结果中是否输出文件大小
	CString mEditCompareMD5Str;//要对比的MD5值
	CButton mBtnCompareMD5;//比较按钮
	afx_msg void OnBnClickedButtonCopymd5();	
	afx_msg void OnDropFiles(HDROP hDropInfo);	
	afx_msg void OnBnClickedButtonComparemd5();
};

MD5CalculatorDlg.cpp文件

#define _CRT_SECURE_NO_WARNINGS	//去除安全警告
// MD5CalculatorDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "MD5Calculator.h"
#include "MD5CalculatorDlg.h"
#include "afxdialogex.h"
#include "common.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

//使用说明信息
#define USEAGE_INFO	_T("1、拖拽文件或目录到该窗口内,即可计算MD5值。\r\n" \
					   "2、支持拖拽多个文件或目录。\r\n"\
					   "3、可比较两个MD5值,区分大小写。\r\n"\
					   "4、勾选\"文件名全路径\"则输出文件全路径。\r\n"\
					   "5、勾选\"文件大小\"则输出文件大小。\r\n"\
					   "6、可指定要计算MD5的文件后缀,未指定则无限制。")

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

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

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

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

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

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

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CMD5CalculatorDlg 对话框



CMD5CalculatorDlg::CMD5CalculatorDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(IDD_MD5CALCULATOR_DIALOG, pParent)
	, mEditMD5Str(_T(""))
	, mEditMD5ResultStr(_T(""))
	, mEditFileExtensionStr(_T(""))
	, mEditCompareMD5Str(_T(""))
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMD5CalculatorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_MD5, mEditMD5Str);
	DDV_MaxChars(pDX, mEditMD5Str, MD5_MAX_LEN);
	DDX_Text(pDX, IDC_EDIT_Result, mEditMD5ResultStr);
	DDX_Control(pDX, IDC_CHECK_FullPath, mCheckIsFullPath);
	DDX_Text(pDX, IDC_EDIT_FileExtension, mEditFileExtensionStr);
	DDV_MaxChars(pDX, mEditFileExtensionStr, FILE_EXTENSION_MAX_LEN);
	DDX_Control(pDX, IDC_CHECK_OutPutFileSize, mCheckOutPutFileSize);
	DDX_Text(pDX, IDC_EDIT_CompareMD5, mEditCompareMD5Str);
	DDV_MaxChars(pDX, mEditCompareMD5Str, MD5_MAX_LEN);
	DDX_Control(pDX, IDC_BUTTON_CompareMD5, mBtnCompareMD5);
}



BEGIN_MESSAGE_MAP(CMD5CalculatorDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_CopyMD5, &CMD5CalculatorDlg::OnBnClickedButtonCopymd5)
	ON_WM_DROPFILES()
	ON_BN_CLICKED(IDC_BUTTON_CompareMD5, &CMD5CalculatorDlg::OnBnClickedButtonComparemd5)
END_MESSAGE_MAP()


// CMD5CalculatorDlg 消息处理程序

//重载回车,不然会退出
void CMD5CalculatorDlg::OnOK()
{
	// TODO: 在此添加专用代码和/或调用基类

	//CDialogEx::OnOK();
}

BOOL CMD5CalculatorDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

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

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	mEditMD5ResultStr = USEAGE_INFO;
	UpdateData(false);
	//SetWindowPos(NULL, 0, 0, 586, 380, SWP_NOMOVE);//设置窗口宽高但不移动位置

	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

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

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

void CMD5CalculatorDlg::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
	{
		CDialogEx::OnPaint();
	}
}

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


//复制MD5值到粘贴板
void CMD5CalculatorDlg::OnBnClickedButtonCopymd5()
{
	// TODO: 在此添加控件通知处理程序代码
	if (OpenClipboard())
	{
		UpdateData(true);
		HGLOBAL clipbuffer;
		char* buffer = NULL;
		EmptyClipboard();
		clipbuffer = GlobalAlloc(GMEM_DDESHARE, mEditMD5Str.GetLength() + 1);
		buffer = (char*)GlobalLock(clipbuffer);
		strcpy_s(buffer, mEditMD5Str.GetLength() + 1, (LPSTR)(LPCTSTR)mEditMD5Str);
		GlobalUnlock(clipbuffer);
		SetClipboardData(CF_TEXT, clipbuffer);
		CloseClipboard();
	}
}

//处理拖拽文件
void CMD5CalculatorDlg::OnDropFiles(HDROP hDropInfo)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	UINT count;
	int pathLen;
	TCHAR filePath[MAX_PATH];
	CString resultStr = "";
	int fileIndex = 1;

	OutputParam_t outputParam = { 0 };
	GetOutputParam(outputParam);
	
	count = DragQueryFile(hDropInfo, 0xFFFFFFFF, NULL, 0);//从成功的拖放操作中检索文件的名称。并取代被拖拽文件的数目

	for (UINT i = 0; i < count; i++) {
		memset(filePath, 0x0, sizeof(filePath));
		pathLen = DragQueryFile(hDropInfo, i, filePath, sizeof(filePath));
		//TRACE(_T("i:%d, filePath:%s\n"), i, filePath);		
		if (!IsDir(filePath)) {
			if (YES == ProcessOneFile(outputParam, fileIndex, filePath, pathLen, resultStr)) {
				fileIndex++;
			}			
		} else {
			//递归目录
			ProcessDir(outputParam, fileIndex, CString(filePath), resultStr);
		}
	}
	mEditMD5ResultStr = resultStr;

	UpdateData(false);//将关联变量值更新到控件
	CDialogEx::OnDropFiles(hDropInfo);
}

//处理一个文件的MD5值
int CMD5CalculatorDlg::ProcessOneFile(OutputParam_t &outputParam, int &index, const char *filePath, int filePathLen, CString & resultStr)
{
	char md5[MD5_MAX_LEN] = { 0 };

	CString fileExtension = GetFileExtension(CString(filePath));
	if (!outputParam.fileExtension.IsEmpty()
		&& 0 != outputParam.fileExtension.CompareNoCase(fileExtension)) {
		//不是指定的后缀直接返回
		return NO;
	}
	
	CFileStatus fileStatus = {0};
	if (YES == outputParam.isOutPutFileSize) {
		if (CFile::GetStatus(filePath, fileStatus)) {
			;//OK
		}
	}

	GetFileMD5(filePath, md5, sizeof(md5));

	if (YES == outputParam.isFullPath) {//全路径
		resultStr.AppendFormat(_T("[%d] MD5:%s; FILE:%s"),
			index, md5, filePath);
	} else {		
		resultStr.AppendFormat(_T("[%d] MD5:%s; FILE:%s"),
			index, md5, GetFileName(filePath, filePathLen));
	}

	if (YES == outputParam.isOutPutFileSize) {
		resultStr.AppendFormat(_T("; SIZE:%llu"), fileStatus.m_size);
	}
	resultStr.AppendFormat(_T("\r\n"));

	mEditCompareMD5Str = mEditMD5Str;
	mEditMD5Str = CString(md5);	

	return YES;
}

//递归处理目录
void CMD5CalculatorDlg::ProcessDir(OutputParam_t &outputParam, int &index, CString path, CString &resultStr)
{
	LPTSTR lpszPath = new TCHAR[path.GetLength() + 1];
	_tcscpy(lpszPath, path);
	if (!IsDir(lpszPath)) {
		return;
	}

	// 查找当前路径下的所有文件夹和文件
	CString strDir = path + _T("\\*.*");;
	CString strFullPath;
	//CString strFileName;

	// 遍历得到所有子文件夹名
	CFileFind finder;
	BOOL bWorking = finder.FindFile(strDir);

	while (bWorking) {
		bWorking = finder.FindNextFile();
		if (0 == finder.GetFileName().Compare(".")
			|| 0 == finder.GetFileName().Compare("..")) 
		{//排除“.”“..”,当前目录和上层目录
			continue;
		}

		if (finder.IsDirectory() ) {
			//递归调用
			ProcessDir(outputParam, index, finder.GetFilePath(), resultStr);
		} else {
			strFullPath = finder.GetFilePath();
			//strFileName = finder.GetFileName();
			if (YES == ProcessOneFile(outputParam, index, (char*)(LPCTSTR)strFullPath, strFullPath.GetLength(), resultStr)) {
				index++;
			}			
		}
	}
	finder.Close();
}

//获取输出参数
void CMD5CalculatorDlg::GetOutputParam(OutputParam_t & outputParam)
{
	UpdateData(true);//控件值更新到关联变量
	outputParam.isFullPath = mCheckIsFullPath.GetCheck();
	outputParam.isOutPutFileSize = mCheckOutPutFileSize.GetCheck();
	outputParam.fileExtension = mEditFileExtensionStr;
}

//比较MD5值
void CMD5CalculatorDlg::OnBnClickedButtonComparemd5()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(true);
	if (mEditMD5Str.IsEmpty() && mEditCompareMD5Str.IsEmpty()) {
		return;
	}

#if 0
	if (0 == mEditMD5Str.Compare(mEditCompareMD5Str)) {
		MessageBox(_T("MD5值一致!"), _T("比较结果"), MB_ICONASTERISK);
	} else {
		MessageBox(_T("MD5值不一致!"), _T("比较结果"), MB_ICONHAND);
	}
#else
	int i;
	int len1 = mEditMD5Str.GetLength();
	int len2 = mEditCompareMD5Str.GetLength();
	int minLen = (len1 < len2) ? len1 : len2;
	if (0 == minLen) {
		MessageBox(_T("MD5值不一致!\r\n其中一个为空!"), _T("比较结果"), MB_ICONHAND);
		return;
	}

	for (i = 0; i < minLen; ++i) {
		if (mEditMD5Str[i] != mEditCompareMD5Str[i]) {
			break;
		}
	}

	TCHAR tmp_buf[128] = { 0 };
	if (i == minLen) {
		if (len1 == len2) {
			MessageBox(_T("MD5值一致!"), _T("比较结果"), MB_ICONASTERISK);
		} else {
			snprintf(tmp_buf, sizeof(tmp_buf) - 1, \
				"MMD5值不一致!\r\n前面的%d个字符相同,但长度不等: '%d' != '%d'", minLen, len1, len2);
			MessageBox(_T(tmp_buf), _T("比较结果"), MB_ICONHAND);
		}
	} else {		
		snprintf(tmp_buf, sizeof(tmp_buf) - 1, \
			"MD5值不一致!\r\n第%d个字符不一致: '%c' != '%c'", i + 1, mEditMD5Str[i], mEditCompareMD5Str[i]);
		MessageBox(_T(tmp_buf), _T("比较结果"), MB_ICONHAND);
	}
#endif
}

md5.h文件

#pragma once
#ifndef MD5_H
#define MD5_H

typedef struct
{
	unsigned int count[2];
	unsigned int state[4];
	unsigned char buffer[64];
} MD5_CTX;


#define F(x,y,z) ((x & y) | (~x & z))
#define G(x,y,z) ((x & z) | (y & ~z))
#define H(x,y,z) (x^y^z)
#define I(x,y,z) (y ^ (x | ~z))
#define ROTATE_LEFT(x,n) ((x << n) | (x >> (32-n)))

#define FF(a,b,c,d,x,s,ac) \
{ \
	a += F(b,c,d) + x + ac; \
	a = ROTATE_LEFT(a,s); \
	a += b; \
}
#define GG(a,b,c,d,x,s,ac) \
{ \
	a += G(b,c,d) + x + ac; \
	a = ROTATE_LEFT(a,s); \
	a += b; \
}
#define HH(a,b,c,d,x,s,ac) \
{ \
	a += H(b,c,d) + x + ac; \
	a = ROTATE_LEFT(a,s); \
	a += b; \
}
#define II(a,b,c,d,x,s,ac) \
{ \
	a += I(b,c,d) + x + ac; \
	a = ROTATE_LEFT(a,s); \
	a += b; \
}                                            
void MD5Init(MD5_CTX *context);
void MD5Update(MD5_CTX *context, unsigned char *input, unsigned int inputlen);
void MD5Final(MD5_CTX *context, unsigned char digest[16]);
void MD5Transform(unsigned int state[4], unsigned char block[64]);
void MD5Encode(unsigned char *output, unsigned int *input, unsigned int len);
void MD5Decode(unsigned int *output, unsigned char *input, unsigned int len);

#endif

md5.cpp文件

#include "stdafx.h"
#include "md5.h"
#include <memory.h>

unsigned char PADDING[] =
{
	0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};

void MD5Init(MD5_CTX *context)
{
	context->count[0] = 0;
	context->count[1] = 0;
	context->state[0] = 0x67452301;
	context->state[1] = 0xEFCDAB89;
	context->state[2] = 0x98BADCFE;
	context->state[3] = 0x10325476;
}

void MD5Update(MD5_CTX *context, unsigned char *input, unsigned int inputlen)
{
	unsigned int i = 0;
	unsigned int index = 0;
	unsigned int partlen = 0;

	index = (context->count[0] >> 3) & 0x3F;
	partlen = 64 - index;
	context->count[0] += inputlen << 3;

	if (context->count[0] < (inputlen << 3))
		context->count[1]++;
	context->count[1] += inputlen >> 29;

	if (inputlen >= partlen)
	{
		memcpy(&context->buffer[index], input, partlen);
		MD5Transform(context->state, context->buffer);

		for (i = partlen; i + 64 <= inputlen; i += 64)
			MD5Transform(context->state, &input[i]);

		index = 0;
	}
	else
	{
		i = 0;
	}
	memcpy(&context->buffer[index], &input[i], inputlen - i);
}

void MD5Final(MD5_CTX *context, unsigned char digest[16])
{
	unsigned int index = 0, padlen = 0;
	unsigned char bits[8];

	index = (context->count[0] >> 3) & 0x3F;
	padlen = (index < 56) ? (56 - index) : (120 - index);
	MD5Encode(bits, context->count, 8);
	MD5Update(context, PADDING, padlen);
	MD5Update(context, bits, 8);
	MD5Encode(digest, context->state, 16);
}

void MD5Encode(unsigned char *output, unsigned int *input, unsigned int len)
{
	unsigned int i = 0;
	unsigned int j = 0;

	while (j < len)
	{
		output[j] = input[i] & 0xFF;
		output[j + 1] = (input[i] >> 8) & 0xFF;
		output[j + 2] = (input[i] >> 16) & 0xFF;
		output[j + 3] = (input[i] >> 24) & 0xFF;
		i++;
		j += 4;
	}
}

void MD5Decode(unsigned int *output, unsigned char *input, unsigned int len)
{
	unsigned int i = 0;
	unsigned int j = 0;

	while (j < len)
	{
		output[i] = (input[j]) |
			(input[j + 1] << 8) |
			(input[j + 2] << 16) |
			(input[j + 3] << 24);
		i++;
		j += 4;
	}
}

void MD5Transform(unsigned int state[4], unsigned char block[64])
{
	unsigned int a = state[0];
	unsigned int b = state[1];
	unsigned int c = state[2];
	unsigned int d = state[3];
	unsigned int x[64];

	MD5Decode(x, block, 64);

	FF(a, b, c, d, x[0], 7, 0xd76aa478); /* 1 */
	FF(d, a, b, c, x[1], 12, 0xe8c7b756); /* 2 */
	FF(c, d, a, b, x[2], 17, 0x242070db); /* 3 */
	FF(b, c, d, a, x[3], 22, 0xc1bdceee); /* 4 */
	FF(a, b, c, d, x[4], 7, 0xf57c0faf); /* 5 */
	FF(d, a, b, c, x[5], 12, 0x4787c62a); /* 6 */
	FF(c, d, a, b, x[6], 17, 0xa8304613); /* 7 */
	FF(b, c, d, a, x[7], 22, 0xfd469501); /* 8 */
	FF(a, b, c, d, x[8], 7, 0x698098d8); /* 9 */
	FF(d, a, b, c, x[9], 12, 0x8b44f7af); /* 10 */
	FF(c, d, a, b, x[10], 17, 0xffff5bb1); /* 11 */
	FF(b, c, d, a, x[11], 22, 0x895cd7be); /* 12 */
	FF(a, b, c, d, x[12], 7, 0x6b901122); /* 13 */
	FF(d, a, b, c, x[13], 12, 0xfd987193); /* 14 */
	FF(c, d, a, b, x[14], 17, 0xa679438e); /* 15 */
	FF(b, c, d, a, x[15], 22, 0x49b40821); /* 16 */

										   /* Round 2 */
	GG(a, b, c, d, x[1], 5, 0xf61e2562); /* 17 */
	GG(d, a, b, c, x[6], 9, 0xc040b340); /* 18 */
	GG(c, d, a, b, x[11], 14, 0x265e5a51); /* 19 */
	GG(b, c, d, a, x[0], 20, 0xe9b6c7aa); /* 20 */
	GG(a, b, c, d, x[5], 5, 0xd62f105d); /* 21 */
	GG(d, a, b, c, x[10], 9, 0x2441453); /* 22 */
	GG(c, d, a, b, x[15], 14, 0xd8a1e681); /* 23 */
	GG(b, c, d, a, x[4], 20, 0xe7d3fbc8); /* 24 */
	GG(a, b, c, d, x[9], 5, 0x21e1cde6); /* 25 */
	GG(d, a, b, c, x[14], 9, 0xc33707d6); /* 26 */
	GG(c, d, a, b, x[3], 14, 0xf4d50d87); /* 27 */
	GG(b, c, d, a, x[8], 20, 0x455a14ed); /* 28 */
	GG(a, b, c, d, x[13], 5, 0xa9e3e905); /* 29 */
	GG(d, a, b, c, x[2], 9, 0xfcefa3f8); /* 30 */
	GG(c, d, a, b, x[7], 14, 0x676f02d9); /* 31 */
	GG(b, c, d, a, x[12], 20, 0x8d2a4c8a); /* 32 */

										   /* Round 3 */
	HH(a, b, c, d, x[5], 4, 0xfffa3942); /* 33 */
	HH(d, a, b, c, x[8], 11, 0x8771f681); /* 34 */
	HH(c, d, a, b, x[11], 16, 0x6d9d6122); /* 35 */
	HH(b, c, d, a, x[14], 23, 0xfde5380c); /* 36 */
	HH(a, b, c, d, x[1], 4, 0xa4beea44); /* 37 */
	HH(d, a, b, c, x[4], 11, 0x4bdecfa9); /* 38 */
	HH(c, d, a, b, x[7], 16, 0xf6bb4b60); /* 39 */
	HH(b, c, d, a, x[10], 23, 0xbebfbc70); /* 40 */
	HH(a, b, c, d, x[13], 4, 0x289b7ec6); /* 41 */
	HH(d, a, b, c, x[0], 11, 0xeaa127fa); /* 42 */
	HH(c, d, a, b, x[3], 16, 0xd4ef3085); /* 43 */
	HH(b, c, d, a, x[6], 23, 0x4881d05); /* 44 */
	HH(a, b, c, d, x[9], 4, 0xd9d4d039); /* 45 */
	HH(d, a, b, c, x[12], 11, 0xe6db99e5); /* 46 */
	HH(c, d, a, b, x[15], 16, 0x1fa27cf8); /* 47 */
	HH(b, c, d, a, x[2], 23, 0xc4ac5665); /* 48 */

										  /* Round 4 */
	II(a, b, c, d, x[0], 6, 0xf4292244); /* 49 */
	II(d, a, b, c, x[7], 10, 0x432aff97); /* 50 */
	II(c, d, a, b, x[14], 15, 0xab9423a7); /* 51 */
	II(b, c, d, a, x[5], 21, 0xfc93a039); /* 52 */
	II(a, b, c, d, x[12], 6, 0x655b59c3); /* 53 */
	II(d, a, b, c, x[3], 10, 0x8f0ccc92); /* 54 */
	II(c, d, a, b, x[10], 15, 0xffeff47d); /* 55 */
	II(b, c, d, a, x[1], 21, 0x85845dd1); /* 56 */
	II(a, b, c, d, x[8], 6, 0x6fa87e4f); /* 57 */
	II(d, a, b, c, x[15], 10, 0xfe2ce6e0); /* 58 */
	II(c, d, a, b, x[6], 15, 0xa3014314); /* 59 */
	II(b, c, d, a, x[13], 21, 0x4e0811a1); /* 60 */
	II(a, b, c, d, x[4], 6, 0xf7537e82); /* 61 */
	II(d, a, b, c, x[11], 10, 0xbd3af235); /* 62 */
	II(c, d, a, b, x[2], 15, 0x2ad7d2bb); /* 63 */
	II(b, c, d, a, x[9], 21, 0xeb86d391); /* 64 */
	state[0] += a;
	state[1] += b;
	state[2] += c;
	state[3] += d;
}

五、总结

5.1文件拖拽

添加ON_WM_DROPFILES消息处理:OnDropFiles()

窗口Accept file属性设置为true

5.2窗口屏蔽回车关闭(默认在窗口、Edit控件按回车回关闭窗口)

edit、wantreturn属性设置为true

重写窗口onOK处理

5.3 复制内容到粘贴板,

    CString MD5String = "abc123";
    if (OpenClipboard())
	{
		UpdateData(true);
		HGLOBAL clipbuffer;
		char* buffer = NULL;
		EmptyClipboard();
		clipbuffer = GlobalAlloc(GMEM_DDESHARE, MD5String.GetLength() + 1);
		buffer = (char*)GlobalLock(clipbuffer);
		strcpy_s(buffer, MD5String.GetLength() + 1, (LPSTR)(LPCTSTR)MD5String);
		GlobalUnlock(clipbuffer);
		SetClipboardData(CF_TEXT, clipbuffer);
		CloseClipboard();
	}

粘贴出来只有一个字节时可右键项目--属性--设置字符集为使用多字节字符集

5.4 获取/设置控件位置

//获取相对于父窗口位置
CRect rect;
GetDlgItem(IDC_STATIC_FileType)->GetWindowRect(&rect);
ScreenToClient(&rect);//转换为相对于父窗口的位置
int x = rect.left;
int y = rect.top;

//设置子控件的位置(相对于父窗口位置)
	
//将文件后缀移动台"复制MD5值"右边    IDC_STATIC_FileType是控件ID
	GetDlgItem(IDC_STATIC_FileType)->SetWindowPos(NULL, 440, 23, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
	GetDlgItem(IDC_EDIT_FileExtension)->SetWindowPos(NULL, 512, 18, 0, 0, SWP_NOZORDER | SWP_NOSIZE);

//设置主窗口大小但不移动位置
SetWindowPos(NULL, 0, 0, 586, 380, SWP_NOMOVE);//设置窗口宽高但不移动位置

SWP_NOMOVE:忽略x、y,维持位置不变;
SWP_NOSIZE:忽略cx、cy,维持大小不变;

==============================修改版==============================

例子打包:外链:https://wwi.lanzouq.com/b0c9zap6d密码:d49u

修改点:

1、默认不清空结果,方便比较多个文件的md5值。

2、结果信息输出后自动滚动到底部,方便查看最新的文件MD5及文件个数。

3、将文件后缀设置移到右边隐藏区,放大窗口可见。

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门