IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> MFC使用小结 -> 正文阅读

[C++知识库]MFC使用小结

获取可编辑框内值

	CString str;
	GetDlgItemText(IDC_COMBO_BOARDID,str);///这儿就是取该输入框的值,并赋给str;
	MessageBox(str);//弹出对话框显示str的值。

MFC-TXT文件-创建文件+写入数据+读取数据

写入txt
	CString pszFileName=filePath+"RubberFile.txt";
	CStdioFile myFile;
	CFileException fileException;	
	if(myFile.Open(pszFileName,CFile::typeText|CFile::modeCreate|CFile::modeReadWrite),&fileException)
	{
	CString strOrder;
	strOrder.Format("%.4f,%.4f",tempVariables.RingRadius.D(),tempVariables.RubberRadius.D()); 
	myFile.WriteString(strOrder);
	}
	else
	{
	TRACE("Can't open file %s,error=%u\n",pszFileName,fileException.m_cause);
	}

	myFile.Close();

读取txt
if(myFile.Open(pszFileName,CFile::typeText|CFile::modeReadWrite),&fileException)
{
	myFile.SeekToBegin();
	CString str1;
	myFile.ReadString(str1);
	/*CString str2;
	myFile.ReadString(str2);*/
	AfxMessageBox(str1);
}
else
{
	TRACE("Can't open file %s,error=%u\n",pszFileName,fileException.m_cause);
}
myFile.Close();

编辑框输入值-获取

	CString pass;
	GetDlgItemText(IDC_PASSWORD,pass);

参数保存和参数读取-结构体方式

typedef struct
{
    int a;
    int b;
    float c;
}param_struct;


param_struct		m_Param;

//获取当前可执行程序的路径,并添加Data目录
CString CCoolProgram::GetModuleDir() 
{
	HMODULE module = GetModuleHandle(0); 
	WCHAR pFileName[MAX_PATH]; 
	GetModuleFileName(module, pFileName, MAX_PATH); 

	CString csFullPath(pFileName); 
	int nPos = csFullPath.ReverseFind( _T('\\') ); 
	csFullPath = csFullPath.Left( nPos ); 
	csFullPath += _T("\\Data\\");
	return csFullPath;
}


//可执行文件的路径
	CString				m_Path;

//获取当前可执行程序的路径 + 参数路径
	m_Path = GetModuleDir();



//保存参数
void SaveParam()
{
	CFile File;
	CFileFind ff;
	CString strFileName;
	strFileName = g_pMainDlg->m_Path + L"Params.data";
	if( ff.FindFile( strFileName ) == FALSE )
	{
		AfxMessageBox(L"参数文件 Params.data 找不到,系统将新建参数文件");
	}

	if( File.Open( strFileName, CFile::modeCreate | CFile::modeWrite ) )
	{
		File.Write( &g_pMainDlg->m_Param, sizeof( param_struct ) );
		File.Close();
	}
}


****************************************
SaveRadio(filePath + "Radio.data");//保存
****************************************
LoadRadio(filePath + "Radio.data");//加载
***********************************************
//读取参数
void LoadParam()
{
	CFile File;
	CFileFind ff;
	CString strFileName;
	strFileName = g_pMainDlg->m_Path + L"Params.data";

	if ( ff.FindFile(strFileName)  )
	{
		if (File.Open( strFileName,CFile::modeRead))
		{
			File.Read( &g_pMainDlg->m_Param, sizeof( param_struct ) );
			File.Close();
		}
		else
		{
			AfxMessageBox(L"打开参数文件 Params.data 失败");
		}
	}
	else
	{
		AfxMessageBox(L"参数文件 Params.data 找不到,系统将新建参数文件,软件启动后请重新设置参数");

		if( File.Open( strFileName, CFile::modeCreate | CFile::modeWrite ) )
		{
			File.Write( &g_pMainDlg->m_Param, sizeof( param_struct ) );
			File.Close();
		}
	}
}

MFC-改变焦点

GetDlgItem(IDC_EDIT_CURFLAW)->SetFocus();//IDC_EDIT_CURFLAW——目标控件ID

MFC-路径-获得exe文件所在路径

TCHAR buf[256]={0};
DWORD dwResult = GetModuleFileName(AfxGetApp()->m_hInstance,buf,256);
CString exePath = buf;
exePath = exePath.Left(exePath.ReverseFind('\\'));
AfxMessageBox(exePath);

MFC-文件-遍历

void CfileTestDlg::BayesCategoryTest(CString &dir)
{
	   CFileFind fileFinder;
	   if(dir.Right(1)!="\\")    
	      dir +="\\";
           CString filePath = dir + _T("*.*");
      
	   BOOL bFinished = fileFinder.FindFile(filePath);该函数返回值非0 还有符合条件的文件, 0表示是最后一个文件。
	   while(bFinished)  //每次循环对应一个类别目录
	   {
		bFinished = fileFinder.FindNextFile();
		if(!fileFinder.IsDirectory() && (!fileFinder.IsDots())) //若是文件
		{
                   	//获取文件类型
			CString fileName = fileFinder.GetFileName();
			int dotPos=fileName.ReverseFind('.');
			CString fileExt=fileName.Right(fileName.GetLength()-dotPos);
			if(fileExt == ".bmp")  //若是bmp文件则开始分类测试
			{
				CString category = fileName.Left(fileName.GetLength()-8);  //应属类
			}
		}
		else if(fileFinder.IsDirectory() && (!fileFinder.IsDots()))  //若是目录则递归调用此方法
		{
			BayesCategoryTest(fileFinder.GetFilePath(),vec);
		}
	   }
           fileFinder.Close();
}

MFC-文件夹-创建

CString m_strFolderPath="c:\\test" 
// 判断路径是否存在 
if (!PathIsDirectory(m_strFolderPath) ) 
{ 
	CString strMsg; 
	strMsg.Format ("指定路径\"%s\"不存在,是否创建?", m_strFolderPath); 
	if (AfxMessageBox(strMsg, MB_YESNO) == IDYES) 
	{ 
		if (!CreateDirectory(m_strFolderPath, NULL ) ) 
		{ 
			strMsg.Format ("创建路径\"%s\"失败!是否继续?", m_strFolderPath); 
			if (AfxMessageBox(strMsg, MB_YESNO) == IDYES) 
			return; 
		} 
	} 
} 

MFC-文件夹-删除一个文件夹下的所有内容

void DeleteDirectoryNoEmpty(CString directory_path) //删除一个文件夹下的所有内容
{
	CFileFind finder;
	CString path;
	path.Format("%s\\*.*", directory_path);
	BOOL bWorking = finder.FindFile(path);
	while (bWorking)
	{
		bWorking = finder.FindNextFile();
		if (finder.IsDirectory() && !finder.IsDots())
		{//处理文件夹
			DeleteDirectoryNoEmpty(finder.GetFilePath()); //递归删除文件夹
			RemoveDirectory(finder.GetFilePath());
		}
		else
		{//删除文件
			AfxMessageBox(finder.GetFileName());
			if (finder.GetFileName() != "." && finder.GetFileName() != "..")
			{
				DeleteFile((HTuple)(finder.GetFilePath()));
			}
		}
	}
}

MFC-下拉框-当前显示

GetBoardInfoToCombobox("空");
GetBoardInfoToCombobox(str_board_id);//获取BoardInformation文件夹下的板号


****************************************
void CRubberDetectionDlg::GetBoardInfoToCombobox(CString currentBoardID) //获取版号信息
{
	m_CComboBox_boardInfo.ResetContent();
	m_CComboBox_boardInfo.AddString("空");// 默认情况

	CString filePath = exePath + "\\BoardInfomation\\";
	filePath = filePath + "*.*"; //获取当前工程下Res文件路径
	CFileFind fileFinder;

	BOOL bFinished = fileFinder.FindFile(filePath);
	while (bFinished)  //每次循环对应一个类别目录
	{
		bFinished = fileFinder.FindNextFile();
		CString fileName = fileFinder.GetFileName();
		if (bFinished != 0 || fileFinder.GetFileName().Right(fileName.GetLength() - fileFinder.GetFileName().ReverseFind('.')))  //考虑到最后一个文件为.bmp文件
		{
			if (fileFinder.IsDirectory() && (!fileFinder.IsDots())) //若是目录/文件夹
			{
				CString boardName = fileFinder.GetFileName();
				int judge_tf = m_CComboBox_boardInfo.AddString(boardName);
				if ((judge_tf == CB_ERR) || (judge_tf == CB_ERRSPACE))
					MessageBox(_T("File ID error!"));
				if (currentBoardID == boardName)
				{
					m_CCombBox_CStringBoardIndex = (byte)judge_tf;
				}
				else if (boardName == "空")
				{
					m_CCombBox_CStringBoardIndex = 0;
				}
			}
		}
	}
	m_CComboBox_boardInfo.SetCurSel(m_CCombBox_CStringBoardIndex);//预置版本号
	GetDlgItem(IDC_EDIT_CURFLAW)->SetFocus();
	UpdateData(FALSE);
}

windows查看进程内存使用记录

“性能监视器”——

1.windows自带的工具C:\Windows\System32\perfmon.exe

2.在绘制区域右击点击“添加计数器”

3.选择“Process”中的“working Set”

4.在“选定对象的实例”中选择自己要观察的进程“XXX”

5.点击“添加”,然后确定

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-05-07 11:00:54  更:2022-05-07 11:02:13 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/21 5:56:27-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码