Last active
May 21, 2019 01:34
-
-
Save jackhftang/776f3582b6611654b96d41bfa0d9cdc2 to your computer and use it in GitHub Desktop.
resolve the nested if in https://msdn.microsoft.com/en-us/library/windows/desktop/bb776913(v=vs.85).aspx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define FAILED(x) (!SUCCEEDED(x)) | |
HRESULT BasicFileOpen() | |
{ | |
// CoCreate the File Open Dialog object. | |
IFileDialog *pfd = NULL; | |
HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfd)); | |
if (FAILED(hr)) return hr; | |
// Create an event handling object, and hook it up to the dialog. | |
IFileDialogEvents *pfde = NULL; | |
hr = CDialogEventHandler_CreateInstance(IID_PPV_ARGS(&pfde)); | |
if (SUCCEEDED(hr)) { | |
// Hook up the event handler. | |
DWORD dwCookie; | |
hr = pfd->Advise(pfde, &dwCookie); | |
if (SUCCEEDED(hr)) { | |
// show dialog | |
hr = ShowFileDialog(hr, pfd, pfde) | |
// Unhook the event handler. | |
pfd->Unadvise(dwCookie); | |
} | |
pfde->Release(); | |
} | |
pfd->Release(); | |
return hr; | |
} | |
void ShowFileDialog(HRESULT hr, IFileDialog *pfd, IFileDialogEvents *pfde) { | |
// Set the options on the dialog. | |
DWORD dwFlags; | |
// Before setting, always get the options first in order | |
// not to override existing options. | |
hr = pfd->GetOptions(&dwFlags); | |
if(FAILED(hr)) return hr; | |
// In this case, get shell items only for file system items. | |
hr = pfd->SetOptions(dwFlags | FOS_FORCEFILESYSTEM); | |
if(FAILED(hr)) return hr; | |
// Set the file types to display only. | |
// Notice that this is a 1-based array. | |
hr = pfd->SetFileTypes(ARRAYSIZE(c_rgSaveTypes), c_rgSaveTypes); | |
if(FAILED(hr)) return hr; | |
// Set the selected file type index to Word Docs for this example. | |
hr = pfd->SetFileTypeIndex(INDEX_WORDDOC); | |
if(FAILED(hr)) return hr; | |
// Set the default extension to be ".doc" file. | |
hr = pfd->SetDefaultExtension(L"doc;docx"); | |
if(FAILED(hr)) return hr; | |
// Show the dialog | |
hr = pfd->Show(NULL); | |
if(FAILED(hr)) return hr; | |
// Obtain the result once the user clicks | |
// the 'Open' button. | |
// The result is an IShellItem object. | |
IShellItem *psiResult; | |
hr = pfd->GetResult(&psiResult); | |
if(FAILED(hr)) return hr; | |
// We are just going to print out the | |
// name of the file for sample sake. | |
PWSTR pszFilePath = NULL; | |
hr = psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); | |
if(FAILED(hr)) { | |
psiResult->Release(); | |
return hr; | |
} | |
TaskDialog( | |
NULL, | |
NULL, | |
L"CommonFileDialogApp", | |
pszFilePath, | |
NULL, | |
TDCBF_OK_BUTTON, | |
TD_INFORMATION_ICON, | |
NULL | |
); | |
CoTaskMemFree(pszFilePath); | |
return hr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment