24 MFC文档串行化和单文档应用程序

这篇具有很好参考价值的文章主要介绍了24 MFC文档串行化和单文档应用程序。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

文档串行化

ui 设计
24 MFC文档串行化和单文档应用程序,mfc,restful,c++24 MFC文档串行化和单文档应用程序,mfc,restful,c++

保存

void CfileDemoDlg::OnBnClickedBtnSave()
{
	UpdateData();
	//CFile file(L"Demo.dat",CFile::modeCreate|CFile::modeWrite);
	第一个参数:文件指针
	第二个参数:模式
	//CArchive ar(&file, CArchive::store);
	//ar << m_strName;
	//ar << m_strSex;
	//ar << m_nAge;
	//ar << m_nHeight;
	//ar << m_fWeight;
	//ar << m_fSalary;
	//ar.Close();
	//file.Close();
	//m_doc.OnNewDocument();//创建一个新文档
	m_doc.OnSaveDocument(L"Demo.dat");//保存
}

读取

void CfileDemoDlg::OnBnClickedBtnRead()
{
	//CFile file(L"Demo.dat",  CFile::modeRead);
	//CArchive ar(&file, CArchive::load);
	注意:一定要按照写入顺序读取,顺序不能乱
	//ar >> m_strName;
	//ar >> m_strSex;
	//ar >> m_nAge;
	//ar >> m_nHeight;
	//ar >> m_fWeight;
	//ar >> m_fSalary;
	//ar.Close();
	//file.Close();
	m_doc.OnOpenDocument(L"Demo.dat");//打开一个文档
	UpdateData(FALSE);//显示
}

定义一个文档类

#pragma once
#include <afxwin.h>
class CMyDoc :public CDocument
{
	CMyDoc();
};

添加虚函数判断是否保存还是读取
24 MFC文档串行化和单文档应用程序,mfc,restful,c++

void CMyDoc::Serialize(CArchive& ar)
{
	CfileDemoDlg* pMainDlg=(CfileDemoDlg*)AfxGetMainWnd();//获取主对话框
	pMainDlg->Serialize(ar);
}

主对话框内添加Serialize虚函数
24 MFC文档串行化和单文档应用程序,mfc,restful,c++

void CfileDemoDlg::Serialize(CArchive& ar)
{
	if (ar.IsStoring())//保存
	{
		//第一个参数:文件指针
		//第二个参数:模式
		//CArchive ar(&file, CArchive::store);
		ar << m_strName;
		ar << m_strSex;
		ar << m_nAge;
		ar << m_nHeight;
		ar << m_fWeight;
		ar << m_fSalary;
	}
	else//读取
	{
		ar >> m_strName;
		ar >> m_strSex;
		ar >> m_nAge;
		ar >> m_nHeight;
		ar >> m_fWeight;
		ar >> m_fSalary;
	}
}

全部代码

// fileDemoDlg.h: 头文件

//

#pragma once
#include "CMyDoc.h"

// CfileDemoDlg 对话框
class CfileDemoDlg : public CDialogEx
{
// 构造
public:
	CfileDemoDlg(CWnd* pParent = nullptr);	// 标准构造函数

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_FILEDEMO_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()
public:
	CString m_strName;
	CString m_strSex;
	UINT m_nAge;
	UINT m_nHeight;
	float m_fWeight;
	float m_fSalary;
	CMyDoc m_doc;
	afx_msg void OnBnClickedBtnSave();
	afx_msg void OnBnClickedBtnRead();
	virtual void Serialize(CArchive& ar);
};
// fileDemoDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "fileDemo.h"
#include "fileDemoDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CfileDemoDlg 对话框



CfileDemoDlg::CfileDemoDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_FILEDEMO_DIALOG, pParent)
	, m_nAge(0)
	, m_nHeight(0)
	, m_fWeight(0)
	, m_fSalary(0)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CfileDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT1, m_strName);
	DDX_Text(pDX, IDC_EDIT2, m_strSex);
	DDX_Text(pDX, IDC_EDIT3, m_nAge);
	DDX_Text(pDX, IDC_EDIT4, m_nHeight);
	DDX_Text(pDX, IDC_EDIT5, m_fWeight);
	DDX_Text(pDX, IDC_EDIT6, m_fSalary);
}

