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

📄 mima_dlg.cpp

📁 VC++6.0基于CAN协议的车辆控制
💻 CPP
字号:
// Mima_Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "PROJECTPMU.h"
#include "Mima_Dlg.h"
#include <direct.h> 
#include <io.h>

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

/////////////////////////////////////////////////////////////////////////////
// CMima_Dlg dialog


CMima_Dlg::CMima_Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMima_Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMima_Dlg)
	//}}AFX_DATA_INIT
}


void CMima_Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMima_Dlg)
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMima_Dlg, CDialog)
	//{{AFX_MSG_MAP(CMima_Dlg)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
	ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
	ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
	ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
	ON_BN_CLICKED(IDC_BUTTON10, OnButton10)
	ON_BN_CLICKED(IDC_BUTTON11, OnButton11)
	ON_WM_SHOWWINDOW()
	ON_BN_CLICKED(IDC_CHANGEPW_BTN, OnChangepwBtn)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMima_Dlg message handlers

BOOL CMima_Dlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	if(NULL != GetModuleFileName(NULL,Dir_Path,256))  //得到应用程序所在文件夹路径
    {		
		int n=strlen(Dir_Path);
		for (int i=n; i>=0; i--)
		{
			if   (Dir_Path[i]   ==   '\\')   
			{   
				Dir_Path[i]   =   '\0';   
				break;   
			}   
		}
		strcpy(Mima_Path,Dir_Path);
        strcat(Mima_Path,"\\mm.ini"); // 播放列表配置文件路径
		strcpy(Config1_Path,Dir_Path);
		strcat(Config1_Path,"\\config1.ini"); // config1.ini路径
	}

	//判断文件是否存在,不存在就创建
    if ( _access(Mima_Path,0) == 0 )  
	{   
		//存在 
		//MessageBox("yes");
	} 
	else
	{   //不存在
	//	CreateDirectory(PL_Path,NULL);   
	//	MessageBox("no ex");
		FILE *fp;
		if((fp=fopen(Mima_Path,"w"))==NULL)
		{
		}
		else
		{
			CString tempField2;
			tempField2 = _T("8888");
			fputs(tempField2,fp);
			fclose(fp);
		}
	}

	 //初始化数据
    m_szPassword="";
	m_szPassword1="";
	m_szPassword2="";
	m_szSuperPW.Format("%d",int(SUPERPASSWORD));

	b_IsChinese=true;
	m_b_ChangePW=false;

	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CMima_Dlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	CString getstr ;
	CString str ;
	((CEdit *)GetDlgItem(IDC_MIMA_EDIT))->GetWindowText(getstr);
	str = getstr + _T("1");
	((CEdit *)GetDlgItem(IDC_MIMA_EDIT))->SetWindowText(str); 
	
}

void CMima_Dlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	CString getstr ;
	CString str ;
	((CEdit *)GetDlgItem(IDC_MIMA_EDIT))->GetWindowText(getstr);
	str = getstr + _T("2");
	((CEdit *)GetDlgItem(IDC_MIMA_EDIT))->SetWindowText(str); 
}

void CMima_Dlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	CString getstr ;
	CString str ;
	((CEdit *)GetDlgItem(IDC_MIMA_EDIT))->GetWindowText(getstr);
	str = getstr + _T("3");
	((CEdit *)GetDlgItem(IDC_MIMA_EDIT))->SetWindowText(str); 
}

void CMima_Dlg::OnButton4() 
{
	// TODO: Add your control notification handler code here
	CString getstr ;
	CString str ;
	((CEdit *)GetDlgItem(IDC_MIMA_EDIT))->GetWindowText(getstr);
	str = getstr + _T("4");
	((CEdit *)GetDlgItem(IDC_MIMA_EDIT))->SetWindowText(str);
}

void CMima_Dlg::OnButton5() 
{
	// TODO: Add your control notification handler code here
	CString getstr ;
	CString str ;
	((CEdit *)GetDlgItem(IDC_MIMA_EDIT))->GetWindowText(getstr);
	str = getstr + _T("5");
	((CEdit *)GetDlgItem(IDC_MIMA_EDIT))->SetWindowText(str); 
}



