151 lines
3.4 KiB
C++
151 lines
3.4 KiB
C++
|
|
|||
|
// 读写文件Dlg.cpp: 实现文件
|
|||
|
//
|
|||
|
|
|||
|
#include "pch.h"
|
|||
|
#include "framework.h"
|
|||
|
#include "读写文件.h"
|
|||
|
#include "读写文件Dlg.h"
|
|||
|
#include "afxdialogex.h"
|
|||
|
|
|||
|
#ifdef _DEBUG
|
|||
|
#define new DEBUG_NEW
|
|||
|
#endif
|
|||
|
|
|||
|
|
|||
|
// C读写文件Dlg 对话框
|
|||
|
|
|||
|
|
|||
|
|
|||
|
C读写文件Dlg::C读写文件Dlg(CWnd* pParent /*=nullptr*/)
|
|||
|
: CDialogEx(IDD_MY_DIALOG, pParent)
|
|||
|
{
|
|||
|
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
|
|||
|
}
|
|||
|
|
|||
|
void C读写文件Dlg::DoDataExchange(CDataExchange* pDX)
|
|||
|
{
|
|||
|
CDialogEx::DoDataExchange(pDX);
|
|||
|
}
|
|||
|
|
|||
|
BEGIN_MESSAGE_MAP(C读写文件Dlg, CDialogEx)
|
|||
|
ON_WM_PAINT()
|
|||
|
ON_WM_QUERYDRAGICON()
|
|||
|
ON_BN_CLICKED(IDC_BUTTON1, &C读写文件Dlg::OnBnClickedButton1)
|
|||
|
ON_BN_CLICKED(IDC_BUTTON2, &C读写文件Dlg::OnBnClickedButton2)
|
|||
|
END_MESSAGE_MAP()
|
|||
|
|
|||
|
|
|||
|
// C读写文件Dlg 消息处理程序
|
|||
|
|
|||
|
BOOL C读写文件Dlg::OnInitDialog()
|
|||
|
{
|
|||
|
CDialogEx::OnInitDialog();
|
|||
|
|
|||
|
// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
|
|||
|
// 执行此操作
|
|||
|
SetIcon(m_hIcon, TRUE); // 设置大图标
|
|||
|
SetIcon(m_hIcon, FALSE); // 设置小图标
|
|||
|
|
|||
|
// TODO: 在此添加额外的初始化代码
|
|||
|
|
|||
|
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
|
|||
|
}
|
|||
|
|
|||
|
// 如果向对话框添加最小化按钮,则需要下面的代码
|
|||
|
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
|
|||
|
// 这将由框架自动完成。
|
|||
|
|
|||
|
void C读写文件Dlg::OnPaint()
|
|||
|
{
|
|||
|
if (IsIconic())
|
|||
|
{
|
|||
|
CPaintDC dc(this); // 用于绘制的设备上下文
|
|||
|
|
|||
|
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
|
|||
|
|
|||
|
// 使图标在工作区矩形中居中
|
|||
|
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;
|
|||
|
|
|||
|
// 绘制图标
|
|||
|
dc.DrawIcon(x, y, m_hIcon);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
CDialogEx::OnPaint();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
//当用户拖动最小化窗口时系统调用此函数取得光标
|
|||
|
//显示。
|
|||
|
HCURSOR C读写文件Dlg::OnQueryDragIcon()
|
|||
|
{
|
|||
|
return static_cast<HCURSOR>(m_hIcon);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
void C读写文件Dlg::OnBnClickedButton1()
|
|||
|
{
|
|||
|
// TODO: 在此添加控件通知处理程序代码
|
|||
|
TCHAR szFilter[] = _T("文本文件(*.txt)|*.txt|所有文件(*.*)|*.*||");
|
|||
|
// 构造打开文件对话框
|
|||
|
CFileDialog fileDlg(TRUE, _T("txt"), NULL, 0, szFilter, this);
|
|||
|
CString strFilePath;
|
|||
|
|
|||
|
// 显示打开文件对话框
|
|||
|
if (IDOK == fileDlg.DoModal())
|
|||
|
{
|
|||
|
// 如果点击了文件对话框上的“打开”按钮,则将选择的文件路径显示到编辑框里
|
|||
|
strFilePath = fileDlg.GetPathName();
|
|||
|
SetDlgItemText(IDC_EDIT1, strFilePath);
|
|||
|
CFile file;
|
|||
|
if (file.Open(strFilePath, CFile::modeRead))
|
|||
|
{
|
|||
|
char* buf = new char[file.GetLength() + 1];
|
|||
|
buf[file.GetLength()] = '\0';
|
|||
|
file.Read(buf, file.GetLength());
|
|||
|
file.Close();
|
|||
|
CString str;
|
|||
|
str = buf;
|
|||
|
delete[]buf;
|
|||
|
SetDlgItemText(IDC_EDIT2, str);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
MessageBox(_T("文件打开失败"));
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
MessageBox(_T("用户取消选择文件"));
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
void C读写文件Dlg::OnBnClickedButton2()
|
|||
|
{
|
|||
|
// TODO: 在此添加控件通知处理程序代码
|
|||
|
CString strFilePath;
|
|||
|
CStringA str;
|
|||
|
GetDlgItemText(IDC_EDIT2, strFilePath);
|
|||
|
str = strFilePath;
|
|||
|
GetDlgItemText(IDC_EDIT1, strFilePath);
|
|||
|
CFile file;
|
|||
|
if (file.Open(strFilePath, CFile::modeReadWrite))
|
|||
|
{
|
|||
|
file.Write(str, str.GetLength());
|
|||
|
file.Close();
|
|||
|
MessageBox(_T("文件保存成功"));
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
MessageBox(_T("文件打开失败"));
|
|||
|
}
|
|||
|
}
|