BEGIN_MESSAGE_MAP(CfileDemoDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_SAVE, &CfileDemoDlg::OnBnClickedBtnSave)
	ON_BN_CLICKED(IDC_BTN_READ, &CfileDemoDlg::OnBnClickedBtnRead)
END_MESSAGE_MAP()


// CfileDemoDlg 消息处理程序

BOOL CfileDemoDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码

	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。

void CfileDemoDlg::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 CfileDemoDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


//保存
void CfileDemoDlg::OnBnClickedBtnSave()
{
	UpdateData();
	//CFile file(L"Demo.dat",CFile::modeCreate|CFile::modeWrite);

	第一个参数:文件指针
	第二个参数:模式
	//CArchive ar(&file, CArchive::store);
	//ar << m_strName;
	//ar << m_strSex;
	//ar << m_nAge;
	//ar << m_nHeight;
	//ar << m_fWeight;
	//ar << m_fSalary;
	//ar.Close();
	
	//file.Close();
	//m_doc.OnNewDocument();//创建一个新文档

	m_doc.OnSaveDocument(L"Demo.dat");//保存

	
}


//读取
void CfileDemoDlg::OnBnClickedBtnRead()
{
	//CFile file(L"Demo.dat",  CFile::modeRead);
	//CArchive ar(&file, CArchive::load);
	注意:一定要按照写入顺序读取,顺序不能乱
	//ar >> m_strName;
	//ar >> m_strSex;
	//ar >> m_nAge;
	//ar >> m_nHeight;
	//ar >> m_fWeight;
	//ar >> m_fSalary;
	//ar.Close();
	//file.Close();
	
	m_doc.OnOpenDocument(L"Demo.dat");//打开一个文档
	UpdateData(FALSE);//显示
}


void CfileDemoDlg::Serialize(CArchive& ar)
{
	if (ar.IsStoring())//保存
	{
		//第一个参数:文件指针
		//第二个参数:模式
		//CArchive ar(&file, CArchive::store);
		ar << m_strName;
		ar << m_strSex;
		ar << m_nAge;
		ar << m_nHeight;
		ar << m_fWeight;
		ar << m_fSalary;

	}
	else//读取
	{
		ar >> m_strName;
		ar >> m_strSex;
		ar >> m_nAge;
		ar >> m_nHeight;
		ar >> m_fWeight;
		ar >> m_fSalary;
	}
}
#pragma once
#include <afxwin.h>
class CMyDoc :public CDocument
{
public:
	CMyDoc();
public:
	virtual void Serialize(CArchive& ar);
};
#include "pch.h"
#include "CMyDoc.h"
#include "fileDemoDlg.h"

CMyDoc::CMyDoc()
{
}
void CMyDoc::Serialize(CArchive& ar)
{
	CfileDemoDlg* pMainDlg=(CfileDemoDlg*)AfxGetMainWnd();//获取主对话框
	pMainDlg->Serialize(ar);
}

单文档应用程序搭建原理

单文档应用程序(SDI应用程序)是一种基于文档的应用程序,它允许用户打开、编辑和保存单个文档。这种类型的应用程序通常用于文本编辑器、图像编辑器、绘图工具等。

在单文档应用程序中,用户可以打开一个文档,对其进行编辑并将其保存。当用户打开同一个文档时,应用程序会将其加载到内存中供用户编辑。用户可以对文档进行各种操作,如添加、删除、修改和移动文档中的内容。

与多文档应用程序(MDI应用程序)不同,单文档应用程序只允许用户同时打开一个文档。这意味着用户必须先关闭当前文档,然后才能打开另一个文档。

属性设置
24 MFC文档串行化和单文档应用程序,mfc,restful,c++

搭建框架

24 MFC文档串行化和单文档应用程序,mfc,restful,c++
24 MFC文档串行化和单文档应用程序,mfc,restful,c++

Win32 过度到MFC 三部曲

CSDIApp 类

1.建立CWinApp 派生类

class CSDIApp :public CWinApp
{
	DECLARE_DYNCREATE(CSDIApp)  //声明动态创建
public:
	BOOL InitInstance();
};

2.重写CWinAPP
3.定义CWinApp派生类的全局变量

