CString赋值给char*、获取文件(exe)所在的当前路径,获取文件的全路径,将CString值赋值给数组并将其打印

CString赋给值char*

路径获得存在path中,将其存放在apGetNativePath指针。HEX_FILE为文件名,path为得到的文件路径。

	CString path;
	path = GetNativePath;
	path += HEX_FILE;
	int ALength = WideCharToMultiByte(CP_ACP, 0, path, -1, NULL, 0, NULL, NULL);
	LPSTR pszA = (LPSTR)_alloca(ALength + 1);
	WideCharToMultiByte(CP_ACP, 0, path, -1, pszA, ALength, NULL, NULL);
	pszA[ALength] = 0;
	memset(GetHexFilePath, 0, 100);
	strncpy_s(GetHexFilePath, (LPSTR)pszA, sizeof(GetHexFilePath));
	GetHexFilePath[sizeof(GetHexFilePath) - 1] = 0;
	apGetHexFilePath = pszA;
	apGetNativePath = pszA;


读取数组,显示其值

	CString msg;
	for (int i=0;i<strlen(GetNativePath);i++)
	{
		msg += GetNativePath[i];
	}
	AfxMessageBox(msg, MB_OK);


读取当前文件路径并打印

		if (_getcwd(GetNativePath, 0xFF))
		{
			strcat_s(apGetNativePath, 200, HEX_FILE_DIS_HMD);
			
			/*print*/
			CString msg;
			for (int i=0;i<strlen(GetNativePath);i++)
			{
				msg += GetNativePath[i];
			}
			AfxMessageBox(msg, MB_OK);

			CString path;
			path = apGetNativePath;
			AfxMessageBox(path,MB_OK);

			apGetNativePath = strcat(apGetNativePath, HEX_FILE_DIS_HMD);

		}


对话框,button,点击button后,选择文件夹,其选择路径将显示在static中,将其文件的路径保存到数组和指针中。

	//这里显示是一个按钮的操作,其将打开一个文件,使其文件路径存放到数组和指针中,并显示到控件中。
	
	BOOL isOpen = TRUE;     //是否打开(否则为保存)  
	CString defaultDir = L"E:\\FileTest";   //默认打开的文件路径  
	CString fileName = L"";         //默认打开的文件名  
	CString filter = L"文件 ( *.hex)|*.hex||";   //文件过虑的类型  
	CFileDialog openFileDlg(isOpen, defaultDir, fileName, OFN_HIDEREADONLY | OFN_READONLY, filter, NULL);
	openFileDlg.GetOFN().lpstrInitialDir = L"E:\\FileTest\\test.doc";
	INT_PTR result = openFileDlg.DoModal();
	CString filePath;
	if (result == IDOK) {
		filePath = openFileDlg.GetPathName();
	}
	CWnd::SetDlgItemTextW(IDC_EDIT3, filePath);//将选中的文件路径显示到IDC_EDIT3中。
	
	int ALength = WideCharToMultiByte(CP_ACP, 0, filePath, -1, NULL, 0, NULL, NULL);
	LPSTR pszA = (LPSTR)_alloca(ALength + 1);
	WideCharToMultiByte(CP_ACP, 0, filePath, -1, pszA, ALength, NULL, NULL);
	pszA[ALength] = 0;//以上将文件路径赋值给LPSTR pszA中,因为程序使用Unicode编码。	

	//全局指针有问题,使用全局数组,下面三行及后面两行都是手动的,将文件路径赋值给数组
	memset(GetHexFilePath, 0, 100);
	strncpy_s(GetHexFilePath, (LPSTR)pszA,sizeof(GetHexFilePath));
	GetHexFilePath[sizeof(GetHexFilePath)-1]=0;

	apGetFilePath = pszA;//将文件路径赋值到指针中
	apGetNativePath = pszA;


MFC窗体使用:

1、获取客户区的大小

CRect rect;
GetClientRect(&rect);

2、获取控件大小

CRect rect;
CWnd *pWnd = GetDlgItem(IDC_picture);//IDC_picture为picture控件ID

pWnd->GetClientRect(&rc);//rc为控件的大小。

3、查看控件的大小并显示

	CRect rectCtrl;
	CStatic *p = (CStatic*)GetDlgItem(IDC_Upgrade_Btn);
	//p->MoveWindow(100, 100, 100, 100);//更改控件大小并移动其到指定位置
	p->GetWindowRect(rectCtrl);
	this->ScreenToClient(rectCtrl);
	CString msg;
	msg.Format(L"%d,%d,%d,%d", rectCtrl.left, rectCtrl.top, rectCtrl.Width(), rectCtrl.Height());
	AfxMessageBox(msg);


4、上面MoveWindow改变大小和位置,大小没有发生改变;

使用两个语句将其改变

	GetDlgItem(IDC_Upgrade_Btn)->SetWindowPos(0, 570, 406, 0, 0, SWP_NOSIZE);
	GetDlgItem(IDC_Upgrade_Btn)->SetWindowPos(0, 0, 0, 130, 40, SWP_NOZORDER | SWP_NOMOVE);


5、整理全的,获取文件的当前路径,或者文件exe的全路径,并在MFC中将其打印出来。

	/*第一种方法*/
	/*cannot get filepath in win8.1*/
	_getcwd(GetHexFilePath, 0xFF);
	strcat_s(apGetHexFilePath, 200, HEX_FILE_DIS_HMD);


	/*第二种方法*/
	CString strExePath;
	CString strPath;
	CString strHexPath;
	GetModuleFileName(NULL, strPath.GetBufferSetLength(MAX_PATH + 1), MAX_PATH + 1);  
	int nPos = strPath.ReverseFind(_T('\\'));
	strExePath = strPath.Left(nPos);
	strExePath += HEX_FILE_DIS_HMD;
	strHexPath = strExePath;

	/*give hex path[] to the char*  */
	int ALength = WideCharToMultiByte(CP_ACP, 0, strHexPath, -1, NULL, 0, NULL, NULL);
	LPSTR pszA = (LPSTR)_alloca(ALength + 1);
	WideCharToMultiByte(CP_ACP, 0, strHexPath, -1, pszA, ALength, NULL, NULL);
	pszA[ALength] = 0;
	memset(GetHexFilePath, 0, 256);
	strncpy_s(GetHexFilePath, (LPSTR)pszA, sizeof(GetHexFilePath));
	GetHexFilePath[sizeof(GetHexFilePath) - 1] = 0;
	apGetHexFilePath = pszA;

	AfxMessageBox(strExePath);//"d:\我的文档\Visual Studio 2005\Projects\test\Debug\" 
	AfxMessageBox(L"hex file"+strHexPath);
	AfxMessageBox(L"exe path is "+strPath);//"d:\我的文档\Visual Studio 2005\Projects\test\Debug\test.exe"
	
	CString msg;
	for (int i = 0; i<strlen(GetHexFilePath); i++)
	{
		msg += GetHexFilePath[i];
	}
	AfxMessageBox(msg, MB_OK);

	CString path;
	path = apGetHexFilePath;
	AfxMessageBox(path, MB_OK);


...

... 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一枚努力的程序猿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值