GetOpenFileName 选择文件的窗口

以前总以为那个选择文件的窗口要自己做,后来才知道不过是一个API而已.

这个API函数的声明是这个样子的:
WINCOMMDLGAPI BOOL APIENTRY GetOpenFileNameA(LPOPENFILENAMEA);
WINCOMMDLGAPI BOOL APIENTRY GetOpenFileNameW(LPOPENFILENAMEW);
#ifdef UNICODE
#define GetOpenFileName GetOpenFileNameW
#else
#define GetOpenFileName GetOpenFileNameA
#endif // !UNICODE


以下是代码

 OPENFILENAME ofn;
 TCHAR szFile[MAX_PATH];

 ZeroMemory(&ofn,sizeof(ofn));
 ofn.lStructSize = sizeof(ofn);
 ofn.hwndOwner = m_hWnd;
 ofn.lpstrFile = szFile;

 ofn.lpstrFile[0] = _T('\0');
 ofn.nMaxFile = sizeof(szFile);
 ofn.lpstrFilter = _T("ALL\0*.*\0Text\0*.TXT\0");
 ofn.nFilterIndex = 1;
 ofn.lpstrFileTitle = NULL;
 ofn.nMaxFileTitle = 0;
 ofn.lpstrInitialDir = NULL;
 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

 if (GetOpenFileName(&ofn))
 {
  OutputDebugString(szFile);
  OutputDebugString(_T("\r\n"));
 }

你可能感兴趣的:(File)