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

📄 姬小兵的加密/解密小程序dlg.cpp

📁 我自己编写的加密解密小程序
💻 CPP
字号:
// 姬小兵的加密/解密小程序Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "姬小兵的加密/解密小程序.h"
#include "姬小兵的加密/解密小程序Dlg.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()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog

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

void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyDlg)
	DDX_Control(pDX, IDC_EDIT2, m_ctlCounter);
	DDX_Control(pDX, IDC_EDIT1, m_strEdit);
	DDX_Text(pDX, IDC_EDIT2, m_iCounter);
	DDX_Text(pDX, IDC_EDIT1, m_strInEdit);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
	//{{AFX_MSG_MAP(CMyDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_CODE, OnCode)
	ON_BN_CLICKED(IDC_UNCODE, OnUncode)
	ON_BN_CLICKED(ID_DISPLAY, OnDisplay)
	ON_BN_CLICKED(IDC_SAVE, OnSave)
	ON_WM_CTLCOLOR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers

BOOL CMyDlg::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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CMyDlg::OnCode() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);//将编辑框中的数据传递给数据成员
	int iFileLength;
	iFileLength=m_strInEdit.GetLength();
	CString buff=m_strInEdit;
	for(int i=0;i<iFileLength;i++)
		{
			if(m_strInEdit[i]!=0x0d&&m_strInEdit[i]!=0x0a&&m_strInEdit[i]!=','
				&&m_strInEdit[i]!='.'&&m_strInEdit[i]!='!'&&m_strInEdit[i]!=(int)(',')/256
				&&m_strInEdit[i]!='。'&&m_strInEdit[i]!='!'&&m_strInEdit[i]!=' '
				&&m_strInEdit[i]!='/'&&m_strInEdit[i]!='\\'&&m_strInEdit[i]!='?')
				m_strInEdit.SetAt(i,m_strInEdit[i]+1);//修改第i个字符,将其ASCII值加1
			if(m_strInEdit[i]=='z'+1)
				m_strInEdit.SetAt(i,'a');
			if(m_strInEdit[i]=='Z'+1)
				m_strInEdit.SetAt(i,'A');
		}
	m_iCounter++;
	UpdateData(FALSE);//更新数据,使数据在对话框中显示
}

void CMyDlg::OnUncode() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	int iFileLength;
	iFileLength=m_strInEdit.GetLength();
	CString buff=m_strInEdit;
	for(int i=0;i<iFileLength;i++)
		{
			if(m_strInEdit[i]!=0x0d&&m_strInEdit[i]!=0x0a&&m_strInEdit[i]!=','
				&&m_strInEdit[i]!='.'&&m_strInEdit[i]!='!'&&m_strInEdit[i]!=(int)(',')/256
				&&m_strInEdit[i]!='。'&&m_strInEdit[i]!='!'&&m_strInEdit[i]!=' '
				&&m_strInEdit[i]!='/'&&m_strInEdit[i]!='\\'&&m_strInEdit[i]!='?')
				m_strInEdit.SetAt(i,m_strInEdit[i]-1);
			if(m_strInEdit[i]=='a'-1)
				m_strInEdit.SetAt(i,'z');
			if(m_strInEdit[i]=='A'-1)
				m_strInEdit.SetAt(i,'Z');
		}
	m_iCounter--;
	UpdateData(FALSE);
}

void CMyDlg::OnDisplay() 
{
	// TODO: Add your control notification handler code here
		CFileDialog CFileDialog(TRUE,_T("txt"),NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
		_T("Text Files(*.txt)|*.txt|All Files(*.*)|*.*||"));//创建一个打开对话框
	if(CFileDialog.DoModal()==IDOK)//显示打开对话框
	{
		CString strPath=CFileDialog.GetPathName();//得到要打开的文件名和路径
		CFile file(strPath, CFile::modeRead);//构造文件对象并以只读方式打开文件
		//file.Open(strPath, CFile::modeRead);

		//CString strFlag("Jxb coded");
		//char strHead[80];
		//file.Read(strHead,strFlag.GetLength());
		//strHead[strFlag.GetLength()]='\0';
		//if(strFlag!=CString(strHead))
		//{
			int iFileLength=file.GetLength();//获得文件长度
			BYTE *buff=(BYTE *)(new char[iFileLength+1]);//分配保存数据的缓冲区
			file.SeekToBegin();//使文件指针指向文件的开始
			file.Read(buff,iFileLength);//将数据读入缓冲区
			*((char *)buff+iFileLength)='\0';
			file.Close();
			m_strInEdit=buff;//将数据赋给编辑框数据成员
			m_iCounter=0;//计数值为零
			UpdateData(FALSE);//更新数据,使数据在对话框中显示
			delete buff;
		//}
		/*else
		{
			file.Read(&m_iCounter,sizeof(int));
			CString strText;
			while(file.GetPosition()!=file.GetLength())
			{
				char ch;
				file.Read(&ch,1);
				strText+=ch;
			}
			m_strInEdit=strText;
			UpdateData(FALSE);
		}*/
	}
}

void CMyDlg::OnSave() 
{
	// TODO: Add your control notification handler code here
		UpdateData(TRUE);//将编辑框中的数据传递给数据成员
	CFileDialog CFileDialog(FALSE,_T("cod"),NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
		_T("Coded Files(*.cod)|*.cod|All Files(*.*)|*.*||"));//创建保存对话框

	if(CFileDialog.DoModal()==IDOK)
	{
		CString strPath=CFileDialog.GetPathName();//获取文件名和路径
		CFile fileCoded(strPath,CFile::modeCreate|CFile::modeWrite);//以写入的方式创建新文件
		//fileCoded.Open(strPath,CFile::modeCreate|CFile::modeWrite);
		int iLength;
		iLength=m_strInEdit.GetLength();//获取编辑框中文件的长度
		//if(m_iCounter==0)
			fileCoded.Write((LPCTSTR)m_strInEdit,iLength);//将数据写入新建的文件
		/*else
		{
			CString strFlag("Jxb Coded");
			fileCoded.Write((LPCTSTR)strFlag,strFlag.GetLength());
			fileCoded.Write(&m_iCounter,sizeof(int));
			fileCoded.Write((LPCTSTR)m_strInEdit,iLength);
		}*/
		fileCoded.Close();
	}
	
}


HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
		if(pWnd==(CWnd *)&m_ctlCounter)
	{
		pDC->SetTextColor(RGB(255,0,0));//设置文字颜色
	}
	// TODO: Return a different brush if the default is not desired
	return hbr;
}

⌨️ 快捷键说明

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