MFC基于对话框,在文本文件中查询个人信息,并将个人信息输出到编辑框
时间: 2024-02-26 07:55:24 浏览: 25
好的,我可以为您解答如何实现这个功能。
首先,您需要在对话框中添加一个编辑框用于显示查询结果。然后,在对话框类的头文件中声明一个CString类型的变量用于存储文本文件的路径。
接下来,在对话框类的OnInitDialog()函数中,您可以使用CFileDialog类让用户选择要查询的文本文件,并将选择的文件路径存储到上述的CString变量中。
然后,您可以使用CStdioFile类来读取文本文件中的信息。假设您的文本文件中每行都是一个人的信息,包括姓名、年龄、性别等等,您可以使用CStdioFile类的ReadString()函数逐行读取文件内容,并使用CString的Split()函数将每行信息分割成一个个字段,然后进行匹配查询。
最后,将查询结果输出到编辑框中即可。您可以使用CEdit类的SetWindowText()函数将查询结果显示到编辑框中。
希望这个回答能够帮助您实现这个功能。如果您需要更详细的代码实现,可以参考MFC的官方文档或者一些相关的教程。
相关问题
设计mfc应用程序,将学生的信息添加到文件中,并将文件中的信息读取出来显示在编辑框中(任选文本文件或二进制文件实现)
好的,这是一个典型的MFC应用程序,需要使用MFC的文档视图框架来实现。
首先,我们需要创建一个MFC文档视图应用程序。在Visual Studio中,选择“新建项目”,然后选择“Visual C++”下的“MFC应用程序”。
接下来,在“应用程序向导”中选择“单文档”框架,然后勾选“文档/视图支持”和“生成多字节字符集”。
现在我们需要设计我们的文档类和视图类。我们可以使用Visual Studio的“添加类”功能来创建它们。
首先,我们创建一个名为“StudentInfo”的结构体来存储学生的信息,如下所示:
```c++
struct StudentInfo {
CString name;
int age;
CString major;
double gpa;
};
```
然后,在文档类中,我们创建一个名为“studentList”的std::vector来存储所有学生的信息。
```c++
class CMyDoc : public CDocument
{
protected:
CMyDoc() noexcept;
DECLARE_DYNCREATE(CMyDoc)
// Attributes
public:
std::vector<StudentInfo> studentList;
// Operations
public:
void AddStudent(const StudentInfo& student);
bool LoadFromFile(const CString& fileName);
bool SaveToFile(const CString& fileName);
// Overrides
public:
virtual BOOL OnNewDocument();
virtual void Serialize(CArchive& ar);
// Implementation
public:
virtual ~CMyDoc();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
};
```
在文档类中,我们还需要实现AddStudent()、LoadFromFile()和SaveToFile()函数来添加学生、从文件中读取学生信息和将学生信息保存到文件中。
```c++
void CMyDoc::AddStudent(const StudentInfo& student)
{
studentList.push_back(student);
SetModifiedFlag();
}
bool CMyDoc::LoadFromFile(const CString& fileName)
{
CFile file;
if (!file.Open(fileName, CFile::modeRead))
return false;
CArchive ar(&file, CArchive::load);
studentList.clear();
int count;
ar >> count;
for (int i = 0; i < count; i++) {
StudentInfo student;
ar >> student.name >> student.age >> student.major >> student.gpa;
studentList.push_back(student);
}
SetModifiedFlag(FALSE);
return true;
}
bool CMyDoc::SaveToFile(const CString& fileName)
{
CFile file;
if (!file.Open(fileName, CFile::modeCreate | CFile::modeWrite))
return false;
CArchive ar(&file, CArchive::store);
ar << (int)studentList.size();
for (const auto& student : studentList) {
ar << student.name << student.age << student.major << student.gpa;
}
SetModifiedFlag(FALSE);
return true;
}
```
接下来,我们需要设计视图类来显示学生信息。我们可以使用Visual Studio的“添加类”功能来创建它。
在视图类中,我们需要添加一些编辑框来显示学生信息。我们还需要添加一些按钮来触发添加学生、从文件中读取学生信息和将学生信息保存到文件中的操作。
在视图类中,我们需要实现OnInitDialog()函数来初始化编辑框和按钮,并实现OnAddStudent()、OnLoadFromFile()和OnSaveToFile()函数来响应按钮的单击事件。
```c++
class CMyView : public CFormView
{
protected:
CMyView() noexcept;
DECLARE_DYNCREATE(CMyView)
// Form Data
public:
enum { IDD = IDD_MY_FORM };
CEdit m_editName;
CEdit m_editAge;
CEdit m_editMajor;
CEdit m_editGPA;
// Overrides
public:
virtual void OnDraw(CDC* /*pDC*/);
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
virtual BOOL OnInitDialog();
afx_msg void OnAddStudent();
afx_msg void OnLoadFromFile();
afx_msg void OnSaveToFile();
DECLARE_MESSAGE_MAP()
// Implementation
public:
virtual ~CMyView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
};
```
在实现OnInitDialog()中,我们需要将编辑框和按钮与类成员变量关联起来。
```c++
BOOL CMyView::OnInitDialog()
{
CFormView::OnInitDialog();
m_editName.SubclassDlgItem(IDC_EDIT_NAME, this);
m_editAge.SubclassDlgItem(IDC_EDIT_AGE, this);
m_editMajor.SubclassDlgItem(IDC_EDIT_MAJOR, this);
m_editGPA.SubclassDlgItem(IDC_EDIT_GPA, this);
return TRUE;
}
```
在实现OnAddStudent()中,我们需要从编辑框中读取学生信息,并将学生信息添加到文档类中。
```c++
void CMyView::OnAddStudent()
{
StudentInfo student;
m_editName.GetWindowText(student.name);
CString strAge;
m_editAge.GetWindowText(strAge);
student.age = _ttoi(strAge);
m_editMajor.GetWindowText(student.major);
CString strGPA;
m_editGPA.GetWindowText(strGPA);
student.gpa = _ttof(strGPA);
CMyDoc* pDoc = GetDocument();
pDoc->AddStudent(student);
m_editName.SetWindowText(_T(""));
m_editAge.SetWindowText(_T(""));
m_editMajor.SetWindowText(_T(""));
m_editGPA.SetWindowText(_T(""));
}
```
在实现OnLoadFromFile()中,我们需要打开文件对话框,选择要读取的文件,然后从文件中读取学生信息,并显示在编辑框中。
```c++
void CMyView::OnLoadFromFile()
{
CFileDialog dlg(TRUE, _T(".txt"), NULL, OFN_HIDEREADONLY | OFN_FILEMUSTEXIST, _T("Text Files (*.txt)|*.txt|Binary Files (*.bin)|*.bin|All Files (*.*)|*.*||"), this);
if (dlg.DoModal() == IDOK) {
CString fileName = dlg.GetPathName();
CMyDoc* pDoc = GetDocument();
if (pDoc->LoadFromFile(fileName)) {
CString message;
message.Format(_T("Loaded %d students from file."), pDoc->studentList.size());
AfxMessageBox(message);
if (!pDoc->studentList.empty()) {
const auto& student = pDoc->studentList.back();
m_editName.SetWindowText(student.name);
m_editAge.SetWindowText(CString().Format(_T("%d"), student.age));
m_editMajor.SetWindowText(student.major);
m_editGPA.SetWindowText(CString().Format(_T("%0.2f"), student.gpa));
}
}
else {
AfxMessageBox(_T("Failed to load students from file."));
}
}
}
```
在实现OnSaveToFile()中,我们需要打开文件对话框,选择要保存的文件,然后将学生信息保存到文件中。
```c++
void CMyView::OnSaveToFile()
{
CFileDialog dlg(FALSE, _T(".txt"), NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("Text Files (*.txt)|*.txt|Binary Files (*.bin)|*.bin|All Files (*.*)|*.*||"), this);
if (dlg.DoModal() == IDOK) {
CString fileName = dlg.GetPathName();
CMyDoc* pDoc = GetDocument();
if (pDoc->SaveToFile(fileName)) {
CString message;
message.Format(_T("Saved %d students to file."), pDoc->studentList.size());
AfxMessageBox(message);
}
else {
AfxMessageBox(_T("Failed to save students to file."));
}
}
}
```
现在我们已经完成了MFC应用程序的设计。我们可以编译并运行应用程序来测试它是否正常工作。
mfc中将文本文件中的姓名,账号,密码,身份证号,余额显示到对话框中。具体代码
以下是一个简单的 MFC 对话框应用程序,可以将文本文件中的姓名、账号、密码、身份证号和余额显示到对话框中:
首先,设计一个对话框,添加5个静态文本框和5个编辑框,用于显示和输入数据。
然后,在 .h 文件中添加以下代码:
```cpp
#pragma once
class CMyDlg : public CDialogEx
{
public:
CMyDlg(CWnd* pParent = nullptr); // 标准构造函数
virtual ~CMyDlg();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_MY_DIALOG };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
HICON m_hIcon;
// 生成的消息映射函数
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
private:
CString m_strName;
CString m_strAccount;
CString m_strPassword;
CString m_strID;
CString m_strBalance;
void LoadDataFromFile(LPCTSTR lpszFileName);
};
```
在 .cpp 文件中添加以下代码:
```cpp
#include "pch.h"
#include "My.h"
#include "MyDlg.h"
#include "afxdialogex.h"
#include <fstream>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CMyDlg 对话框
CMyDlg::CMyDlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_MY_DIALOG, pParent)
, m_strName(_T(""))
, m_strAccount(_T(""))
, m_strPassword(_T(""))
, m_strID(_T(""))
, m_strBalance(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
CMyDlg::~CMyDlg()
{
}
void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT_NAME, m_strName);
DDX_Text(pDX, IDC_EDIT_ACCOUNT, m_strAccount);
DDX_Text(pDX, IDC_EDIT_PASSWORD, m_strPassword);
DDX_Text(pDX, IDC_EDIT_ID, m_strID);
DDX_Text(pDX, IDC_EDIT_BALANCE, m_strBalance);
}
BEGIN_MESSAGE_MAP(CMyDlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
END_MESSAGE_MAP()
// CMyDlg 消息处理程序
BOOL CMyDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != nullptr)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
LoadDataFromFile(_T("data.txt"));
UpdateData(FALSE);
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CMyDlg::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 CMyDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CMyDlg::LoadDataFromFile(LPCTSTR lpszFileName)
{
std::ifstream ifs(lpszFileName);
if (!ifs)
{
AfxMessageBox(_T("打开文件失败!"));
return;
}
std::string strLine;
if (std::getline(ifs, strLine))
{
std::istringstream iss(strLine);
iss >> m_strName >> m_strAccount >> m_strPassword >> m_strID >> m_strBalance;
}
ifs.close();
}
```
在 LoadDataFromFile 函数中,首先使用 std::ifstream 打开指定的文件,如果打开失败,则弹出一个消息框提示用户。然后,使用 std::getline 从文件中读取一行数据,将其保存到 std::string 变量 strLine 中。接着,使用 std::istringstream 将 strLine 转换为 std::stringstream,使用 iss 从中读取姓名、账号、密码、身份证号和余额,将它们赋值给对应的 CString 变量。最后,关闭文件并返回。
在 OnInitDialog 函数中,调用 LoadDataFromFile 从文件中读取数据,并使用 UpdateData(FALSE) 将数据显示到对话框中。
注意:上述代码中读取文件的方式是基于文本文件的,如果文件是二进制文件,则需要使用不同的方式读取。此外,读取文件时应该使用异常处理机制,以避免程序崩溃。