C++使用Win32接口,打开文件对话框选文件。
/***************************************************************************
*IDE:VS2019
*Language:C/C++
*Project:Windows打开文件对话框
*Author:Raise
*DateTime:2020/6/26 15:53
*Note:代码是以前得应该是17年18年的。
****************************************************************************/
#include <iostream>
#include <windows.h>
#include <commdlg.h>
int main()
{
OPENFILENAME ofn; // 公共对话框结构。
TCHAR szFile[MAX_PATH]; // 保存获取文件名称的缓冲区。
// 初始化选择文件对话框。
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = NULL;
ofn.lpstrFile = szFile;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = L"All(*.*)\0*.*\0Text(*.txt)\0*.txt\0JPG(*.jpg)\0*.jpg";//(LPCWSTR)"All(*.*)\0*.*\0Text(*.txt)\0*.TXT\0\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
//ofn.lpTemplateName = MAKEINTRESOURCE(ID_TEMP_DIALOG);
// 显示打开选择文件对话框。
if (GetOpenFileName(&ofn))
{
//显示选择的文件。
std::wcout << szFile << std::endl;
//OutputDebugString(szFile); //这一句是显示路径吗?为什么不显示?
//OutputDebugString((LPCWSTR)"\r\n");
}
system("pause");
return 0;
}