#include "stdafx.h"
#include "SDI.h"
#include "CMainFrame.h"

CSDIApp theApp;//定义CWinApp派生类的全局变量
IMPLEMENT_DYNAMIC(CSDIApp, CWinApp) //实现动态创建
BOOL CSDIApp::InitInstance()
{
	//CMainFrame* pFrame = new CMainFrame;
	//pFrame->Create(NULL, L"框架程序");
	//m_pMainWnd = pFrame;
	//pFrame->ShowWindow(SW_SHOW);
	//单文档
	CSingleDocTemplate* pDocTemplate = new CSingleDocTemplate(
		IDR_MAINFRAME,//所有资源ID
		RUNTIME_CLASS(CMainDoc),//文档类
		RUNTIME_CLASS(CMainFrame),//主窗口框架啊
		RUNTIME_CLASS(CMainView)//视图类
	);

	//添加到文档模板对象中
	AddDocTemplate(pDocTemplate);
	//解析命令行信息
	CCommandLineInfo cmdLineInfo;
	ParseCommandLine(cmdLineInfo);

	//执行命令行命令
	if (FALSE == ProcessShellCommand(cmdLineInfo))
		return FALSE;

	//显示窗口
	m_pMainWnd->ShowWindow(SW_SHOW);
	m_pMainWnd->UpdateWindow();


	return TRUE;
}

MFC 框架类

CMainFrame .h

class CMainFrame :public CFrameWnd 
{
	DECLARE_DYNCREATE(CMainFrame)  //声明动态创建
public:
	CMainFrame();
};

CMainFrame .cpp

#include "stdafx.h"
#include "CMainFrame.h"


IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd) //实现动态创建

CMainFrame::CMainFrame()
{
}

完成框架
24 MFC文档串行化和单文档应用程序,mfc,restful,c++24 MFC文档串行化和单文档应用程序,mfc,restful,c++

设置ID资源

设置菜单
24 MFC文档串行化和单文档应用程序,mfc,restful,c++设置图标
名字统一24 MFC文档串行化和单文档应用程序,mfc,restful,c++
添加快捷键
24 MFC文档串行化和单文档应用程序,mfc,restful,c++24 MFC文档串行化和单文档应用程序,mfc,restful,c++
添加字符串
24 MFC文档串行化和单文档应用程序,mfc,restful,c++24 MFC文档串行化和单文档应用程序,mfc,restful,c++工具栏资源
24 MFC文档串行化和单文档应用程序,mfc,restful,c++

创建主文档类

#pragma once
#include "stdafx.h"
class CMainDoc :public CDocument
{
	DECLARE_DYNCREATE(CMainDoc)  //声明动态创建
public:
	CMainDoc();
};
#include "CMainDoc.h"
#include "stdafx.h"
IMPLEMENT_DYNAMIC(CMainDoc,CDocument) //实现动态创建
CMainDoc::CMainDoc()
{
}

创建视图类

#pragma once
#include <afxwin.h>
class CMainView :public CView
{
		DECLARE_DYNCREATE(CMainView)  //声明动态创建
public:
	CMainView();
};
#include "CMainView.h"
#include "stdafx.h"
IMPLEMENT_DYNAMIC(CMainView, CView) //实现动态创建
CMainView::CMainView()
{
}

24 MFC文档串行化和单文档应用程序,mfc,restful,c++

全部代码

CMainDoc.h

#pragma once
#include "stdafx.h"
class CMainDoc :public CDocument
{
	DECLARE_DYNCREATE(CMainDoc)  //声明动态创建
public:
	CMainDoc();
};

CMainDoc.cpp

#include "CMainDoc.h"
#include "stdafx.h"

IMPLEMENT_DYNAMIC(CMainDoc,CDocument) //实现动态创建

CMainDoc::CMainDoc()
{
}

CMainFrame.h

#pragma once
class CMainFrame :public CFrameWnd 
{
	DECLARE_DYNCREATE(CMainFrame)  //声明动态创建
public:
	CMainFrame();
};

CMainFrame.cpp

#include "stdafx.h"
#include "CMainFrame.h"

IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd) //实现动态创建

CMainFrame::CMainFrame()
{
}