void CMima_Dlg::OnButton6() 
{
	// TODO: Add your control notification handler code here
	CString getstr ;
	CString str ;
	((CEdit *)GetDlgItem(IDC_MIMA_EDIT))->GetWindowText(getstr);
	str = getstr + _T("6");
	((CEdit *)GetDlgItem(IDC_MIMA_EDIT))->SetWindowText(str); 
}

void CMima_Dlg::OnButton7() 
{
	// TODO: Add your control notification handler code here
	CString getstr ;
	CString str ;
	((CEdit *)GetDlgItem(IDC_MIMA_EDIT))->GetWindowText(getstr);
	str = getstr + _T("7");
	((CEdit *)GetDlgItem(IDC_MIMA_EDIT))->SetWindowText(str); 
}

void CMima_Dlg::OnButton8() 
{
	// TODO: Add your control notification handler code here
	CString getstr ;
	CString str ;
	((CEdit *)GetDlgItem(IDC_MIMA_EDIT))->GetWindowText(getstr);
	str = getstr + _T("8");
	((CEdit *)GetDlgItem(IDC_MIMA_EDIT))->SetWindowText(str);
}

void CMima_Dlg::OnButton9() 
{
	// TODO: Add your control notification handler code here
	CString getstr ;
	CString str ;
	((CEdit *)GetDlgItem(IDC_MIMA_EDIT))->GetWindowText(getstr);
	str = getstr + _T("9");
	((CEdit *)GetDlgItem(IDC_MIMA_EDIT))->SetWindowText(str);
}

void CMima_Dlg::OnButton10() 
{
	// TODO: Add your control notification handler code here
	CString getstr ;
	CString str ;
	((CEdit *)GetDlgItem(IDC_MIMA_EDIT))->GetWindowText(getstr);
	str = getstr + _T("0");
	((CEdit *)GetDlgItem(IDC_MIMA_EDIT))->SetWindowText(str); 
}

void CMima_Dlg::OnButton11() //清空密码
{
	// TODO: Add your control notification handler code here
	((CEdit *)GetDlgItem(IDC_MIMA_EDIT))->SetWindowText(_T(""));
}

void CMima_Dlg::OnShowWindow(BOOL bShow, UINT nStatus) 
{
	CDialog::OnShowWindow(bShow, nStatus);
	
	// TODO: Add your message handler code here
	
}


