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);
...
...