www.gusucode.com > VC++文本文件加密、解密程序小工具源码-源码程序 > VC++文本文件加密、解密程序小工具源码-源码程序/code/文章Dlg.cpp

    // 文章Dlg.cpp : implementation file
// Download by http://www.NewXing.com

#include "stdafx.h"
#include "文章.h"
#include "文章Dlg.h"

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

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

CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyDlg)
	m_strInEdit = _T("");
	m_iCounter = 0;
	//}}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, ID_DISPLAY, m_Open);
	DDX_Control(pDX, IDCANCEL, m_Close);
	DDX_Control(pDX, IDC_UNCODE, m_UnCode);
	DDX_Control(pDX, IDC_SAVE, m_Save);
	DDX_Control(pDX, ID_CODE, m_Code);
	DDX_Control(pDX, IDC_EDIT1, m_strEdit);
	DDX_Text(pDX, IDC_EDIT1, m_strInEdit);
	DDX_Text(pDX, IDC_EDIT2, m_iCounter);
	//}}AFX_DATA_MAP
}

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

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

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

	SetWindowText(_T("文章加密--作者:benson!"));
	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 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::OnDisplay() 
{
	// TODO: Add your control notification handler code here
	CFileDialog FileDlg(TRUE,_T("txt"),NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
		_T("Text Files(*.txt)|*.txt|All Files(*.*)|*.*||"));
	if(FileDlg.DoModal()==IDOK)
	{
		CString strPath=FileDlg.GetPathName();
		CFile file;
		file.Open(strPath, CFile::modeRead);

		CString strFlag("BensonCoded");
		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::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);
			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::OnSave() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CFileDialog FileDlg(FALSE,_T("cod"),NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
		_T("Coded Files(*.cod)|*.cod|All Files(*.*)|*.*||"));

	if(FileDlg.DoModal()==IDOK)
	{
		CString strPath=FileDlg.GetPathName();
		CFile fileCoded;
		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("BensonCoded");
			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
	
	// TODO: Return a different brush if the default is not desired
	if(pWnd==(CWnd *)&m_ctlCounter)
	{
		 
	
		pDC->SetBkColor(RGB(100,200,255));
		pDC->SetTextColor(RGB(255,0,0));
	}
	return hbr;
}