void CMima_Dlg::OnOK() 
{
	// TODO: Add extra validation here
	CString getstr,temp ;
	
	if(!m_b_ChangePW)    
	{		
		((CEdit *)GetDlgItem(IDC_MIMA_EDIT))->GetWindowText(getstr);
		if (getstr=="")  //判断密码输入不能为空
		{
			if (b_IsChinese)
				temp = _T("密码不能为空!\n请先输入正确密码");	
			else
				temp = _T("Password error!\nPlease input right password");
			AfxMessageBox(temp);
			return;
		}
		else
		{
			if(dcfile(Mima_Path))  //解密文件
			{
				if((getstr==m_szSuperPW)||(getstr==m_szPassword))
					CDialog::OnOK();
				else
				{
					if (b_IsChinese)
						temp = _T("密码错误!");	
					else
						temp = _T("Password error!");
					AfxMessageBox(temp);
					((CEdit *)GetDlgItem(IDC_MIMA_EDIT))->SetWindowText(_T(""));
				}	
			}
		}
	}
	else
	{
		((CEdit *)GetDlgItem(IDC_MIMA_EDIT))->GetWindowText(getstr);
		if (getstr=="")  //判断密码输入不能为空
		{
			if (b_IsChinese)
				temp = _T("输入密码不能为空!");	
			else
				temp = _T("The Password do not be empty!");
			AfxMessageBox(temp);
			return;
		}
		if(m_szPassword1=="")
		{ 
			m_szPassword1=getstr;
			((CEdit *)GetDlgItem(IDC_MIMA_EDIT))->SetWindowText(_T(""));
			if (b_IsChinese)
			{
				((CStatic *)GetDlgItem(IDC_TISHI_STATIC))->SetWindowText(_T("请再次输入密码确认:"));
				((CButton *)GetDlgItem(IDC_CHANGEPW_BTN))->SetWindowText(_T("取消修改"));
			}
			else
			{
				((CStatic *)GetDlgItem(IDC_TISHI_STATIC))->SetWindowText(_T("Input password again"));
				((CButton *)GetDlgItem(IDC_CHANGEPW_BTN))->SetWindowText(_T("Cancel Change "));
			}
		}
		else
		{
			m_szPassword2=getstr;
			if(m_szPassword1==m_szPassword2)
			{
				//先将原文件删除并新建一个。
				FILE *fp;
				if((fp=fopen(Mima_Path,"w"))==NULL)
				{
				}
				else
				{
					fclose(fp);
				}
				
				if(ecfile(Mima_Path)) //修改密码并加密文件
				{
					m_szPassword="";
					m_szPassword1="";
					m_szPassword2="";
					m_b_ChangePW = false;
					((CEdit *)GetDlgItem(IDC_MIMA_EDIT))->SetWindowText(_T(""));
					if (b_IsChinese)
					{
						temp = _T("修改密码成功!");
						((CStatic *)GetDlgItem(IDC_TISHI_STATIC))->SetWindowText(_T("请输入密码:"));
						((CButton *)GetDlgItem(IDC_CHANGEPW_BTN))->SetWindowText(_T("修改密码"));
					}
					else
					{
						temp = _T("Change password succeed!");
						((CStatic *)GetDlgItem(IDC_TISHI_STATIC))->SetWindowText(_T("Input Password: "));
						((CButton *)GetDlgItem(IDC_CHANGEPW_BTN))->SetWindowText(_T("Change Password"));
					}
					AfxMessageBox(temp);
				}
				else
				{
					if (b_IsChinese)
					{
						temp = _T("修改密码失败!");
					}
					else
					{
						temp = _T("Change password unsuccessfully!");
					}
					AfxMessageBox(temp);
				}
				
			}
			else
			{
				m_szPassword2="";
				((CEdit *)GetDlgItem(IDC_MIMA_EDIT))->SetWindowText(_T(""));
				if (b_IsChinese)
				{
					temp = _T("确认密码错误!");
				}
				else
				{
					temp = _T("Password error!");
				}
				AfxMessageBox(temp);
			}
		}
	}

	
//	CDialog::OnOK();
}


//给文件加密的函数
BOOL CMima_Dlg::ecfile(LPCTSTR fpath)
{
	char *data;
	CFile *file;
	DWORD flen;

//	m_password=837;

	file = new CFile;
	if ( !file->Open(fpath, CFile::shareDenyNone|CFile::modeReadWrite))
	{
		return FALSE;
	}

//	flen = file->GetLength();
//	data = new char[(int)flen];

//	file->SeekToBegin();
//	file->Read(data, flen);
  //  strncpy(   data,   str,   strlen( m_szSuperPW )   );  
    UpdateData(TRUE);
	CString str;
	((CEdit *)GetDlgItem(IDC_MIMA_EDIT))->GetWindowText(str);
	data = str.GetBuffer(0);
    flen = strlen(data);

	//m_password = _atoi64(data);

	for(int i=0; i<(int)flen; i++)
	{
		data[i] ^= SUPERPASSWORD;
		data[i] ^= flen;
	}


	file->SeekToBegin();
	file->Write(data, flen);
	//delete[] data;

/*	//m_password = SUPERPASSWORD);
	//添加密码验证信息
	char cpass[5] = "love";
	for(int j=0; j<5; j++)
	{
		cpass[j] ^= SUPERPASSWORD;
	}
	file->SeekToEnd();
	file->Write(&cpass, 5);
*/
    file->Close();
	delete file;

	return TRUE;
}

