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;
}