已经获取了CString类型的文件路径,一般推荐使用CFile或者CStdioFile进行文件 *** 作。
fread也是可以的,打开的文件名参数使用cstringGetBuffer(0)就可以了,不过注意工程最好不要是UNICODE模式,这种模式下fread会有一些麻烦。
win7 + vs2008 下通过:
#include <commdlgh>
#include <shlobjh>
///
//获取文件路径对话框 (树目录,可以同时打开文件夹和文件)
//回调函数BrowseCallbackProc用于指定初始化目录为szDir
///
static
INT CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData) // lParam member of the BROWSEINFO structure used in the call to SHBrowseForFolder
{
switch(uMsg)
{
case BFFM_INITIALIZED:
{
// WParam is TRUE since you are passing a path
// It would be FALSE if you were passing a pidl
// TCHAR szDir[MAX_PATH]={0};
// GetCurrentDirectory(sizeof(szDir)/sizeof(TCHAR), szDir);
// SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)"D:\\Qt\\Qt521_mingw48");
}
break;
case BFFM_SELCHANGED:
{
}
break;
}
return 0;
}
static
BOOL GetFilePath(TCHAR FilePath,
HWND hParentWnd,
TCHAR InstructionString=NULL, //提醒语句
UINT ulFlags=BIF_USENEWUI //BIF_USENEWUI拥有新特性,有新建文件夹按钮,可以调整大小,一般加上
|BIF_RETURNFSANCESTORS //Only return file system ancestors
|BIF_RETURNONLYFSDIRS //Only return file system directories
|BIF_BROWSEINCLUDEFILES//The browse dialog will display files as well as folders
)
{
BROWSEINFO bInfo={0};
bInfohwndOwner=hParentWnd;
bInfopszDisplayName=FilePath;
bInfolpszTitle=InstructionString;
bInfoulFlags= ulFlags;
bInfolpfn=BrowseCallbackProc; // bInfolpfn=NULL;
bInfolParam=0; // BrowseCallbackProc lpData
LPITEMIDLIST pidl= ::SHBrowseForFolder(&bInfo);
if (pidl == NULL)
return false;
if (::SHGetPathFromIDList(pidl,FilePath) == FALSE)
return false;
return true; //default return
}
//调用测试:
TCHAR FilePath[MAX_PATH]="D:\\Qt\\Qt521_mingw48";
if( GetFilePath(FilePath,hWnd,"请选择一个文件") )
{
::MessageBox(::GetFocus(),FilePath,"标题",MB_OK);
}
用EnumFontFamiliesEx函数就可以了,示例如下:
LOGFONT lf;ZeroMemory(&lf,sizeof(LOGFONT));
lflfCharSet = DEFAULT_CHARSET;
_tcscpy(lflfFaceName,TEXT(""));
EnumFontFamiliesEx(pDC->GetSafeHdc(),&lf,(FONTENUMPROC)EnumFontFamiliesProc,(LPARAM)this,0);BOOL CALLBACK CFontCombo::EnumFontFamiliesProc(LPENUMLOGFONT lpelf,LPNEWTEXTMETRIC lpntm,DWORD nFontType,long lParam)
{
if(!(nFontType & RASTER_FONTTYPE))
{
// if (lpelf->elfLogFontlfCharSet == ANSI_CHARSET || lpelf->elfLogFontlfCharSet == GB2312_CHARSET)
{
CFontCombo pCombo = (CFontCombo )lParam;
//过滤以@开头的字体,过滤已经添加的字体。
if(lpelf->elfFullName[0] != TEXT('@') && pCombo->FindStringExact(0,(LPCTSTR)lpelf->elfFullName) == LB_ERR)
{
int index = ((CFontCombo )lParam)->AddString((LPCTSTR)lpelf->elfFullName);
LPLOGFONT lpLF = new LOGFONT;
CopyMemory(lpLF,lpelf,sizeof(LOGFONT));
((CFontCombo )lParam)->SetItemData(index,(DWORD)lpLF);
}
}
}
return TRUE;
}
BROWSEINFO bInfo;
ZeroMemory(&bInfo, sizeof(bInfo));
bInfohwndOwner =GetSafeHwnd();
bInfolpszTitle = _T("请选择需要监视文件夹的路径: ");
bInfoulFlags = BIF_RETURNONLYFSDIRS;
LPITEMIDLIST lpDlist; //用来保存返回信息的IDList
lpDlist = SHBrowseForFolder(&bInfo) ; //显示选择对话框
if(lpDlist != NULL) //用户按了确定按钮
{
TCHAR chPath[MAX_PATH]; //用来存储路径的字符串
SHGetPathFromIDList(lpDlist, chPath);//把项目标识列表转化成字符串
CString m_strPath = chPath; //将TCHAR类型的字符串转换为CString类型的字符串
}
以上就是关于在mfc中获取的文件路径是cstring型,想用fread和fopen这样的函数读取文件全部的内容,包括:在mfc中获取的文件路径是cstring型,想用fread和fopen这样的函数读取文件、求一个VC6.0 mfc中树形控件获取磁盘文件目录的例子、mfc如何获取系统字体文件路径等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)