//给文件解密的函数  判断密码是否输入正确
BOOL CMima_Dlg::dcfile(LPCTSTR fpath)
{
	char *data;
	CFile *file;
	DWORD flen;
//	char love[5];

	file = new CFile;
	if( !file->Open(fpath, CFile::shareDenyNone|CFile::modeReadWrite))
	{
		return FALSE;
	}

	flen = file->GetLength();
	data = new char[(int)flen];

	char sBuff[100];
	memset(sBuff,0,sizeof(sBuff));
/*
	//检验密码是不是正确
	file->Seek(-5, CFile::end);
	file->Read(&love, 5);

//	m_password = 837;//epass();

	for(int i=0; i<5; i++)
	{
		love[i] ^= SUPERPASSWORD;
	}

	if(strcmp(love, "love")!=0)
	{
		return FALSE;
	}
*/

	//解密
	file->SeekToBegin();
	file->Read(data, flen);

	for(int j=0; j<(int)flen; j++)
	{
		data[j] ^= SUPERPASSWORD;
		data[j] ^= flen;
		sBuff[j]=data[j];
	}
//	data[j]="\0";
	//sBuff[j]="\0";
	/*
	file->SeekToBegin();
	file->Write(data, flen);
	file->SetLength(flen-5);
	*/
	m_szPassword=sBuff;

	file->Close();

	delete[] data;
	delete file;
	return TRUE;
}

void CMima_Dlg::OnChangepwBtn() 
{
	// TODO: Add your control notification handler code here
	CString temp,str;

	((CEdit *)GetDlgItem(IDC_MIMA_EDIT))->GetWindowText(str);
	
	if(!m_b_ChangePW)    
	{
		UpdateData(TRUE);

		if (str=="")  //判断密码输入不能为空
		{
			if (b_IsChinese)
				temp = _T("密码不能为空!\n请先输入正确密码");	
			else
				temp = _T("Password error!\nPlease input right password");
			AfxMessageBox(temp);
			return;
		}
		else 
		{  
			if(dcfile(Mima_Path))
			{
				if(str!=m_szPassword)   //判断输入旧的密码应正确
				{
					if (b_IsChinese)
						temp = _T("密码错误!\n请先输入正确密码");	
					else
						temp = _T("Password error!\nPlease input right password");
					AfxMessageBox(temp);
					return;	
				}
				else
				{
					//AfxMessageBox("dfsfsf");
					m_b_ChangePW = true;
					((CEdit *)GetDlgItem(IDC_MIMA_EDIT))->SetWindowText(_T(""));
					if (b_IsChinese)
					{
						((CStatic *)GetDlgItem(IDC_TISHI_STATIC))->SetWindowText(_T("请输入新密码:"));
						((CButton *)GetDlgItem(IDC_CHANGEPW_BTN))->SetWindowText(_T("取消修改"));
					}
					else
					{
						((CStatic *)GetDlgItem(IDC_TISHI_STATIC))->SetWindowText(_T("Input New Password: "));
						((CButton *)GetDlgItem(IDC_CHANGEPW_BTN))->SetWindowText(_T("Cancel Change"));
					}
				}
			}
		}
	}
	else
	{
		m_szPassword="";
		m_szPassword1="";
		m_szPassword2="";
		m_b_ChangePW = false;
		((CEdit *)GetDlgItem(IDC_MIMA_EDIT))->SetWindowText(_T(""));
        if (b_IsChinese)
		{
			((CStatic *)GetDlgItem(IDC_TISHI_STATIC))->SetWindowText(_T("请输入密码:"));
			((CButton *)GetDlgItem(IDC_CHANGEPW_BTN))->SetWindowText(_T("修改密码"));
		}
		else
		{
			((CStatic *)GetDlgItem(IDC_TISHI_STATIC))->SetWindowText(_T("Input Password: "));
			((CButton *)GetDlgItem(IDC_CHANGEPW_BTN))->SetWindowText(_T("Change Password"));
		}
	}
}

⌨️ 快捷键说明

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