BOOL OpenFilesVistaUp(HWND hwnd, lFILEINFO *pFInfoList) { IFileOpenDialog *pfd; IFileDialogCustomize *pfdc; FILEOPENDIALOGOPTIONS dwOptions; DWORD dwCookie = 0; CoInitialize(NULL); // CoCreate the dialog object. HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfd)); if (SUCCEEDED(hr)) { hr = pfd->QueryInterface(IID_PPV_ARGS(&pfdc)); if(SUCCEEDED(hr)) { hr = pfdc->EnableOpenDropDown(FDIALOG_OPENCHOICES); if (SUCCEEDED(hr)) { hr = pfdc->AddControlItem(FDIALOG_OPENCHOICES, FDIALOG_CHOICE_OPEN, L"&Open"); if (SUCCEEDED(hr)) { hr = pfdc->AddControlItem(FDIALOG_OPENCHOICES, FDIALOG_CHOICE_REPARENT, L"&Reparent hash file"); hr = pfdc->AddControlItem(FDIALOG_OPENCHOICES, FDIALOG_CHOICE_ALLHASHES, L"&Open all hash files"); hr = pfdc->AddControlItem(FDIALOG_OPENCHOICES, FDIALOG_CHOICE_BSD, L"&Force open as BSD-style"); } } } pfdc->Release(); hr = pfd->GetOptions(&dwOptions); if (SUCCEEDED(hr)) { hr = pfd->SetOptions(dwOptions | FOS_ALLOWMULTISELECT | FOS_FORCEFILESYSTEM); if (SUCCEEDED(hr)) { COpenFileListener *ofl = new COpenFileListener(pFInfoList); hr = pfd->Advise(ofl,&dwCookie); if (SUCCEEDED(hr)) { hr = pfd->Show(hwnd); if (SUCCEEDED(hr)) { } pfd->Unadvise(dwCookie); } } } pfd->Release(); } CoUninitialize(); return SUCCEEDED(hr); }
PWSTR LoadFile() { IFileOpenDialog *pFileOpen; PWSTR pszFilePath = NULL; // Create the FileOpenDialog object. HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen)); if (SUCCEEDED(hr)) { //IShellItem *psiDocuments = NULL; //hr = SHCreateItemInKnownFolder(FOLDERID_Documents, 0, NULL, IID_PPV_ARGS(&psiDocuments)); //if (SUCCEEDED(hr)) { // hr = pFileOpen->SetFolder(psiDocuments); // psiDocuments->Release(); //} // Show the Open dialog box. hr = pFileOpen->Show(NULL); // Get the file name from the dialog box. if (SUCCEEDED(hr)) { IShellItem *pItem; hr = pFileOpen->GetResult(&pItem); if (SUCCEEDED(hr)) { hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); pItem->Release(); } } pFileOpen->Release(); } return pszFilePath; }
// Open an audio/video file. void OnFileOpen(HWND hwnd) { IFileOpenDialog *pFileOpen = NULL; IShellItem *pItem = NULL; PWSTR pszFilePath = NULL; // Create the FileOpenDialog object. HRESULT hr = CoCreateInstance(__uuidof(FileOpenDialog), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pFileOpen)); if (FAILED(hr)) { goto done; } // Show the Open dialog box. hr = pFileOpen->Show(NULL); if (hr == HRESULT_FROM_WIN32(ERROR_CANCELLED)) { // The user canceled the dialog. Do not treat as an error. hr = S_OK; goto done; } else if (FAILED(hr)) { goto done; } // Get the file name from the dialog box. hr = pFileOpen->GetResult(&pItem); if (FAILED(hr)) { goto done; } hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); if (FAILED(hr)) { goto done; } // Display the file name to the user. hr = g_pPlayer->OpenURL(pszFilePath); if (SUCCEEDED(hr)) { UpdateUI(hwnd, OpenPending); } done: if (FAILED(hr)) { NotifyError(hwnd, L"Could not open the file.", hr); UpdateUI(hwnd, Closed); } CoTaskMemFree(pszFilePath); SafeRelease(&pItem); SafeRelease(&pFileOpen); }
STDMETHODIMP COpenFileListener::OnFileOk(IFileDialog* pfd) { IShellItemArray *psiaResults; IFileDialogCustomize *pfdc; HRESULT hr; IFileOpenDialog *fod; FILEINFO fileinfoTmp = {0}; DWORD choice; fileinfoTmp.parentList = pFInfoList; hr = pfd->QueryInterface(IID_PPV_ARGS(&fod)); if(SUCCEEDED(hr)) { hr = fod->GetSelectedItems(&psiaResults); if (SUCCEEDED(hr)) { DWORD fileCount; IShellItem *isi; LPWSTR pwsz = NULL; psiaResults->GetCount(&fileCount); for(DWORD i=0;i<fileCount;i++) { psiaResults->GetItemAt(i,&isi); isi->GetDisplayName(SIGDN_FILESYSPATH,&pwsz); isi->Release(); fileinfoTmp.szFilename = pwsz; pFInfoList->fInfos.push_back(fileinfoTmp); CoTaskMemFree(pwsz); } psiaResults->Release(); } fod->Release(); } hr = pfd->QueryInterface(IID_PPV_ARGS(&pfdc)); if(SUCCEEDED(hr)) { hr = pfdc->GetSelectedControlItem(FDIALOG_OPENCHOICES,&choice); if(SUCCEEDED(hr)) { if(choice==FDIALOG_CHOICE_REPARENT) { pFInfoList->uiCmdOpts = CMD_REPARENT; } else if(choice==FDIALOG_CHOICE_ALLHASHES) { pFInfoList->uiCmdOpts = CMD_ALLHASHES; } else if(choice==FDIALOG_CHOICE_BSD) { pFInfoList->uiCmdOpts = CMD_FORCE_BSD; } } pfdc->Release(); } return S_OK; }
void FileOpen::showDialog() { #ifdef _WIN32 path = NULL; HRESULT hResult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); if (SUCCEEDED(hResult)) { //Create a instance to open the fileOpenDialog IFileOpenDialog *pFileOpen; hResult = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pFileOpen)); if (SUCCEEDED(hResult)) { //show the File Open hResult = pFileOpen->Show(NULL); //get the file name result if (SUCCEEDED(hResult)) { IShellItem *pItem; hResult = pFileOpen->GetResult(&pItem); if (SUCCEEDED(hResult)) { PWSTR FilePath; hResult = pItem->GetDisplayName(SIGDN_FILESYSPATH, &FilePath); //get the file name and copy to "path" if (SUCCEEDED(hResult)) { int count = wcslen(FilePath); path = new char[2*count]; wcstombs(path, FilePath, count); path[count] = '\0'; CoTaskMemFree(FilePath); } pItem->Release(); } } pFileOpen->Release(); } CoUninitialize(); } #else std::cout << "Sorry, this is not implemented on linux yet =(" << std::endl; return; #endif }
BOOL ReadFromFile() { IFileOpenDialog *pDlg; COMDLG_FILTERSPEC FileTypes[] = { { L"PS2 MemoryCard files", L"*.ps2" }, { L"All files", L"*.*" } }; HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDlg)); WCHAR cPath[MAX_PATH] = L""; GetCurrentDirectoryW(sizeof(cPath) / sizeof(cPath[0]), cPath); IShellItem *psiFolder, *psiParent; SHCreateItemFromParsingName(cPath, NULL, IID_PPV_ARGS(&psiFolder)); psiFolder->GetParent(&psiParent); //初期フォルダの指定 pDlg->SetFolder(psiFolder); //フィルターの指定 pDlg->SetFileTypes(_countof(FileTypes), FileTypes); //ダイアログ表示 hr = pDlg->Show(NULL); //ファイル名 LPOLESTR pwsz = NULL; if (SUCCEEDED(hr)) { IShellItem *pItem; hr = pDlg->GetResult(&pItem); if (SUCCEEDED(hr)) { hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pwsz); if (SUCCEEDED(hr)) { HANDLE hFile; hFile = CreateFile(pwsz, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); if (hFile) { DWORD BytesRead; BOOL b = ReadFile(hFile, &(byteMemDat.Byte), sizeof(byteMemDat), &BytesRead, NULL); if (BytesRead) { CloseHandle(hFile); } } } } } UpdateDataList(&byteMemDat); return TRUE; }
std::vector<std::wstring> APlayerWindow::showOpenFile() { HRESULT hr = S_OK; std::vector<std::wstring> filePaths; IFileOpenDialog *fileDlg = NULL; hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&fileDlg)); if (FAILED(hr)) return filePaths; ON_SCOPE_EXIT([&] { fileDlg->Release(); }); IKnownFolderManager *pkfm = NULL; hr = CoCreateInstance(CLSID_KnownFolderManager, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pkfm)); if (FAILED(hr)) return filePaths; ON_SCOPE_EXIT([&] { pkfm->Release(); }); IKnownFolder *pKnownFolder = NULL; hr = pkfm->GetFolder(FOLDERID_PublicMusic, &pKnownFolder); if (FAILED(hr)) return filePaths; ON_SCOPE_EXIT([&] { pKnownFolder->Release(); }); IShellItem *psi = NULL; hr = pKnownFolder->GetShellItem(0, IID_PPV_ARGS(&psi)); if (FAILED(hr)) return filePaths; ON_SCOPE_EXIT([&] { psi->Release(); }); hr = fileDlg->AddPlace(psi, FDAP_BOTTOM); COMDLG_FILTERSPEC rgSpec[] = { { L"ÒôÀÖÎļþ", SupportType } }; fileDlg->SetFileTypes(1, rgSpec); DWORD dwOptions; fileDlg->GetOptions(&dwOptions); fileDlg->SetOptions(dwOptions | FOS_ALLOWMULTISELECT); hr = fileDlg->Show(NULL); if (SUCCEEDED(hr)) { IShellItemArray *pRets; hr = fileDlg->GetResults(&pRets); if (SUCCEEDED(hr)) { DWORD count; pRets->GetCount(&count); for (DWORD i = 0; i < count; i++) { IShellItem *pRet; LPWSTR nameBuffer; pRets->GetItemAt(i, &pRet); pRet->GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING, &nameBuffer); filePaths.push_back(std::wstring(nameBuffer)); pRet->Release(); CoTaskMemFree(nameBuffer); } pRets->Release(); } } return filePaths; }
~IFileOpenDialog_Wrapper() { if (m_pInterface) { m_pInterface->Release(); CoUninitialize(); } }
string getFilePathWithDialog() { string path = ""; HRESULT hr = CoInitializeEx(NULL, COINITBASE_MULTITHREADED | COINIT_DISABLE_OLE1DDE); if (SUCCEEDED(hr)) { IFileOpenDialog *pFileOpen; // Create the FileOpenDialog object. hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen)); if (SUCCEEDED(hr)) { // Show the Open dialog box. hr = pFileOpen->Show(NULL); // Get the file name from the dialog box. if (SUCCEEDED(hr)) { IShellItem *pItem; hr = pFileOpen->GetResult(&pItem); if (SUCCEEDED(hr)) { PWSTR pszFilePath; hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); // Display the file name to the user. if (SUCCEEDED(hr)) { //MessageBox(NULL, pszFilePath, L"File Path", MB_OK); char buffer[500]; wcstombs(buffer, pszFilePath, 500); path = buffer; CoTaskMemFree(pszFilePath); } pItem->Release(); } } pFileOpen->Release(); } CoUninitialize(); } return path; }
PWSTR openUserFileDiaglog() { PWSTR pszFilePath = NULL; HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); if (SUCCEEDED(hr)) { IFileOpenDialog *pFileOpen = NULL; // Create the FileOpenDialog object. hr = CoCreateInstance( CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileOpenDialog, (void**)&pFileOpen); if (SUCCEEDED(hr)) { // Show the Open dialog box. hr = pFileOpen->Show(NULL); // Get the file name from the dialog box. if (SUCCEEDED(hr)) { IShellItem *pItem = NULL; hr = pFileOpen->GetResult(&pItem); if (SUCCEEDED(hr)) { hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); pItem->Release(); } } pFileOpen->Release(); } CoUninitialize(); } return pszFilePath; }
const bool Core::select_file() { //Open file dialog, straight from https://msdn.microsoft.com/en-us/library/windows/desktop/ff485843(v=vs.85).aspx HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); PWSTR pszFilePath = nullptr; if (SUCCEEDED(hr)) { IFileOpenDialog *pFileOpen; // Create the FileOpenDialog object. hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen)); if (SUCCEEDED(hr)) { //Boilerplate for only showing *.nes files in the dialog. See the MSDN docs more info. COMDLG_FILTERSPEC fileFilter; fileFilter.pszName = L"iNES"; fileFilter.pszSpec = L"*.nes"; pFileOpen->SetFileTypes(1, &fileFilter); pFileOpen->SetFileTypeIndex(1); pFileOpen->SetDefaultExtension(L"nes"); // Show the Open dialog box. hr = pFileOpen->Show(NULL); // Get the file name from the dialog box. if (SUCCEEDED(hr)) { IShellItem *pItem; hr = pFileOpen->GetResult(&pItem); if (SUCCEEDED(hr)) { hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); if (SUCCEEDED(hr)) { logmsg("Opening file"); } else { pszFilePath = nullptr; } pItem->Release(); } } pFileOpen->Release(); } CoUninitialize(); } if (pszFilePath == nullptr) { alert_error("Unable to open file! File must have the extension \".nes\""); return false; } //Convert wchar_t string to char string std::size_t i; wcstombs_s(&i, fileSelection, pszFilePath, MAX_PATH); return true; }
int openFileGetName2() { HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); if (SUCCEEDED(hr)) { IFileOpenDialog *pFileOpen; // Create the FileOpenDialog object. hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen)); if (SUCCEEDED(hr)) { // Show the Open dialog box. hr = pFileOpen->Show(NULL); // Get the file name from the dialog box. if (SUCCEEDED(hr)) { IShellItem *pItem; hr = pFileOpen->GetResult(&pItem); if (SUCCEEDED(hr)) { hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &endnFile); // Display the file name to the user. if (SUCCEEDED(hr)) { endnFILE = endnFile; CoTaskMemFree(endnFile); } pItem->Release(); } } pFileOpen->Release(); } CoUninitialize(); } return 0; }
/* * OpenFileName * Opens a folder dialog box, and returns the path * Returns an empty string if dialog is canceled */ std::string OpenFolder(/*char *filter,*/ HWND owner) { HRESULT hr; IFileOpenDialog *pOpenFolderDialog; std::string folderPath; // CoCreate the dialog object. hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pOpenFolderDialog)); if (SUCCEEDED(hr)) { pOpenFolderDialog->SetOptions(FOS_PICKFOLDERS); // Show the dialog hr = pOpenFolderDialog->Show(owner); if (SUCCEEDED(hr)) { // Obtain the result of the user's interaction with the dialog. IShellItem *psiResult; hr = pOpenFolderDialog->GetResult(&psiResult); LPWSTR folderW = NULL; psiResult->GetDisplayName(SIGDN_FILESYSPATH, &folderW); char* folderC; folderC = new char[300]; folderPath = wcstombs(folderC,folderW, 300); folderPath = folderC; //free memory if (folderC) delete[] folderC; CoTaskMemFree ( folderW ); psiResult->Release(); } } pOpenFolderDialog->Release(); return folderPath; }
void gameOfLife3D::MainWnd::OnOpenFile() { static COMDLG_FILTERSPEC dialogFiltersMM[] = { { L"Life File(*.lif;*.life)", L"*.lif;*.life" }, { L"All types(*.*)", L"*.*" }, }; IFileOpenDialog *pFileOpenDialog = nullptr; HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pFileOpenDialog)); if (SUCCEEDED(hr)) { hr = pFileOpenDialog->SetFileTypes(2, dialogFiltersMM); } IShellItem *pResult = nullptr; if (SUCCEEDED(hr)) { hr = pFileOpenDialog->Show(m_hwnd); } if (SUCCEEDED(hr)) { hr = pFileOpenDialog->GetResult(&pResult); } WCHAR *pPath = nullptr; if (SUCCEEDED(hr)) { hr = pResult->GetDisplayName(SIGDN_FILESYSPATH, &pPath); } if (SUCCEEDED(hr)) { std::wstring fileName(pPath); auto lifeFile = std::make_shared<gameOfLife3D::io::LifeFile>(); lifeFile->Load(fileName); m_canvasPanel->SetLifeFile(lifeFile); } if (pPath != nullptr) { CoTaskMemFree(pPath); } SafeRelease(&pResult); SafeRelease(&pFileOpenDialog); }
bool CPPageWebServer::PickDir(CString& dir) { CString strTitle = ResStr(IDS_PPAGEWEBSERVER_0); bool success = false; if (SysVersion::IsVistaOrLater()) { CFileDialog dlg(TRUE); IFileOpenDialog* openDlgPtr = dlg.GetIFileOpenDialog(); if (openDlgPtr != NULL) { openDlgPtr->SetTitle(strTitle); openDlgPtr->SetOptions(FOS_PICKFOLDERS | FOS_FORCEFILESYSTEM | FOS_PATHMUSTEXIST); // Typedef for function SHCreateItemFromParsingName typedef HRESULT(STDAPICALLTYPE * PFN_TYPE_SHCreateItemFromParsingName)(PCWSTR /*pszPath*/, IBindCtx* /*pbc*/, REFIID /*riid*/, void** /*ppv*/); // Load SHELL32.DLL to get pointer to aforementioned function HINSTANCE hDllShell = ::LoadLibrary(_T("Shell32.dll")); PFN_TYPE_SHCreateItemFromParsingName pfnSHCreateItemFromParsingName = NULL; if (hDllShell != NULL) { // Try to get the pointer to that function pfnSHCreateItemFromParsingName = reinterpret_cast<PFN_TYPE_SHCreateItemFromParsingName>(::GetProcAddress(hDllShell, "SHCreateItemFromParsingName")); } if (pfnSHCreateItemFromParsingName != NULL) { CComPtr<IShellItem> psiFolder; if (SUCCEEDED(pfnSHCreateItemFromParsingName(dir, NULL, IID_PPV_ARGS(&psiFolder)))) { openDlgPtr->SetFolder(psiFolder); } } if (SUCCEEDED(openDlgPtr->Show(m_hWnd))) { dir = dlg.GetFolderPath(); success = true; } openDlgPtr->Release(); } } else { TCHAR buff[_MAX_PATH]; BROWSEINFO bi; bi.hwndOwner = m_hWnd; bi.pidlRoot = NULL; bi.pszDisplayName = buff; bi.lpszTitle = strTitle; bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_VALIDATE | BIF_USENEWUI; bi.lpfn = BrowseCtrlCallback; bi.lParam = (LPARAM)(LPCTSTR)dir; bi.iImage = 0; LPITEMIDLIST iil = SHBrowseForFolder(&bi); if (iil) { SHGetPathFromIDList(iil, buff); dir = buff; success = true; } } return success; }
std::string Dlg_MemBookmark::ImportDialog() { std::string str; if ( g_pCurrentGameData->GetGameID() == 0 ) { MessageBox( nullptr, _T("ROM not loaded: please load a ROM first!"), _T("Error!"), MB_OK ); return str; } IFileOpenDialog* pDlg = nullptr; HRESULT hr = CoCreateInstance( CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileOpenDialog, reinterpret_cast<void**>( &pDlg ) ); if ( hr == S_OK ) { hr = pDlg->SetFileTypes( ARRAYSIZE( c_rgFileTypes ), c_rgFileTypes ); if ( hr == S_OK ) { hr = pDlg->Show( nullptr ); if ( hr == S_OK ) { IShellItem* pItem = nullptr; hr = pDlg->GetResult( &pItem ); if ( hr == S_OK ) { LPWSTR pStr = nullptr; hr = pItem->GetDisplayName( SIGDN_FILESYSPATH, &pStr ); if ( hr == S_OK ) { str = Narrow( pStr ); } pItem->Release(); } } } pDlg->Release(); } return str; }
void gameOfLife3D::MainWnd::OnOpenCSFile() { static COMDLG_FILTERSPEC dialogFiltersMM[] = { { L"effect File(*.fx)", L"*.fx" }, { L"compute shader File(*.cs)", L"*.cs" }, { L"All types(*.*)", L"*.*" }, }; IFileOpenDialog *pFileOpenDialog = nullptr; HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pFileOpenDialog)); if (SUCCEEDED(hr)) { hr = pFileOpenDialog->SetFileTypes(3, dialogFiltersMM); } IShellItem *pResult = nullptr; if (SUCCEEDED(hr)) { hr = pFileOpenDialog->Show(m_hwnd); } if (SUCCEEDED(hr)) { hr = pFileOpenDialog->GetResult(&pResult); } WCHAR *pPath = nullptr; if (SUCCEEDED(hr)) { hr = pResult->GetDisplayName(SIGDN_FILESYSPATH, &pPath); } if (SUCCEEDED(hr)) { std::wstring fileName(pPath); m_canvasPanel->OpenCSFile(fileName); } if (pPath != nullptr) { CoTaskMemFree(pPath); } SafeRelease(&pResult); SafeRelease(&pFileOpenDialog); }
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; }
QString qt_win_CID_get_existing_directory(const QFileDialogArgs &args) { QString result; QDialog modal_widget; modal_widget.setAttribute(Qt::WA_NoChildEventsForParent, true); modal_widget.setParent(args.parent, Qt::Window); QApplicationPrivate::enterModal(&modal_widget); IFileOpenDialog *pfd = 0; HRESULT hr = CoCreateInstance(QT_CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, QT_IID_IFileOpenDialog, reinterpret_cast<void**>(&pfd)); if (SUCCEEDED(hr)) { qt_win_set_IFileDialogOptions(pfd, args.selection, args.directory, args.caption, QStringList(), QFileDialog::ExistingFile, args.options); // Set the FOS_PICKFOLDERS flag DWORD newOptions; hr = pfd->GetOptions(&newOptions); newOptions |= (FOS_PICKFOLDERS | FOS_FORCEFILESYSTEM); if (SUCCEEDED(hr) && SUCCEEDED((hr = pfd->SetOptions(newOptions)))) { QWidget *parentWindow = args.parent; if (parentWindow) parentWindow = parentWindow->window(); else parentWindow = QApplication::activeWindow(); // Show the file dialog. hr = pfd->Show(parentWindow ? parentWindow->winId() : 0); if (SUCCEEDED(hr)) { // Retrieve the result IShellItem *psi = 0; hr = pfd->GetResult(&psi); if (SUCCEEDED(hr)) { // Retrieve the file name from shell item. wchar_t *pszPath; hr = psi->GetDisplayName(SIGDN_FILESYSPATH, &pszPath); if (SUCCEEDED(hr)) { result = QString::fromWCharArray(pszPath); CoTaskMemFree(pszPath); } psi->Release(); // Free the current item. } } } } QApplicationPrivate::leaveModal(&modal_widget); qt_win_eatMouseMove(); if (pfd) pfd->Release(); return result; }
void CPPageDVD::OnBnClickedButton1() { CString path; CString strTitle = ResStr(IDS_MAINFRM_46); if (IsWinVistaOrLater()) { CFileDialog dlg(TRUE); IFileOpenDialog *openDlgPtr = dlg.GetIFileOpenDialog(); if (openDlgPtr != NULL) { openDlgPtr->SetTitle(strTitle); openDlgPtr->SetOptions(FOS_PICKFOLDERS | FOS_FORCEFILESYSTEM | FOS_PATHMUSTEXIST); if (FAILED(openDlgPtr->Show(m_hWnd))) { openDlgPtr->Release(); return; } openDlgPtr->Release(); path = dlg.GetFolderPath(); } } else { TCHAR _path[_MAX_PATH]; BROWSEINFO bi; bi.hwndOwner = m_hWnd; bi.pidlRoot = NULL; bi.pszDisplayName = _path; bi.lpszTitle = strTitle; bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_VALIDATE | BIF_USENEWUI | BIF_NONEWFOLDERBUTTON; bi.lpfn = NULL; bi.lParam = 0; bi.iImage = 0; LPITEMIDLIST iil = SHBrowseForFolder(&bi); if (iil) { SHGetPathFromIDList(iil, _path); path = _path; } } if (!path.IsEmpty()) { m_dvdpath = path; UpdateData(FALSE); SetModified(); } }
bool CPPageWebServer::PickDir(CString& dir) { CString strTitle = ResStr(IDS_PPAGEWEBSERVER_0); bool success = false; if (SysVersion::IsVistaOrLater()) { CFileDialog dlg(TRUE); IFileOpenDialog* openDlgPtr = dlg.GetIFileOpenDialog(); if (openDlgPtr != nullptr) { openDlgPtr->SetTitle(strTitle); openDlgPtr->SetOptions(FOS_PICKFOLDERS | FOS_FORCEFILESYSTEM | FOS_PATHMUSTEXIST); if (!dir.IsEmpty()) { CComPtr<IShellItem> psiFolder; if (SUCCEEDED(afxGlobalData.ShellCreateItemFromParsingName(dir, nullptr, IID_PPV_ARGS(&psiFolder)))) { openDlgPtr->SetFolder(psiFolder); } } if (SUCCEEDED(openDlgPtr->Show(m_hWnd))) { dir = dlg.GetFolderPath(); success = true; } openDlgPtr->Release(); } } else { TCHAR buff[MAX_PATH]; BROWSEINFO bi; bi.hwndOwner = m_hWnd; bi.pidlRoot = nullptr; bi.pszDisplayName = buff; bi.lpszTitle = strTitle; bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_VALIDATE | BIF_USENEWUI; bi.lpfn = BrowseCtrlCallback; bi.lParam = (LPARAM)(LPCTSTR)dir; bi.iImage = 0; LPITEMIDLIST iil = SHBrowseForFolder(&bi); if (iil) { SHGetPathFromIDList(iil, buff); dir = buff; success = true; } } return success; }
void EditorScreen::LoadMap() { HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); if (SUCCEEDED(hr)) { IFileOpenDialog *pFileOpen = NULL; HRESULT hr = CoCreateInstance(__uuidof(FileOpenDialog), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pFileOpen)); if (SUCCEEDED(hr)) { hr = pFileOpen->SetDefaultExtension(L"xml"); hr = pFileOpen->SetFileTypes(ARRAYSIZE(c_rgSaveTypes), c_rgSaveTypes); hr = pFileOpen->Show(NULL); if (SUCCEEDED(hr)) { IShellItem *pItem; hr = pFileOpen->GetResult(&pItem); if (SUCCEEDED(hr)) { PWSTR pszFilePath; hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); // Display the file name to the user. if (SUCCEEDED(hr)) { // Reset current Map information ResetMap(); std::string filePath = utf8_encode(pszFilePath); map->LoadMap(filePath); CoTaskMemFree(pszFilePath); } pItem->Release(); } } pFileOpen->Release(); } CoUninitialize(); } }
void openBrowseDialogCore(FileDialogType type, const Path& defaultPath, const String& filterList, Vector<Path>& paths, AsyncOp& returnValue) { // Init COM library. CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); IFileDialog* fileDialog = nullptr; UINT32 dialogType = ((UINT32)type & (UINT32)FileDialogType::TypeMask); bool isOpenDialog = dialogType == (UINT32)FileDialogType::OpenFile || dialogType == (UINT32)FileDialogType::OpenFolder; // Create dialog IID classId = isOpenDialog ? CLSID_FileOpenDialog : CLSID_FileSaveDialog; CoCreateInstance(classId, nullptr, CLSCTX_ALL, IID_PPV_ARGS(&fileDialog)); addFiltersToDialog(fileDialog, filterList); setDefaultPath(fileDialog, defaultPath); // Apply multiselect flags bool isMultiselect = false; if (isOpenDialog) { if (dialogType == (UINT32)FileDialogType::OpenFile) { if (((UINT32)type & (UINT32)FileDialogType::Multiselect) != 0) { DWORD dwFlags; fileDialog->GetOptions(&dwFlags); fileDialog->SetOptions(dwFlags | FOS_ALLOWMULTISELECT); isMultiselect = true; } } else { DWORD dwFlags; fileDialog->GetOptions(&dwFlags); fileDialog->SetOptions(dwFlags | FOS_PICKFOLDERS); } } // Show the dialog bool finalResult = false; // Need to enable all windows, otherwise when the browse dialog closes the active window will become some // background window Win32Window::_enableAllWindows(); if (SUCCEEDED(fileDialog->Show(nullptr))) { if (isMultiselect) { // Get file names IFileOpenDialog* fileOpenDialog; fileDialog->QueryInterface(IID_IFileOpenDialog, (void**)&fileOpenDialog); IShellItemArray* shellItems = nullptr; fileOpenDialog->GetResults(&shellItems); getPaths(shellItems, paths); shellItems->Release(); fileOpenDialog->Release(); } else { // Get the file name IShellItem* shellItem = nullptr; fileDialog->GetResult(&shellItem); LPWSTR filePath = nullptr; shellItem->GetDisplayName(SIGDN_FILESYSPATH, &filePath); paths.push_back((Path)UTF8::fromWide(WString(filePath))); CoTaskMemFree(filePath); shellItem->Release(); } finalResult = true; } // Restore modal window state (before we enabled all windows) Win32Window::_restoreModalWindows(); CoUninitialize(); returnValue._completeOperation(finalResult); }
//----------------------------------------------------------------------------- bool VistaFileSelector::runModalInternal () { fileDialog = 0; HRESULT hr = -1; if (style == kSelectSaveFile) { hr = CoCreateInstance (CLSID_FileSaveDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARG(IFileDialog, &fileDialog)); if (defaultSaveName) { fileDialog->SetFileName (UTF8StringHelper (defaultSaveName)); } } else { hr = CoCreateInstance (CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARG(IFileDialog, &fileDialog)); if (SUCCEEDED (hr)) { if (style == kSelectDirectory) { DWORD dwOptions = 0; hr = fileDialog->GetOptions (&dwOptions); if (SUCCEEDED (hr)) hr = fileDialog->SetOptions (dwOptions | FOS_PICKFOLDERS); if (FAILED (hr)) { fileDialog->Release (); fileDialog = 0; return false; } } if (allowMultiFileSelection) { DWORD dwOptions = 0; hr = fileDialog->GetOptions (&dwOptions); if (SUCCEEDED (hr)) hr = fileDialog->SetOptions (dwOptions | FOS_ALLOWMULTISELECT); if (FAILED (hr)) { fileDialog->Release (); fileDialog = 0; return false; } } } } if (FAILED (hr)) { fileDialog = 0; return false; } if (title) hr = fileDialog->SetTitle (UTF8StringHelper (title)); DWORD numExtensions = 0; DWORD defaultFileTypeIndex = 0; COMDLG_FILTERSPEC* filters = buildExtensionFilter (extensions, defaultExtension, numExtensions, defaultFileTypeIndex); if (filters) { fileDialog->SetFileTypes (numExtensions, filters); if (defaultFileTypeIndex) fileDialog->SetFileTypeIndex (defaultFileTypeIndex); } if (initialPath && _SHCreateItemFromParsingName) { IShellItem* shellItem; hr = _SHCreateItemFromParsingName (UTF8StringHelper (initialPath), 0, IID_PPV_ARG (IShellItem, &shellItem)); if (SUCCEEDED (hr)) { fileDialog->SetFolder (shellItem); shellItem->Release (); } } Win32Frame* win32Frame = frame->getPlatformFrame () ? dynamic_cast<Win32Frame*> (frame->getPlatformFrame ()) : 0; hr = fileDialog->Show (win32Frame ? win32Frame->getPlatformWindow () : 0); if (SUCCEEDED (hr)) { if (allowMultiFileSelection) { IFileOpenDialog* openFileDialog = 0; hr = fileDialog->QueryInterface (IID_PPV_ARG(IFileOpenDialog, &openFileDialog)); if (SUCCEEDED (hr)) { IShellItemArray* items; hr = openFileDialog->GetResults (&items); if (SUCCEEDED (hr)) { DWORD count; hr = items->GetCount (&count); for (DWORD i = 0; i < count; i++) { IShellItem* item; hr = items->GetItemAt (i, &item); if (SUCCEEDED (hr)) { LPWSTR filesysPath = 0; hr = item->GetDisplayName (SIGDN_FILESYSPATH, &filesysPath); if (SUCCEEDED (hr)) { UTF8StringHelper str (filesysPath); UTF8StringBuffer resultPath = String::newWithString (str); result.push_back (resultPath); } item->Release (); } } items->Release (); } openFileDialog->Release (); } } else { IShellItem *item; hr = fileDialog->GetResult (&item); if (SUCCEEDED (hr)) { LPWSTR filesysPath = 0; hr = item->GetDisplayName (SIGDN_FILESYSPATH, &filesysPath); if (SUCCEEDED (hr)) { UTF8StringHelper str (filesysPath); UTF8StringBuffer resultPath = String::newWithString (str); result.push_back (resultPath); } item->Release (); } } } fileDialog->Release (); fileDialog = 0; freeExtensionFilter (filters); return SUCCEEDED (hr); }
void KPR_system_openDirectory(xsMachine* the) { HRESULT hr; xsIntegerValue argc = xsToInteger(xsArgc); IFileOpenDialog *pFileOpen = NULL; IShellItem *pItem = NULL; xsStringValue string; PWSTR wideString = NULL; xsIntegerValue urlLength; xsStringValue url = NULL; hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileOpenDialog, (LPVOID *)&pFileOpen); if (!SUCCEEDED(hr)) goto bail; if ((argc > 0) && xsTest(xsArg(0))) { if (xsFindString(xsArg(0), xsID_prompt, &string)) { wideString = xsStringToWideString(string); hr = pFileOpen->SetTitle(wideString); if (!SUCCEEDED(hr)) goto bail; CoTaskMemFree(wideString); wideString = NULL; } if (xsFindString(xsArg(0), xsID_message, &string)) { wideString = xsStringToWideString(string); hr = pFileOpen->SetTitle(wideString); if (!SUCCEEDED(hr)) goto bail; CoTaskMemFree(wideString); wideString = NULL; } if (xsFindString(xsArg(0), xsID_url, &string)) { wideString = xsStringToWideString(string); hr = SHCreateItemFromParsingName(wideString, NULL, IID_IShellItem, (LPVOID *)&pItem); if (!SUCCEEDED(hr)) goto bail; CoTaskMemFree(wideString); wideString = NULL; hr = pFileOpen->SetFolder(pItem); if (!SUCCEEDED(hr)) goto bail; pItem->Release(); pItem = NULL; } } hr = pFileOpen->SetOptions(FOS_PICKFOLDERS); if (!SUCCEEDED(hr)) goto bail; hr = pFileOpen->Show(GetForegroundWindow()); if (!SUCCEEDED(hr)) goto bail; hr = pFileOpen->GetResult(&pItem); if (!SUCCEEDED(hr)) goto bail; hr = pItem->GetDisplayName(SIGDN_URL, &wideString); if (!SUCCEEDED(hr)) goto bail; urlLength = WideCharToMultiByte(CP_UTF8, 0, wideString, -1, NULL, 0, NULL, NULL); FskMemPtrNew(urlLength + 1, &url); WideCharToMultiByte(CP_UTF8, 0, wideString, -1, url, urlLength, NULL, NULL); url[urlLength - 1] = '/'; url[urlLength] = 0; (void)xsCallFunction1(xsArg(1), xsNull, xsString(url)); bail: FskMemPtrDispose(url); if (wideString) CoTaskMemFree(wideString); if (pItem) pItem->Release(); if (pFileOpen) pFileOpen->Release(); }
CBrowseFolder::retVal CBrowseFolder::Show(HWND parent, CString& path, const CString& sDefaultPath /* = CString() */) { retVal ret = OK; //assume OK m_sDefaultPath = sDefaultPath; if (m_sDefaultPath.IsEmpty() && !path.IsEmpty()) { while (!PathFileExists(path) && !path.IsEmpty()) { CString p = path.Left(path.ReverseFind(L'\\')); if ((p.GetLength() == 2) && (p[1] == L':')) { p += L"\\"; if (p.Compare(path) == 0) p.Empty(); } if (p.GetLength() < 2) p.Empty(); path = p; } // if the result path already contains a path, use that as the default path m_sDefaultPath = path; } HRESULT hr; // Create a new common open file dialog IFileOpenDialog* pfd = NULL; hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfd)); if (SUCCEEDED(hr)) { // Set the dialog as a folder picker DWORD dwOptions; if (SUCCEEDED(hr = pfd->GetOptions(&dwOptions))) { hr = pfd->SetOptions(dwOptions | FOS_PICKFOLDERS | FOS_FORCEFILESYSTEM | FOS_PATHMUSTEXIST); } // Set a title if (SUCCEEDED(hr)) { TCHAR * nl = _tcschr(m_title, '\n'); if (nl) *nl = 0; pfd->SetTitle(m_title); } // set the default folder if (SUCCEEDED(hr)) { typedef HRESULT(WINAPI *SHCIFPN)(PCWSTR pszPath, IBindCtx * pbc, REFIID riid, void ** ppv); SHCIFPN pSHCIFPN = hLib.GetProcAddress<SHCIFPN>("SHCreateItemFromParsingName"); if (pSHCIFPN) { IShellItem *psiDefault = 0; hr = pSHCIFPN(m_sDefaultPath, NULL, IID_PPV_ARGS(&psiDefault)); if (SUCCEEDED(hr)) { hr = pfd->SetFolder(psiDefault); psiDefault->Release(); } } } // Show the open file dialog if (SUCCEEDED(hr) && SUCCEEDED(hr = pfd->Show(parent))) { // Get the selection from the user IShellItem* psiResult = NULL; hr = pfd->GetResult(&psiResult); if (SUCCEEDED(hr)) { PWSTR pszPath = NULL; hr = psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszPath); if (SUCCEEDED(hr)) { path = pszPath; CoTaskMemFree(pszPath); } psiResult->Release(); } else ret = CANCEL; } else ret = CANCEL; pfd->Release(); } else { BROWSEINFO browseInfo = {}; browseInfo.hwndOwner = parent; browseInfo.pidlRoot = m_root; browseInfo.pszDisplayName = m_displayName; browseInfo.lpszTitle = m_title; browseInfo.ulFlags = m_style; browseInfo.lParam = reinterpret_cast<LPARAM>(this); PCIDLIST_ABSOLUTE itemIDList = SHBrowseForFolder(&browseInfo); //is the dialog canceled? if (!itemIDList) ret = CANCEL; if (ret != CANCEL) { if (!SHGetPathFromIDList(itemIDList, path.GetBuffer(MAX_PATH))) // MAX_PATH ok. Explorer can't handle paths longer than MAX_PATH. ret = NOPATH; path.ReleaseBuffer(); CoTaskMemFree((LPVOID)itemIDList); } } return ret; }
bool BrowseFolderDialog(HWND hwndOwner,TSTask::String *pDirectory,LPCTSTR pszTitle) { if (pDirectory==nullptr) return false; IFileOpenDialog *pFileOpenDialog; HRESULT hr; hr=::CoCreateInstance(CLSID_FileOpenDialog,nullptr,CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pFileOpenDialog)); if (FAILED(hr)) return false; FILEOPENDIALOGOPTIONS Options; pFileOpenDialog->GetOptions(&Options); pFileOpenDialog->SetOptions(Options | FOS_PICKFOLDERS | FOS_FORCEFILESYSTEM); if (!pDirectory->empty()) { IShellItem *psiFolder; hr=::SHCreateItemFromParsingName(pDirectory->c_str(),nullptr,IID_PPV_ARGS(&psiFolder)); if (SUCCEEDED(hr)) { IShellItem *psiParent; hr=psiFolder->GetParent(&psiParent); if (SUCCEEDED(hr)) { pFileOpenDialog->SetFolder(psiParent); LPWSTR pszName; hr=psiFolder->GetDisplayName(SIGDN_NORMALDISPLAY,&pszName); if (SUCCEEDED(hr)) { pFileOpenDialog->SetFileName(pszName); ::CoTaskMemFree(pszName); } psiParent->Release(); } psiFolder->Release(); } } if (!TSTask::IsStringEmpty(pszTitle)) pFileOpenDialog->SetTitle(pszTitle); bool fOK=false; hr=pFileOpenDialog->Show(hwndOwner); if (SUCCEEDED(hr)) { LPWSTR pszPath; IShellItem *psi; hr=pFileOpenDialog->GetResult(&psi); if (SUCCEEDED(hr)) { hr=psi->GetDisplayName(SIGDN_FILESYSPATH,&pszPath); if (SUCCEEDED(hr)) { *pDirectory=pszPath; fOK=true; ::CoTaskMemFree(pszPath); } psi->Release(); } } pFileOpenDialog->Release(); return fOK; }
QStringList qt_win_CID_get_open_file_names(const QFileDialogArgs &args, QString *initialDirectory, const QStringList &filterList, QString *selectedFilter, int selectedFilterIndex) { QStringList result; QDialog modal_widget; modal_widget.setAttribute(Qt::WA_NoChildEventsForParent, true); modal_widget.setParent(args.parent, Qt::Window); QApplicationPrivate::enterModal(&modal_widget); // Multiple selection is allowed only in IFileOpenDialog. IFileOpenDialog *pfd = 0; HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfd)); if (SUCCEEDED(hr)) { qt_win_set_IFileDialogOptions(pfd, args.selection, args.directory, args.caption, filterList, QFileDialog::ExistingFiles, args.options); // Set the currently selected filter (one-based index). hr = pfd->SetFileTypeIndex(selectedFilterIndex+1); QWidget *parentWindow = args.parent; if (parentWindow) parentWindow = parentWindow->window(); else parentWindow = QApplication::activeWindow(); // Show the file dialog. hr = pfd->Show(parentWindow ? parentWindow->winId() : 0); if (SUCCEEDED(hr)) { // Retrieve the results. IShellItemArray *psiaResults; hr = pfd->GetResults(&psiaResults); if (SUCCEEDED(hr)) { DWORD numItems = 0; psiaResults->GetCount(&numItems); for (DWORD i = 0; i<numItems; i++) { IShellItem *psi = 0; hr = psiaResults->GetItemAt(i, &psi); if (SUCCEEDED(hr)) { // Retrieve the file name from shell item. wchar_t *pszPath; hr = psi->GetDisplayName(SIGDN_FILESYSPATH, &pszPath); if (SUCCEEDED(hr)) { QString fileName = QString::fromWCharArray(pszPath); result.append(fileName); CoTaskMemFree(pszPath); } psi->Release(); // Free the current item. } } psiaResults->Release(); // Free the array of items. } } } QApplicationPrivate::leaveModal(&modal_widget); qt_win_eatMouseMove(); if (!result.isEmpty()) { // Retrieve the current folder name. IShellItem *psi = 0; hr = pfd->GetFolder(&psi); if (SUCCEEDED(hr)) { wchar_t *pszPath; hr = psi->GetDisplayName(SIGDN_FILESYSPATH, &pszPath); if (SUCCEEDED(hr)) { *initialDirectory = QString::fromWCharArray(pszPath); CoTaskMemFree(pszPath); } psi->Release(); } // Retrieve the currently selected filter. if (selectedFilter) { quint32 filetype = 0; hr = pfd->GetFileTypeIndex(&filetype); if (SUCCEEDED(hr) && filetype && filetype <= (quint32)filterList.length()) { // This is a one-based index, not zero-based. *selectedFilter = filterList[filetype-1]; } } } if (pfd) pfd->Release(); return result; }
static INT_PTR CALLBACK DlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { DWORD id; // NMHDR *p; switch (uMsg) { case WM_COMMAND: id = LOWORD(wParam); if (id == IDOK || id == IDCANCEL) { if (id == IDOK) { PC_SetPluginVar(m_dwPluginID, VAR_TEXT_COLOR, dwTextColor); PC_SetPluginVar(m_dwPluginID, VAR_BACKGROUND_COLOR, dwBackgroundColor); PC_SetPluginVar(m_dwPluginID, VAR_TEXT_FILE, szFileName.c_str()); PC_SetPluginVar(m_dwPluginID, VAR_TEXT_FONT_FAMILY, szFontFamily.c_str()); PC_SetPluginVar(m_dwPluginID, VAR_TEXT_FILE, szFileName.c_str()); PC_SetPluginVar(m_dwPluginID, VAR_TEXT_FONT_SIZE, dwFontSize); PC_SetPluginVar(m_dwPluginID, VAR_SHOW_BACKGROUND, Button_GetCheck(GetDlgItem(hwnd, IDC_SHOW_BACKGROUND)) == BST_CHECKED); int style = (bBold ? 1 : 0) | (bItalic ? 2 : 0); PC_SetPluginVar(m_dwPluginID, VAR_TEXT_FONT_STYLE, style); } EndDialog(hwnd, id); } if (id == IDC_TEXT_COLOR_BTN || id == IDC_BACKGROUND_COLOR_BTN) { DWORD &color_var = (id == IDC_TEXT_COLOR_BTN) ? dwTextColor : dwBackgroundColor; COLORREF custColors[16]; CHOOSECOLOR cc = { 0 }; cc.lStructSize = sizeof(cc); cc.hwndOwner = hwnd; cc.rgbResult = color_var; cc.Flags = CC_FULLOPEN | CC_RGBINIT; cc.lpCustColors = custColors; if (ChooseColor(&cc)) { color_var = cc.rgbResult; InvalidateRect(hwnd, 0, 0); } } if (id == IDC_TEXT_FONT) { LOGFONT lf = { 0 }; lf.lfItalic = bItalic; wcscpy_s(lf.lfFaceName, szFontFamily.c_str()); HDC dc = GetDC(hwnd); lf.lfHeight = -MulDiv(dwFontSize, GetDeviceCaps(dc, LOGPIXELSY), 72); lf.lfWeight = 400; CHOOSEFONT cf = { 0 }; cf.lStructSize = sizeof(cf); cf.hwndOwner = hwnd; cf.lpLogFont = &lf; cf.iPointSize = dwFontSize * 10; cf.Flags = CF_FORCEFONTEXIST | CF_INITTOLOGFONTSTRUCT | CF_NOSCRIPTSEL; if (ChooseFont(&cf)) { bItalic = lf.lfItalic != 0; bBold = lf.lfWeight > 500; szFontFamily = lf.lfFaceName; dwFontSize = (-lf.lfHeight * 72) / GetDeviceCaps(dc, LOGPIXELSY); SetFontButtonText(hwnd); } ReleaseDC(hwnd, dc); } if (id == IDC_TEXT_FILE) { HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); if (SUCCEEDED(hr)) { IFileOpenDialog *pFileOpen; COMDLG_FILTERSPEC rgSpec[] = { { L"Text Files", L"*.txt" }, { L"All Files", L"*.*" }, }; // Create the FileOpenDialog object. hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen)); if (SUCCEEDED(hr)) { // Show the Open dialog box. pFileOpen->SetFileTypes(2, rgSpec); hr = pFileOpen->Show(NULL); // Get the file name from the dialog box. if (SUCCEEDED(hr)) { IShellItem *pItem; hr = pFileOpen->GetResult(&pItem); if (SUCCEEDED(hr)) { PWSTR pszFilePath; hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); // Display the file name to the user. if (SUCCEEDED(hr)) { //MessageBox(NULL, pszFilePath, L"File Path", MB_OK); //szFileName = L"CATS"; szFileName = pszFilePath; SetFileNameButtonText(hwnd); CoTaskMemFree(pszFilePath); } pItem->Release(); } } pFileOpen->Release(); } CoUninitialize(); } } break; case WM_NOTIFY: break; case WM_DRAWITEM: { auto lpDIS = (LPDRAWITEMSTRUCT)lParam; if (lpDIS->CtlID == IDC_TEXT_COLOR_BTN) DrawColorButton(lpDIS, dwTextColor); if (lpDIS->CtlID == IDC_BACKGROUND_COLOR_BTN) DrawColorButton(lpDIS, dwBackgroundColor); break; } case WM_INITDIALOG: InitSettingsDialog(hwnd); return true; } return false; }
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; }