本文实例讲述了C++采用openfilename打开文件对话框用法,分享给大家供大家参考。具体方法如下:
这里需要注意,有的时候会因为没设置nMaxFile,而一直不出现对话框。
具体功能代码如下:
复制代码 代码如下:
char szFileName[MAX_PATH]={0};
OPENFILENAME openFileName = {0};
openFileName.lStructSize = sizeof(OPENFILENAME);
openFileName.nMaxFile = MAX_PATH; //这个必须设置,不设置的话不会出现打开文件对话框
openFileName.lpstrFilter = "文本文件(*.txt)\0*.txt\0所有文件(*.*)\0*.*\0\0";
openFileName.lpstrFile = szFileName;
openFileName.nFilterIndex = 1;
openFileName.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (::GetOpenFileName(&openFileName))
{
::MessageBoxA(hwndDlg, openFileName.lpstrFile, "", MB_OK);
}
OPENFILENAME openFileName = {0};
openFileName.lStructSize = sizeof(OPENFILENAME);
openFileName.nMaxFile = MAX_PATH; //这个必须设置,不设置的话不会出现打开文件对话框
openFileName.lpstrFilter = "文本文件(*.txt)\0*.txt\0所有文件(*.*)\0*.*\0\0";
openFileName.lpstrFile = szFileName;
openFileName.nFilterIndex = 1;
openFileName.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (::GetOpenFileName(&openFileName))
{
::MessageBoxA(hwndDlg, openFileName.lpstrFile, "", MB_OK);
}
希望本文所述对大家的C++程序设计有所帮助。