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

📄 mybase64dlg.cpp

📁 Base64算法
💻 CPP
字号:
// MyBase64Dlg.cpp : implementation file
//

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

/////////////////////////////////////////////////////////////////////////////
// CMyBase64Dlg dialog

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

void CMyBase64Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyBase64Dlg)
	DDX_Radio(pDX, IDC_RADIO_FILE, m_DataType);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyBase64Dlg, CDialog)
	//{{AFX_MSG_MAP(CMyBase64Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_SRCFILE, OnBtnSrcfile)
	ON_BN_CLICKED(IDC_BTN_DSTFILE, OnBtnDstfile)
	ON_BN_CLICKED(IDC_BTN_ENCODE, OnBtnEncode)
	ON_BN_CLICKED(IDC_BTN_DECODE, OnBtnDecode)
	ON_BN_CLICKED(IDC_BTN_ABOUT, OnBtnAbout)
	ON_BN_CLICKED(IDC_RADIO_FILE, OnRadioFile)
	ON_BN_CLICKED(IDC_RADIO_STRING, OnRadioString)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyBase64Dlg message handlers

BOOL CMyBase64Dlg::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

	m_DataType=0;
	UpdateData(false);
	OnRadioFile();

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

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

/*********************************
选择输入文件
*********************************/
void CMyBase64Dlg::OnBtnSrcfile() 
{
	ZFileDialog	FileDlg;
	deque<string> dqTmp=FileDlg.GetOpenFileName(m_hWnd,false,"所有文件 (*.*)\0*.*\0\0");
	if(dqTmp.size()==0) return;
	strInFile=dqTmp[0];
	SetDlgItemText(IDC_EDIT_SRCFILE,strInFile.c_str());
}

/*********************************
选择输出文件
*********************************/
void CMyBase64Dlg::OnBtnDstfile() 
{
	ZFileDialog	FileDlg;
	strOutFile=FileDlg.GetSaveFileName(m_hWnd,true,"所有文件 (*.*)\0*.*\0\0");
	if(strOutFile.empty()) return;
	SetDlgItemText(IDC_EDIT_DSTFILE,strOutFile.c_str());	
}

/*********************************
编码
*********************************/
void CMyBase64Dlg::OnBtnEncode() 
{
	UpdateData();
	ZBase64 base64;
	if(m_DataType==0)
	{
		if(strInFile.empty())
		{
			AfxMessageBox("请指定输入文件!");
			return;
		}
		if(strOutFile.empty())
		{
			AfxMessageBox("请指定输出文件!");
			return;
		}

		FILE* fin=fopen(strInFile.c_str(),"rb");
		if(fin==NULL)
		{
			AfxMessageBox("输入文件打开失败!");
			return;
		}
		FILE* fout=fopen(strOutFile.c_str(),"wb+");
		if(fout==NULL)
		{
			AfxMessageBox("输出文件打开失败!");
			return;
		}

		char* Buf=new char[BUFFER_READ+1];
		memset(Buf,0,BUFFER_READ+1);
		int ReadSize=fread(Buf,1,BUFFER_READ,fin);
		while(ReadSize==BUFFER_READ)
		{
			string strTmpResult=base64.Encode((unsigned char*)Buf,ReadSize);
			fwrite(strTmpResult.c_str(),1,strTmpResult.length(),fout);
			memset(Buf,0,BUFFER_READ+1);
			ReadSize=fread(Buf,1,BUFFER_READ,fin);
		}
		if(ReadSize>0)
		{
			string strTmpResult=base64.Encode((unsigned char*)Buf,ReadSize);
			fwrite(strTmpResult.c_str(),1,strTmpResult.length(),fout);
		}
		delete[] Buf;
		fclose(fin);
		fclose(fout);
		AfxMessageBox("文件编码成功!");
	}
	else if(m_DataType==1)
	{
		CString strTmp;
		GetDlgItemText(IDC_EDIT_SRCSTRING,strTmp);
		if(strTmp.IsEmpty()) return;
		string strResult=base64.Encode((unsigned char*)(LPCTSTR)strTmp,strTmp.GetLength());
		SetDlgItemText(IDC_EDIT_DSTSTRING,strResult.c_str());
	}
}

