void OnTakePhoto(HWND hwnd) { wchar_t filename[MAX_PATH]; // Get the path to the Documents folder. IShellItem *psi = NULL; PWSTR pszFolderPath = NULL; HRESULT hr = SHCreateItemInKnownFolder(FOLDERID_Documents, 0, NULL, IID_PPV_ARGS(&psi)); if (FAILED(hr)) { goto done; } hr = psi->GetDisplayName(SIGDN_FILESYSPATH, &pszFolderPath); if (FAILED(hr)) { goto done; } // Construct a file name based on the current time. SYSTEMTIME time; GetLocalTime(&time); hr = StringCchPrintf(filename, MAX_PATH, L"MyPhoto%04u_%02u%02u_%02u%02u%02u.jpg", time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond); if (FAILED(hr)) { goto done; } LPTSTR path = PathCombine(PhotoFileName, pszFolderPath, filename); if (path == NULL) { hr = E_FAIL; goto done; } hr = g_pEngine->TakePhoto(path); if (FAILED(hr)) { goto done; } _SetStatusText(path); done: SafeRelease(&psi); CoTaskMemFree(pszFolderPath); if (FAILED(hr)) { ShowError(hwnd, IDS_ERR_PHOTO, hr); } UpdateUI(hwnd); }
/** * Get the shell item that represents the library. * * \param pwszLibraryName * The name of the shell library * * \param ppShellItem * If the operation succeeds, ppShellItem outputs the IShellItem2 interface * that represents the library. The caller is responsible for calling * Release on the shell item. If the function fails, NULL is returned from * *ppShellItem. */ HRESULT GetShellLibraryItem(LPWSTR pwszLibraryName, IShellItem2** ppShellItem) { HRESULT hr = E_NOINTERFACE; *ppShellItem = NULL; // Create the real library file name WCHAR wszRealLibraryName[MAX_PATH] = { 0 }; swprintf_s(wszRealLibraryName, L"%s%s", pwszLibraryName, L".library-ms"); hr = SHCreateItemInKnownFolder(FOLDERID_UsersLibraries, KF_FLAG_DEFAULT_PATH | KF_FLAG_NO_ALIAS, wszRealLibraryName, IID_PPV_ARGS(ppShellItem)); return hr; }
extern "C" LXCWIN_API HRESULT fileSaveDialog(HWND hWnd, LPWSTR dialogTitle, LPWSTR *result) { *result = NULL; HRESULT hr = S_OK; CoInitialize(nullptr); IFileOpenDialog *pfd = NULL;// yes, *open*, since this dialog selects an existing parent dir, not a new file hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfd)); if (SUCCEEDED(hr)) { // set default folder to "My Documents" IShellItem *psiDocuments = NULL; hr = SHCreateItemInKnownFolder(FOLDERID_Documents, 0, NULL, IID_PPV_ARGS(&psiDocuments)); if (SUCCEEDED(hr)) { hr = pfd->SetDefaultFolder(psiDocuments); psiDocuments->Release(); } // dialog title pfd->SetTitle(dialogTitle); // ok button text pfd->SetOkButtonLabel(L"Choose target"); // only choose directories DWORD dwOptions; hr = pfd->GetOptions(&dwOptions); if (SUCCEEDED(hr)) { pfd->SetOptions(dwOptions | FOS_PICKFOLDERS); } // show the save dialog hr = pfd->Show(hWnd); if (SUCCEEDED(hr)) { IShellItem *psiaResult; hr = pfd->GetResult(&psiaResult); if (SUCCEEDED(hr)) { psiaResult->GetDisplayName(SIGDN_FILESYSPATH, &(*result)); psiaResult->Release(); } } pfd->Release(); } return hr; }
// This helper creates the scope object that is a collection of shell items that // define where the search will operate. HRESULT CreateScope(IShellItemArray **ppsia) { *ppsia = NULL; IObjectCollection *pObjects; HRESULT hr = CreateShellItemArray(IID_PPV_ARGS(&pObjects)); if (SUCCEEDED(hr)) { IShellItem *psi; if (SUCCEEDED(SHCreateItemInKnownFolder(FOLDERID_DocumentsLibrary, 0, NULL, IID_PPV_ARGS(&psi)))) { pObjects->AddObject(psi); psi->Release(); } // Other items can be added to pObjects similar to the code above. hr = pObjects->QueryInterface(ppsia); pObjects->Release(); } return hr; }
// Adds a custom category to the Jump List. Each item that should be in the category is added to // an ordered collection, and then the category is appended to the Jump List as a whole. HRESULT _AddCategoryToList(ICustomDestinationList *pcdl, IObjectArray *poaRemoved) { IObjectCollection *poc; HRESULT hr = CoCreateInstance(CLSID_EnumerableObjectCollection, NULL, CLSCTX_INPROC, IID_PPV_ARGS(&poc)); if (SUCCEEDED(hr)) { for (UINT i = 0; i < ARRAYSIZE(c_rgpszFiles); i++) { IShellItem *psi; if (SUCCEEDED(SHCreateItemInKnownFolder(FOLDERID_Documents, KF_FLAG_DEFAULT, c_rgpszFiles[i], IID_PPV_ARGS(&psi)))) { // Items listed in the removed list may not be re-added to the Jump List during this // list-building transaction. They should not be re-added to the Jump List until // the user has used the item again. The AppendCategory call below will fail if // an attempt to add an item in the removed list is made. if (!_IsItemInArray(psi, poaRemoved)) { poc->AddObject(psi); } psi->Release(); } } IObjectArray *poa; hr = poc->QueryInterface(IID_PPV_ARGS(&poa)); if (SUCCEEDED(hr)) { // Add the category to the Jump List. If there were more categories, they would appear // from top to bottom in the order they were appended. hr = pcdl->AppendCategory(L"Custom Category", poa); poa->Release(); } poc->Release(); } return hr; }
extern "C" LXCWIN_API HRESULT fileOpenDialog(HWND hWnd, DWORD *count, LPWSTR **result) { *result = NULL; HRESULT hr = S_OK; CoInitialize(nullptr); IFileOpenDialog *pfd = NULL; hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfd)); if (SUCCEEDED(hr)) { // set default folder to "My Documents" IShellItem *psiDocuments = NULL; hr = SHCreateItemInKnownFolder(FOLDERID_Documents, 0, NULL, IID_PPV_ARGS(&psiDocuments)); if (SUCCEEDED(hr)) { hr = pfd->SetDefaultFolder(psiDocuments); psiDocuments->Release(); } // dialog title pfd->SetTitle(L"Select files to share"); // allow multiselect, restrict to real files DWORD dwOptions; hr = pfd->GetOptions(&dwOptions); if (SUCCEEDED(hr)) { // ideally, allow selecting folders as well as files, but IFileDialog does not support this :( pfd->SetOptions(dwOptions | FOS_ALLOWMULTISELECT | FOS_FORCEFILESYSTEM); // | FOS_PICKFOLDERS } // do not limit to certain file types // show the open file dialog hr = pfd->Show(hWnd); if (SUCCEEDED(hr)) { IShellItemArray *psiaResults; hr = pfd->GetResults(&psiaResults); if (SUCCEEDED(hr)) { hr = psiaResults->GetCount(count); if (SUCCEEDED(hr)) { *result = (LPWSTR*)calloc(*count, sizeof(LPWSTR)); if (*result != NULL) { for (DWORD i = 0; i < *count; i++) { IShellItem *resultItem = NULL; hr = psiaResults->GetItemAt(i, &resultItem); if (SUCCEEDED(hr)) { resultItem->GetDisplayName(SIGDN_FILESYSPATH, &((*result)[i])); resultItem->Release(); } } // paths now contains selected files } } psiaResults->Release(); } } pfd->Release(); } return hr; }