CMainView.h

#pragma once
#include "stdafx.h"
class CMainView :public CView
{
	DECLARE_DYNCREATE(CMainView)  //声明动态创建
public:
	CMainView();
	virtual void OnDraw(CDC* /*pDC*/);
};

CMainView.cpp

#include "CMainView.h"
#include "stdafx.h"
IMPLEMENT_DYNAMIC(CMainView, CView) //实现动态创建
CMainView::CMainView()
{
}


void CMainView::OnDraw(CDC* /*pDC*/)
{
	// TODO: 在此添加专用代码和/或调用基类
}

CMainView.h

class CSDIApp :public CWinApp
{
	DECLARE_DYNCREATE(CSDIApp)  //声明动态创建
public:

	BOOL InitInstance();
};

CMainView.cpp

#include "stdafx.h"
#include "SDI.h"
#include "CMainFrame.h"
#include "CMainDoc.h"
#include "CMainView.h"
CSDIApp theApp;
IMPLEMENT_DYNAMIC(CSDIApp, CWinApp) //实现动态创建

BOOL CSDIApp::InitInstance()
{
	/*CMainFrame* pFrame = new CMainFrame;
	pFrame->Create(NULL, L"框架程序");
	m_pMainWnd = pFrame;
	pFrame->ShowWindow(SW_SHOW);*/

	//单文档
	CSingleDocTemplate* pDocTemplate = new CSingleDocTemplate(
		IDR_MAINFRAME,//所有资源ID
		RUNTIME_CLASS(CMainDoc),//文档类
		RUNTIME_CLASS(CMainFrame),//主窗口框架啊
		RUNTIME_CLASS(CMainView)//视图类
	);

	//添加到文档模板对象中
	AddDocTemplate(pDocTemplate);
	//解析命令行信息
	CCommandLineInfo cmdLineInfo;
	ParseCommandLine(cmdLineInfo);

	//执行命令行命令
	if (FALSE == ProcessShellCommand(cmdLineInfo))
		return FALSE;

	//显示窗口
	m_pMainWnd->ShowWindow(SW_SHOW);
	m_pMainWnd->UpdateWindow();
	return TRUE;
}

单文档应用程序

设置标题

24 MFC文档串行化和单文档应用程序,mfc,restful,c++

设置窗口名字
24 MFC文档串行化和单文档应用程序,mfc,restful,c++

BOOL CSingleDoc::OnNewDocument()
{
	SetTitle(L"名字");

	return CDocument::OnNewDocument();
}

24 MFC文档串行化和单文档应用程序,mfc,restful,c++

设置窗口参数,去除多余标题
24 MFC文档串行化和单文档应用程序,mfc,restful,c++

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	cs.cx = 800;
	cs.cy = 600;
	cs.style = cs.style &~FWS_ADDTOTITLE;//去掉风格
	return CFrameWnd::PreCreateWindow(cs);
}

24 MFC文档串行化和单文档应用程序,mfc,restful,c++24 MFC文档串行化和单文档应用程序,mfc,restful,c++

绘图

在View 中绘图
24 MFC文档串行化和单文档应用程序,mfc,restful,c++24 MFC文档串行化和单文档应用程序,mfc,restful,c++在frame中绘图
24 MFC文档串行化和单文档应用程序,mfc,restful,c++

结论:视图在框架之上

简单的管理系统

视图界面继承CListView
24 MFC文档串行化和单文档应用程序,mfc,restful,c++

24 MFC文档串行化和单文档应用程序,mfc,restful,c++
重写虚函数
24 MFC文档串行化和单文档应用程序,mfc,restful,c++

24 MFC文档串行化和单文档应用程序,mfc,restful,c++

void CMangerSystmView::OnInitialUpdate()
{
	CListView::OnInitialUpdate();

	m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
	m_list.ModifyStyle(0, LVS_REPORT|LVS_SHOWSELALWAYS);//设置风格
	m_list.InsertColumn(0,L"ID",LVCFMT_LEFT,100);
	m_list.InsertColumn(0, L"姓名", LVCFMT_LEFT, 100);
	m_list.InsertColumn(0, L"性别", LVCFMT_LEFT, 100);
	m_list.InsertColumn(0, L"年龄", LVCFMT_LEFT, 100);
}