/*********************************
解码
*********************************/
void CMyBase64Dlg::OnBtnDecode() 
{
	UpdateData();
	ZBase64 base64;
	if(m_DataType==0)
	{
		if(strInFile.empty())
		{
			AfxMessageBox("请指定输入文件!");
			return;
		}
		if(strOutFile.empty())
		{
			AfxMessageBox("请指定输出文件!");
			return;
		}

		FILE* fin=fopen(strInFile.c_str(),"rb");
		if(fin==NULL)
		{
			AfxMessageBox("输入文件打开失败!");
			return;
		}
		FILE* fout=fopen(strOutFile.c_str(),"wb+");
		if(fout==NULL)
		{
			AfxMessageBox("输出文件打开失败!");
			return;
		}
		//int size=1024*1024;
		char* Buf=new char[BUFFER_WRITE+1];
		memset(Buf,0,BUFFER_WRITE+1);
		int ReadSize=fread(Buf,1,BUFFER_WRITE,fin);
		while(ReadSize==BUFFER_WRITE)
		{
			int OutByte=0;
			string strTmpResult=base64.Decode(Buf,ReadSize,OutByte);
			fwrite(strTmpResult.c_str(),1,OutByte,fout);
			memset(Buf,0,BUFFER_WRITE+1);
			ReadSize=fread(Buf,1,BUFFER_WRITE,fin);
		}
		if(ReadSize>0)
		{
			int OutByte=0;
			string strTmpResult=base64.Decode(Buf,ReadSize,OutByte);
			fwrite(strTmpResult.c_str(),1,OutByte,fout);
		}
		delete[] Buf;
		fclose(fin);
		fclose(fout);
		AfxMessageBox("文件解码成功!");
	}
	else if(m_DataType==1)
	{
		CString strTmp;
		GetDlgItemText(IDC_EDIT_SRCSTRING,strTmp);
		if(strTmp.IsEmpty()) return;
		int OutByte=0;
		string strResult=base64.Decode((LPSTR)(LPCTSTR)strTmp,strTmp.GetLength(),OutByte);
		SetDlgItemText(IDC_EDIT_DSTSTRING,strResult.c_str());
	}	
}

/*********************************
关于
*********************************/
void CMyBase64Dlg::OnBtnAbout() 
{
	CAboutDlg dlg;
	dlg.DoModal();
}

/*********************************
点击"文件"单选按钮
*********************************/
void CMyBase64Dlg::OnRadioFile() 
{
	UpdateData();
	if(m_DataType==0)	
	{
		::EnableWindow(::GetDlgItem(m_hWnd,IDC_BTN_SRCFILE),true);
		::EnableWindow(::GetDlgItem(m_hWnd,IDC_BTN_DSTFILE),true);
		::EnableWindow(::GetDlgItem(m_hWnd,IDC_EDIT_SRCSTRING),false);
	}
	else if(m_DataType==1)
	{
		::EnableWindow(::GetDlgItem(m_hWnd,IDC_BTN_SRCFILE),false);
		::EnableWindow(::GetDlgItem(m_hWnd,IDC_BTN_DSTFILE),false);
		::EnableWindow(::GetDlgItem(m_hWnd,IDC_EDIT_SRCSTRING),true);
	}
	SetDlgItemText(IDC_EDIT_SRCSTRING,"");
	SetDlgItemText(IDC_EDIT_DSTSTRING,"");
	SetDlgItemText(IDC_EDIT_DSTFILE,"");
	SetDlgItemText(IDC_EDIT_SRCFILE,"");
	strInFile="";
	strOutFile="";
}

/*********************************
点击"字符串"单选按钮
*********************************/
void CMyBase64Dlg::OnRadioString() 
{
	UpdateData();
	if(m_DataType==0)	
	{
		::EnableWindow(::GetDlgItem(m_hWnd,IDC_BTN_SRCFILE),true);
		::EnableWindow(::GetDlgItem(m_hWnd,IDC_BTN_DSTFILE),true);
		::EnableWindow(::GetDlgItem(m_hWnd,IDC_EDIT_SRCSTRING),false);
	}
	else if(m_DataType==1)
	{
		::EnableWindow(::GetDlgItem(m_hWnd,IDC_BTN_SRCFILE),false);
		::EnableWindow(::GetDlgItem(m_hWnd,IDC_BTN_DSTFILE),false);
		::EnableWindow(::GetDlgItem(m_hWnd,IDC_EDIT_SRCSTRING),true);
	}
	SetDlgItemText(IDC_EDIT_SRCSTRING,"");
	SetDlgItemText(IDC_EDIT_DSTSTRING,"");
	SetDlgItemText(IDC_EDIT_DSTFILE,"");
	SetDlgItemText(IDC_EDIT_SRCFILE,"");
	strInFile="";
	strOutFile="";
}

⌨️ 快捷键说明

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