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.点击“添加”,然后确定

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值