设置菜单24 MFC文档串行化和单文档应用程序,mfc,restful,c++
24 MFC文档串行化和单文档应用程序,mfc,restful,c++添加窗口
24 MFC文档串行化和单文档应用程序,mfc,restful,c++添加类
24 MFC文档串行化和单文档应用程序,mfc,restful,c++添加控件变量
24 MFC文档串行化和单文档应用程序,mfc,restful,c++

添加

void CMangerSystmView::OnAdd()
{
	CAddDlg addDlg;
	if (IDCANCEL == addDlg.DoModal())
		return;
	int nCount = m_list.GetItemCount();//获取总行数
	CString str;
	str.Format(L"%d",addDlg.m_nID);
	m_list.InsertItem(nCount,str);
	m_list.SetItemText(nCount, 1, addDlg.m_strName);
	m_list.SetItemText(nCount, 2, addDlg.m_strSex);
	str.Format(L"%d", addDlg.m_nAge);
	m_list.SetItemText(nCount, 3, str);
}

24 MFC文档串行化和单文档应用程序,mfc,restful,c++

删除

void CMangerSystmView::OnDelete()
{
	POSITION pos = m_list.GetFirstSelectedItemPosition();//获取相中的位置
	int nSel=m_list.GetNextSelectedItem(pos);
	CString str;
	str.Format(L"%d", nSel);
	SetWindowText(str);
	if (nSel < 0)
	{
		MessageBox(L"请选中后在删除");
		return;
	}
	
	if (IDYES == MessageBox(L"确定要删除吗", L"提示", MB_YESNO))
	{
		m_list.DeleteItem(nSel);
	}
}

修改
24 MFC文档串行化和单文档应用程序,mfc,restful,c++24 MFC文档串行化和单文档应用程序,mfc,restful,c++

void CMangerSystmView::OnModify()
{
	CModifyDlg dlg;
	POSITION pos = m_list.GetFirstSelectedItemPosition();//获取相中的位置
	int nSel = m_list.GetNextSelectedItem(pos);//获取当前选项

	if (nSel < 0)
	{
		MessageBox(L"请选中后在修改");
		return;
	}
	dlg.m_nID=_wtoi(m_list.GetItemText(nSel, 0 ));
	dlg.m_strName = m_list.GetItemText(nSel, 1);
	dlg.m_strSex= m_list.GetItemText(nSel, 2);
	dlg.m_nAge = _wtoi(m_list.GetItemText(nSel, 3));
	if (IDCANCEL == dlg.DoModal())
	{
		return;
	}
	CString str;
	str.Format(L"%d", dlg.m_nID);
	m_list.SetItemText(nSel, 0,str);
	m_list.SetItemText(nSel, 1, dlg.m_strName);
	m_list.SetItemText(nSel, 2, dlg.m_strSex);
	str.Format(L"%d", dlg.m_nAge);
	m_list.SetItemText(nSel, 3, str);

}

添加快捷键
查看菜单ID24 MFC文档串行化和单文档应用程序,mfc,restful,c++>设置工具栏
24 MFC文档串行化和单文档应用程序,mfc,restful,c++24 MFC文档串行化和单文档应用程序,mfc,restful,c++

24 MFC文档串行化和单文档应用程序,mfc,restful,c++

部分代码

//MangerSystmView.h

public:
	virtual void OnInitialUpdate();
	afx_msg void OnAdd();
	afx_msg void OnDelete();
	afx_msg void OnModify();
	CListCtrl& m_list ;

// MangerSystmView.cpp: CMangerSystmView 类的实现文章来源地址https://www.toymoban.com/news/detail-581998.html

//

#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义,并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "MangerSystm.h"
#endif

#include "MangerSystmDoc.h"
#include "MangerSystmView.h"

#include "CAddDlg.h"
#include "CModifyDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMangerSystmView

IMPLEMENT_DYNCREATE(CMangerSystmView, CListView)

BEGIN_MESSAGE_MAP(CMangerSystmView, CListView)
	// 标准打印命令
	ON_COMMAND(ID_FILE_PRINT, &CListView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CListView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CListView::OnFilePrintPreview)
	ON_COMMAND(ID_ADD, &CMangerSystmView::OnAdd)
	ON_COMMAND(ID_DELETE, &CMangerSystmView::OnDelete)
	ON_COMMAND(ID_MODIFY, &CMangerSystmView::OnModify)
END_MESSAGE_MAP()

// CMangerSystmView 构造/析构

CMangerSystmView::CMangerSystmView():m_list(GetListCtrl())
{
	// TODO: 在此处添加构造代码

}

CMangerSystmView::~CMangerSystmView()
{
}

BOOL CMangerSystmView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式

	return CListView::PreCreateWindow(cs);
}

void CMangerSystmView::OnInitialUpdate()
{
	CListView::OnInitialUpdate();

	m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
	m_list.ModifyStyle(0, LVS_REPORT|LVS_SHOWSELALWAYS);//设置风格
	m_list.InsertColumn(0,L"ID",LVCFMT_LEFT,100);
	m_list.InsertColumn(0, L"姓名", LVCFMT_LEFT, 100);
	m_list.InsertColumn(0, L"性别", LVCFMT_LEFT, 100);
	m_list.InsertColumn(0, L"年龄", LVCFMT_LEFT, 100);
}


void CMangerSystmView::OnAdd()
{
	CAddDlg addDlg;
	if (IDCANCEL == addDlg.DoModal())
		return;
	int nCount = m_list.GetItemCount();//获取总行数
	CString str;
	str.Format(L"%d",addDlg.m_nID);
	m_list.InsertItem(nCount,str);
	m_list.SetItemText(nCount, 1, addDlg.m_strName);
	m_list.SetItemText(nCount, 2, addDlg.m_strSex);
	str.Format(L"%d", addDlg.m_nAge);
	m_list.SetItemText(nCount, 3, str);
}


void CMangerSystmView::OnDelete()
{
	POSITION pos = m_list.GetFirstSelectedItemPosition();//获取相中的位置
	int nSel=m_list.GetNextSelectedItem(pos);
	CString str;
	str.Format(L"%d", nSel);
	SetWindowText(str);
	if (nSel < 0)
	{
		MessageBox(L"请选中后在删除");
		return;
	}
	
	if (IDYES == MessageBox(L"确定要删除吗", L"提示", MB_YESNO))
	{
		m_list.DeleteItem(nSel);
	}
}

//修改
void CMangerSystmView::OnModify()
{
	CModifyDlg dlg;
	POSITION pos = m_list.GetFirstSelectedItemPosition();//获取相中的位置
	int nSel = m_list.GetNextSelectedItem(pos);//获取当前选项

	if (nSel < 0)
	{
		MessageBox(L"请选中后在修改");
		return;
	}

	dlg.m_nID=_wtoi(m_list.GetItemText(nSel, 0 ));
	dlg.m_strName = m_list.GetItemText(nSel, 1);
	dlg.m_strSex= m_list.GetItemText(nSel, 2);
	dlg.m_nAge = _wtoi(m_list.GetItemText(nSel, 3));
	if (IDCANCEL == dlg.DoModal())
	{
		return;
	}
	CString str;
	str.Format(L"%d", dlg.m_nID);
	m_list.SetItemText(nSel, 0,str);
	m_list.SetItemText(nSel, 1, dlg.m_strName);
	m_list.SetItemText(nSel, 2, dlg.m_strSex);
	str.Format(L"%d", dlg.m_nAge);
	m_list.SetItemText(nSel, 3, str);

}

到了这里,关于24 MFC文档串行化和单文档应用程序的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 使用LabVIEW打开默认应用程序中的文档(PDF,Word,Excel,Html)

    问题详情 我想让我的LabVIEW VI使用默认应用程序打开硬盘上的文档。如何实现? 解决方案 有一个名为 \\\"Open a Document on Disk.vi\\\" 的 VI,它可以在操作系统注册的默认应用程序中打开任何文档(Word、Excel、HTML 等)。该 VI 不在面板上,而是位于 LabVIEWvi.libPlatformbrowser.llbOpen a Doc

    2024年03月12日
    浏览(42)
  • 解锁高质量文档转换:通过Java应用程序免费将PDF转换为XPS

    在当今数字时代,文档转换已经成为我们日常生活中不可或缺的一部分。然而,不是所有的文件格式都是平等无差的,在与他人共享文档时可能会遇到兼容性问题。这就是我们需要进行文件格式转换的原因,而将PDF转换为XPS是一种受欢迎的转换选择。如果你正在寻找一种使用

    2024年02月11日
    浏览(39)
  • word2003,出现Microsoft Visual Basic对话框,并显示警告“该工程中的宏被禁止。请参阅联机帮助或主应用程序的文档来决定如何激活宏。”的解决方案

    当打开word2003时候,出现Microsoft Visual Basic对话框,并显示警告“该工程中的宏被禁止。请参阅联机帮助或主应用程序的文档来决定如何激活宏。”,如图1和图2所示。 图1 word 2003的宏被禁止界面(word界面和警告对话框) 图2 word 2003的宏被禁止界面(警告对话框) 图3 宏禁止解

    2024年02月06日
    浏览(39)
  • 保护您的Android应用程序:Android应用程序安全一览

    我们都知道Android是为所有人设计的——开放、面向开发者、面向用户,这种开放性为今天和明天的移动技术提供了很多便利。然而,开放性也带来了需要妥善处理的安全风险。 安全是我们所有人都关注的重要问题。无论是我们的个人数据、电子邮件、电话号码、凭证、服务

    2024年02月03日
    浏览(53)
  • Flutter中的Web应用程序开发:构建现代Web应用程序

    作者:禅与计算机程序设计艺术 作为人工智能专家,程序员和软件架构师,CTO,我今天将为大家分享有关 Flutter 中 Web 应用程序开发的见解。在这篇文章中,我们将深入探讨 Flutter Web 应用程序的开发过程、技术原理以及最佳实践。 引言 随着移动设备的普及,Web 应用程序在全

    2024年02月12日
    浏览(63)
  • 软件提示应用程序无法启动,因为应用程序的并行配置不正确……

    刚刚新买了电脑,把以前电脑上的绿色版精简迅雷.exe复制到新电脑上没想到提示: 应用程序无法启动,因为应用程序的并行配置不正确。请参阅应用程序事件日志,或使用命令行sxstrace.exe工具 如下图: 经过一番研究,解决步骤如下: 常见的运行库有如下很多: Microsoft Vi

    2024年02月13日
    浏览(63)
  • 基于C#的画图应用程序(窗体应用程序)C#组件、控件

      使用VS2019,建立窗体应用程序,借助一系列控件实现画图功能。   结合组件与控件的使用。 目录 一、预备知识 1、什么是C#的组件和控件 (1)共同点 (2)不同点 二、主要步骤 1、软件以及环境 2、设置接口 3、界面设计 4、编写代码 5、编译程序和运行 三、编写代码 1、设

    2023年04月08日
    浏览(45)
  • GPT应用程序的行业应用

    GPT(Generative Pre-trained Transformer)应用程序在各个行业都有广泛的应用潜力,其自然语言生成的能力使其适用于多种场景。以下是一些行业中常见的GPT应用,希望对大家有所帮助。北京木奇移动技术有限公司,专业的软件外包开发公司,欢迎交流合作。 1.教育: 智能辅导和学

    2024年01月23日
    浏览(29)
  • 电脑“应用程序无法启动,因为应用程序的并行配置不正确......“问题的解决方法

    问题 : win10某天开机发现绝大部分exe都无法运行或安装,错误信息提示如下: “应用程序无法启动,因为应用程序的并行配置不正确。有关详细信息,请参阅应用程序事件日志,或使用命令行 sxstrace.exe 工具。” 排查过程 :命令行 sxstrace.exe 工具无法正常打开 查看应用程序

    2024年02月04日
    浏览(57)
  • 【完美解决】应用程序无法正常启动(0xc000007b)请单击“确定”关闭应用程序

    年期安装CorelDRAW X8 (64-Bit),安装完成之后运行一点毛病都没有,可是过了两三个月,再打开就出现“应用程序无法正常启动(0xc000007b)请单击“确定”关闭应用程序”这个提示框,如下图示 出现这个问题我就上网查找,无非四种操作,我试了Directx可以了,下面统一记录下 在键

    2024年02月04日
    浏览(37)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包