📄 文件比较mfcdlg.cpp
字号:
// 文件比较MFCDlg.cpp : implementation file
//
#include "stdafx.h"
#include "文件比较MFC.h"
#include "文件比较MFCDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMFCDlg dialog
CMFCDlg::CMFCDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMFCDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMFCDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMFCDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMFCDlg)
DDX_Control(pDX, IDC_EDIT5, m_EchoInfor);
DDX_Control(pDX, IDC_EDIT4, m_output2);
DDX_Control(pDX, IDC_EDIT3, m_output1);
DDX_Control(pDX, IDC_EDIT2, m_input2);
DDX_Control(pDX, IDC_EDIT1, m_input1);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMFCDlg, CDialog)
//{{AFX_MSG_MAP(CMFCDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnOpenFile1)
ON_BN_CLICKED(IDC_BUTTON2, OnOpenFile2)
ON_BN_CLICKED(IDC_BUTTON3, OnCompare)
ON_EN_CHANGE(IDC_EDIT5, OnEchoInfor)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMFCDlg message handlers
BOOL CMFCDlg::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
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 CMFCDlg::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 CMFCDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMFCDlg::OnOpenFile1()
{
// TODO: Add your control notification handler code here
//打开比较文件1
OPENFILENAME OpenFileName;
char szDirName[MAX_PATH]="";
char szFile[MAX_PATH]="\0";
char szFileTitle[MAX_PATH]="\0";
char szFilter[]={"所有文件(*.*)\0*.*\0"};
OpenFileName.lStructSize=sizeof(OPENFILENAME);
OpenFileName.hwndOwner=NULL;
OpenFileName.hInstance=NULL;
OpenFileName.lpstrFilter=szFilter;
OpenFileName.lpstrCustomFilter=(LPTSTR)NULL;
OpenFileName.nMaxCustFilter=0L;
OpenFileName.nFilterIndex=1L;
OpenFileName.lpstrFile=szFile;
OpenFileName.nMaxFile=sizeof(szFile);
OpenFileName.lpstrFileTitle=szFileTitle;
OpenFileName.nMaxFileTitle=sizeof(szFileTitle);
OpenFileName.lpstrInitialDir=NULL;
OpenFileName.lpstrTitle="打开比较文件1";
OpenFileName.nFileOffset=0;
OpenFileName.nFileExtension=0;
OpenFileName.lpstrDefExt=NULL;
OpenFileName.lCustData=0;
OpenFileName.Flags=OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
if (GetOpenFileName(&OpenFileName)) //打开成功
{
m_input1.SetSel(0,-1);
m_input1.ReplaceSel(OpenFileName.lpstrFile); //在"文件1"的输入框中显示文件路径
}
else //打开失败
{
if (strcmp(OpenFileName.lpstrFile,"")==0) //取消了"打开文件"
{
return;
}
MessageBox("打开比较文件1失败!","失败",MB_OK);
}
}
void CMFCDlg::OnOpenFile2()
{
// TODO: Add your control notification handler code here
//打开比较文件2
OPENFILENAME OpenFileName;
char szDirName[MAX_PATH]="";
char szFile[MAX_PATH]="\0";
char szFileTitle[MAX_PATH]="\0";
char szFilter[]={"所有文件(*.*)\0*.*\0"};
OpenFileName.lStructSize=sizeof(OPENFILENAME);
OpenFileName.hwndOwner=NULL;
OpenFileName.hInstance=NULL;
OpenFileName.lpstrFilter=szFilter;
OpenFileName.lpstrCustomFilter=(LPTSTR)NULL;
OpenFileName.nMaxCustFilter=0L;
OpenFileName.nFilterIndex=1L;
OpenFileName.lpstrFile=szFile;
OpenFileName.nMaxFile=sizeof(szFile);
OpenFileName.lpstrFileTitle=szFileTitle;
OpenFileName.nMaxFileTitle=sizeof(szFileTitle);
OpenFileName.lpstrInitialDir=NULL;
OpenFileName.lpstrTitle="打开比较文件2";
OpenFileName.nFileOffset=0;
OpenFileName.nFileExtension=0;
OpenFileName.lpstrDefExt=NULL;
OpenFileName.lCustData=0;
OpenFileName.Flags=OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
if (GetOpenFileName(&OpenFileName))
{
m_input2.SetSel(0,-1);
m_input2.ReplaceSel(OpenFileName.lpstrFile); //在"文件2"的输入框中显示文件路径
}
else
{
if (strcmp(OpenFileName.lpstrFile,"")==0) //取消了"打开文件"
{
return;
}
MessageBox("打开比较文件2失败!","失败",MB_OK);
}
}
void CMFCDlg::OnCompare() //文件比较按钮
{
// TODO: Add your control notification handler code here
//得到两输入框的文件路径长度
char TextContent1[MAX_PATH]="";
char TextContent2[MAX_PATH]="";
int CountInput1=m_input1.GetWindowTextLength();
int CountInput2=m_input2.GetWindowTextLength();
if (CountInput1 > MAX_PATH) //文件1路径过长
{
MessageBox("文件1的路径太长啦!","错误",MB_OK);
return;
}
if (CountInput1 == 0) //文件1路径为空
{
MessageBox("文件1路径不能为空!");
return;
}
if (CountInput2 > MAX_PATH) //文件2路径过长
{
MessageBox("文件2的路径太长啦!","错误",MB_OK);
return;
}
if (CountInput2 == 0) //文件2路径为空
{
MessageBox("文件2路径不能为空!");
return;
}
//得到两文件路径
m_input1.GetLine(1,TextContent1,MAX_PATH);
m_input2.GetLine(1,TextContent2,MAX_PATH);
//打开文件
char diff1[100]="";
char diff2[100]="";
char diffInfo1[500]="";
char diffInfo2[500]="";
char content1[21],content2[21];
int ch1,ch2;
FILE *in1,*in2;
if ((in1=fopen(TextContent1,"rb"))==NULL)
{
MessageBox("打开比较文件1时出错!","错误",MB_OK);
return;
}
if ((in2=fopen(TextContent2,"rb"))==NULL)
{
MessageBox("打开比较文件2时出错!","错误",MB_OK);
return;
}
//读文件比较
ch1=fgetc(in1);
ch2=fgetc(in2);
while(!feof(in1) || !feof(in2))
{
if (ch1 != ch2) //两文件内容不相同
{
m_output1.SetSel(0,-1);
m_output1.ReplaceSel("两文件内容不同:\r\n");
m_output2.SetSel(0,-1);
m_output2.ReplaceSel("两文件内容不同:\r\n");
sprintf(diff1,"不同点:\r\n文件1:\r\n位置:%d\r\n字符:%c\r\nASCII:(0x%X)",ftell(in1),ch1,ch1);
sprintf(diff2,"不同点:\r\n文件2:\r\n位置:%d\r\n字符:%c\r\nASCII:(0x%X)",ftell(in2),ch2,ch2);
fseek( in1, -1, 1 );
fseek( in2, -1, 1 );
fgets( content1, 20, in1 );
fgets( content2, 20, in2 );
content1[ strlen(content1)-2 ] = '\0'; //去掉后两个字节,因为可能是换行符
content2[ strlen(content2)-2 ] = '\0'; //去掉后两个字节,因为可能是换行符
sprintf(diffInfo1,"您所比较的两个文件内容不相同,\r\n从不同处起的字符串为:\r\n\r\n文件1:%s\r\n文件2:%s\r\n\r\n",content1,content2);
sprintf(diffInfo2,"文件1 ASCII(16进制):%02X %02X %02X %02X %02X\r\n文件2 ASCII(16进制):%02X %02X %02X %02X %02X",
content1[0],content1[1],content1[2],content1[3],content1[4],
content2[0],content2[1],content2[2],content2[3],content2[4]);
m_output1.ReplaceSel(diff1);
m_output2.ReplaceSel(diff2);
m_EchoInfor.SetSel(0,-1);
m_EchoInfor.ReplaceSel(diffInfo1);
m_EchoInfor.ReplaceSel(diffInfo2);
return;
}
ch1=fgetc(in1);
ch2=fgetc(in2);
}
fclose (in1);
fclose (in2);
//两文件内容相同
m_output1.SetSel(0,-1);
m_output1.ReplaceSel("两文件内容相同!\r\n\r\n ^_^");
m_output2.SetSel(0,-1);
m_output2.ReplaceSel("两文件内容相同!\r\n\r\n ^_^");
if ( strcmp( TextContent1, TextContent2 ) == 0 ) //同一个文件
{
m_EchoInfor.SetSel(0,-1);
m_EchoInfor.ReplaceSel("您比较同一个文件,内容肯定相同啊!!!\r\n\r\n\r\n\t\tO_O");
}
else
{
m_EchoInfor.SetSel(0,-1);
m_EchoInfor.ReplaceSel("您所比较的两个文件内容是相同的!!!!\r\n\r\n\r\n\r\n\t\t^_^");
}
}
void CMFCDlg::OnEchoInfor()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -