win api get selected file type
To get the selected file type using the WinAPI in Windows, you can use the following steps:
-
Obtain the handle to the open file dialog box using the GetOpenFileName or GetSaveFileName function.
-
Use the OFN_EXPLORER flag when calling GetOpenFileName or GetSaveFileName to indicate that the dialog box should use the newer Explorer-style interface.
-
Retrieve the selected file name using the lpstrFile member of the OPENFILENAME structure.
-
Determine the file extension by searching for the last occurrence of the '.' character in the file name.
-
Use the file extension to determine the file type by comparing it to a list of known file extensions and their corresponding file types.
Here is some sample code that demonstrates these steps:
OPENFILENAME ofn;
char szFile[MAX_PATH];
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST;
if (GetOpenFileName(&ofn))
{
char* ext = strrchr(szFile, '.');
if (ext != NULL)
{
if (_stricmp(ext, ".txt") == 0)
{
// File is a text file
}
else if (_stricmp(ext, ".jpg") == 0 || _stricmp(ext, ".jpeg") == 0)
{
// File is a JPEG image
}
else if (_stricmp(ext, ".pdf") == 0)
{
// File is a PDF document
}
else
{
// File type is unknown
}
}
}
``
原文地址: https://www.cveoy.top/t/topic/g7Gb 著作权归作者所有。请勿转载和采集!