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

📄 encrywithpicturedlg.cpp

📁 这是一个利用图片加密文件的源程序
💻 CPP
字号:
// EncryWithPictureDlg.cpp : implementation file
//

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

/////////////////////////////////////////////////////////////////////////////
// CEncryWithPictureDlg dialog

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

void CEncryWithPictureDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEncryWithPictureDlg)
	DDX_Control(pDX, IDC_PICTURE, m_picture);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CEncryWithPictureDlg, CDialog)
	//{{AFX_MSG_MAP(CEncryWithPictureDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTSELECTPIC, OnSelectPic)
	ON_BN_CLICKED(IDC_BTENCRY, OnEncry)
	ON_BN_CLICKED(IDC_BTADDFILE, OnAddFile)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEncryWithPictureDlg message handlers

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

	
	return TRUE;
}

void CEncryWithPictureDlg::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 CEncryWithPictureDlg::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();
	}
	if(!path.IsEmpty())
	{
		CRect rc;
		m_picture.GetClientRect(&rc);
		CDC *pDC=m_picture.GetDC();
		HBITMAP hbmp=(HBITMAP)::LoadImage(AfxGetInstanceHandle(),path,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_DEFAULTCOLOR|LR_DEFAULTSIZE);
		BITMAP bm;
		GetObject(hbmp,sizeof(bm),&bm);
		CDC memdc;
		memdc.CreateCompatibleDC(pDC);
		memdc.SelectObject(hbmp);
		pDC->StretchBlt(0,0,rc.Width(),rc.Height(),&memdc,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CEncryWithPictureDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CEncryWithPictureDlg::OnSelectPic() 
{
	CFileDialog log(TRUE,"文件",NULL,OFN_HIDEREADONLY,"位图(*.BMP)|*.bmp||",NULL);
	if(log.DoModal()==IDOK)
	{
	path=log.GetPathName();
	GetDlgItem(IDC_EDPICPATH)->SetWindowText(path);
	}
}

void CEncryWithPictureDlg::OnEncry() 
{
	CString strpic,strtxt,desname;
	GetDlgItem(IDC_EDFILEPATH)->GetWindowText(strtxt);
	GetDlgItem(IDC_EDPICPATH)->GetWindowText(strpic);
	CFile readpic,readfile,writefile;

	desname.Format("%smingrisofttemp.txt",strpathtemp);
	HANDLE handle=::CreateFile(desname,GENERIC_WRITE,FILE_SHARE_WRITE,0,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
	if(handle)::CloseHandle(handle);
	int i=readfile.Open(strtxt,CFile::modeRead);
	readpic.Open(strpic,CFile::modeRead);
	writefile.Open(desname,CFile::modeCreate|CFile::modeReadWrite);

	if(i==0)return;
	char picbuf[128];
	char filebuf[128];
	char desbuf[128];
	while(1)
	{
	ZeroMemory(filebuf,128);
	ZeroMemory(picbuf,128);
	ZeroMemory(desbuf,128);
	DWORD i=readfile.Read(filebuf,128);
	readpic.Read(picbuf,128);
	for(int p=0;p<i;p++)
	{
		char m=filebuf[p];
		char n=picbuf[p];
		desbuf[p]=m^n;
	}
	writefile.Write(desbuf,i);
	if(i==0)goto end;
	}
	end:
	readpic.Close();
	readfile.Close();
	writefile.Close();
	::DeleteFile(strtxt);
	::rename(desname,strtxt);
	AfxMessageBox("成功");
}

void CEncryWithPictureDlg::OnAddFile() 
{
	CFileDialog log(TRUE,"文件",NULL,OFN_HIDEREADONLY,"文本(*.txt)|*.txt||",NULL);
	if(log.DoModal()==IDOK)
	{
	CString path;
	path=log.GetPathName();
	strpathtemp=log.GetFileName();
	int pos=path.Find(strpathtemp);
	strpathtemp=path.Left(pos);
	GetDlgItem(IDC_EDFILEPATH)->SetWindowText(path);
	}
}

⌨️ 快捷键说明

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