Skip to content

Instantly share code, notes, and snippets.

@hash3liZer
Last active August 12, 2022 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hash3liZer/31fc7ffc3e0a3fa15ab3d26d47941084 to your computer and use it in GitHub Desktop.
Save hash3liZer/31fc7ffc3e0a3fa15ab3d26d47941084 to your computer and use it in GitHub Desktop.
Open a File Dialog Box
// Specific Extensions
// https://cpp.hotexamples.com/examples/-/IFileDialog/SetFileTypeIndex/cpp-ifiledialog-setfiletypeindex-method-examples.html
PWSTR OpenFileBox(HWND hWnd) {
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED |
COINIT_DISABLE_OLE1DDE);
if (!SUCCEEDED(hr)) {
return NULL;
}
IFileOpenDialog* pFileOpen;
// Create the FileOpenDialog object.
hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL,
IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen));
COMDLG_FILTERSPEC filter[2];
filter[0].pszName = L"JPEG(*.jpg;*.jpeg)";
filter[0].pszSpec = L"*.jpg;*.jpeg";
filter[1].pszName = L"Images (*.jpg;*.jpeg;*.png;*.cur)";
filter[1].pszSpec = L"*.jpg;*.jpeg;*.png;*.cur";
if (!SUCCEEDED(hr)) {
return NULL;
}
// Show the Open dialog box.
hr = pFileOpen->SetFileTypes(2, filter);
hr = pFileOpen->SetFileTypeIndex(2);
hr = pFileOpen->Show(hWnd);
if (!SUCCEEDED(hr)) {
return NULL;
}
IShellItem* pItem;
hr = pFileOpen->GetResult(&pItem);
if (!SUCCEEDED(hr)) {
return NULL;
}
PWSTR pszFilePath;
hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
if (!SUCCEEDED(hr)) {
return NULL;
}
pItem->Release();
pFileOpen->Release();
CoUninitialize();
return pszFilePath;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment