VS 2022使用MFC编写FTP客户端

VS2022使用MFC编写FTP客户端

VS2022配置MFC环境

1.打开Visual Studio Installer

1.点击修改
在这里插入图片描述
2.选择使用C++的桌面开发,勾选红线部分
![在这里插入图片描述](https://img-blog.csdnimg.cn/a6b737a55fec425ab65db2d9b3a34e4c.png
3.选择完成之后,点击上方的“单个组件”
勾选红线部分
在这里插入图片描述
4.点击最右下角的修改,等待安装

2.创建MFC应用

1.创建新项目:
搜索找到MFC应用并点击下一步
在这里插入图片描述
2.配置MFC应用程序:
配置下列红线中的项目
在这里插入图片描述
3.修改对话框标题
这里随意修改自己想要的标题
在这里插入图片描述
4.勾选Windows套接字
在这里插入图片描述
5.之后点击右下角“完成”,等待十几秒

3.编写MFC项目

1.在工具箱中找到这次项目会用到的四个组件

在这里插入图片描述
没有工具箱的可以点击vs最上方的视图,在这一栏中找到工具箱。

2.将四个Button,一个Edit Control,一个List Box和若干个Static Text拖拽到Dialog文件当中。

在这里插入图片描述

3.右键每一个工具,然后点击属性,在属性当中配置一些相关信息

更改Edit Control的ID
在这里插入图片描述
然后更改ListBox的
ID改为IDC_ListBox

然后更改Button1的
ID改为IDC_Connect
描述文字改为连接

然后更改Button2的
ID改为IDC_Update
描述文字改为上传

然后更改Button3的
ID改为IDC_Delete
描述文字改为删除

然后更改Button4的
ID改为IDC_Download
描述文字改为下载
在这里插入图片描述

4.添加变量

右键每一个组件(Button&&EditControl&&ListBox)
然后选择添加变量
更改名称:
(为了方便,我将所有的名称都更改为:On+==控件ID的IDC_==的后半部分,这里不逐个演示了,看到下面这个例子就会懂)
在这里插入图片描述
更改完之后点击完成。

5.类向导

右键任意一个组件
然后选择类向导
在这里插入图片描述
点击这六个对象ID,然后点击添加到处理程序
然后点击确定

6.方法

在这里插入图片描述
将这些添加到方法,
以OnClickedConnect为例:
函数名为:For+Connect ,返回类型:void
在这里插入图片描述
然后点击确定

7.可能会出现的问题:

问题1.未定义表示符:
找到头文件MFC_优快云_TestDlg.cpp
添加头文件:

#include "resource.h"

2.然后再资源管理器中点击resource.h,出现什么都点确定

4.函数编写

1.添加头文件

在framework.h文件当中添加:

#include<afxinet.h>
#include<afxsock.h>
2.添加变量

在Dlg.cpp文件当中添加:

CInternetSession* m_pInetSession;
CFtpConnection* m_pFtpConnection;
CFtpFileFind* m_pRemoteFile;
CString m_pRemoteFileName, m_LocateFileName;
3.在Dlg.cpp文件中编写函数:

ForConnect函数:
注意
在这个函数中,传入的参数增加一个

CString m_IPadress
	CString m_strUser;
	CString m_strPass;
	m_strPass = "123456";//密码
	m_strUser = "sjc";//用户名
	m_pInetSession = new CInternetSession(AfxGetAppName(), 1, PRE_CONFIG_INTERNET_ACCESS);
	m_pFtpConnection = m_pInetSession->GetFtpConnection(m_IPadress, m_strUser, m_strPass);
	m_pRemoteFile = new CFtpFileFind(m_pFtpConnection);

ForDelete函数:

		CString selfile;
	OnListBox.GetText(OnListBox.GetCurSel(), selfile);
	if (!selfile.IsEmpty())
	{
		if (AfxMessageBox(L"确定要删除这个文件吗?", 4 + 48) == 6)
		{
			m_pFtpConnection->Remove(selfile);
		}
		CString strdir;
		m_pFtpConnection->GetCurrentDirectory(strdir);
		m_pInetSession->Close();
		this->OnClickedConnect();
		m_pFtpConnection->SetCurrentDirectory(strdir);
		this->OnSelchangeListbox();
	}

ForDownload函数:

	CString selfile;
	OnListBox.GetText(OnListBox.GetCurSel(), selfile);
	if (!selfile.IsEmpty())
	{
		CFileDialog file(FALSE, NULL, selfile, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, L"所有文件(*.*)|*.*|", this);
		if (file.DoModal() == IDOK)
		{
			CString strname;
			strname = file.GetFileName();
			CString strdir;
			m_pFtpConnection->GetCurrentDirectory(strdir);
			m_pFtpConnection->GetFile(selfile, strname);
			m_pInetSession->Close();
			this->OnClickedConnect();
			m_pFtpConnection->SetCurrentDirectory(strdir);
			this->ForListbox();
			AfxMessageBox(_T("下载成功!"));
		}
	}

ForUpdate函数:

	CString str;
	CString strname;
	CFileDialog file(true, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, L"所有文件(*.*)|*.*|", this);
	if (file.DoModal() == IDOK)
	{
		str = file.GetPathName();
		strname = file.GetFileName();

	}

	CString strdir;
	m_pFtpConnection->GetCurrentDirectory(strdir);
	BOOL bput = m_pFtpConnection->PutFile((LPCTSTR)str, (LPCTSTR)strname);
	if (bput)
	{
		m_pInetSession->Close();
		this->OnClickedConnect();
		m_pFtpConnection->SetCurrentDirectory(strdir);
		this->ForListbox();
		AfxMessageBox(_T("上传成功!"));
	}

ForListbox函数:

	OnListBox.ResetContent();
	CFtpFileFind ftpfind(m_pFtpConnection);
	CString strdirpath;
	m_pFtpConnection->GetCurrentDirectory(strdirpath);
	BOOL bfind = ftpfind.FindFile(strdirpath);
	while (bfind)
	{
		bfind = ftpfind.FindNextFile();
		CString strpath;
		if (ftpfind.IsDots())
		{
			continue;
		}
		if (!ftpfind.IsDirectory())
		{
			strpath = ftpfind.GetFileName();
			OnListBox.AddString(strpath);
		}
		else
		{
			strpath = ftpfind.GetFilePath();
			OnListBox.AddString(strpath);
		}
	}

OnClickedConnect()函数:

	CString Adress;
	GetDlgItemText(IDC_IPINPUT, Adress);
	ForConnect(Adress);
	this->ForListbox();

OnClickedUpdate()函数:

	this->ForUpdate();
	this->ForListbox();

OnClickedDownload()函数:

	this->ForDownload();
	this->ForListbox();

OnClickedDelete()函数:

	this->ForDelete();
	this->ForListbox();

5.测试

连接成功
在这里插入图片描述

### Visual Studio 2022MFC 开发指南及相关问题 Visual Studio 2022 是微软推出的最新版本集成开发环境 (IDE),支持多种编程语言和技术框架,其中包括 Microsoft Foundation Classes (MFC)[^1]。MFC 是一种用于简化 Windows 应用程序开发的 C++ 类库,在早期阶段广泛应用于构建图形用户界面 (GUI) 的应用程序。 #### 使用 VS2022 进行 MFC 开发的关键点 在 Visual Studio 2022 中继续支持 MFC 开发,并提供了许多工具来帮助开发者更高效地创建和维护基于 MFC 的项目。以下是几个重要的方面: - **代码生成器的支持**: 在过去的实现中,MFC 的复杂性和底层细节被隐藏在自动生成的代码背后。这种模式延续到了现代 IDE 版本中,尽管自动化程度有所提升,但仍需注意理解这些生成代码的功能及其潜在影响。 - **Windows 移动应用设计准则**: 对于涉及移动设备的应用场景,可以参考一些通用的设计原则[^2]。虽然主要针对较旧平台制定,但其中关于性能优化、资源管理等方面的理念仍然适用。 - **社区贡献与文档共享**: 像 优快云 博客这样的在线资源提供了一个交流学习的好地方[^3]。通过查阅类似的第三方文章或者官方文档可以获得具体案例分析以及常见错误解决方案。 下面展示如何设置一个新的 MFC 工程实例: ```cpp // Example of creating an empty MFC project in Visual Studio 2022. #include <afxwin.h> // MFC core and standard components class CMFCTestApp : public CWinApp { public: virtual BOOL InitInstance() override; }; BOOL CMFCTestApp::InitInstance() { m_pMainWnd = new CFrameWnd(); m_pMainWnd->Create(NULL, _T("Test Window")); m_pMainWnd->ShowWindow(SW_SHOW); m_pMainWnd->UpdateWindow(); return TRUE; } CMFCTestApp theApp; int main(){ return theApp.Run(); } ``` 此示例展示了基本结构初始化过程,实际项目可能更加复杂并包含更多功能模块定义。 ---
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

R丿清风不解语

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值