如何设置 IFileOpenDialog 接口的默认文件夹

huangapple go评论79阅读模式
英文:

How to set default folder for IFileOpenDialog interface

问题

我使用MFC界面的IFileOpenDialog来选择源数据文件。显然,对话框在上次使用的目录(文件夹)中打开。
我想在用户发送到函数的文件夹中打开对话框(在参数lsCurFolder中指定的路径)。我还没有找到如何实现这一点。

英文:

I use the MFC interface IFileOpenDialog for selecting the source data file. Obviously, dialog is open in last used directory (folder).
I want to open the dialog in folder, the user send to function (the path in the parameter lsCurFolder). I have not find how to do it.

inline bool OpenFile(LPTSTR& pszFilePath, const FileType fTyp = MajCut, LPCTSTR lsCurFolder) {

	HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED |
								COINIT_DISABLE_OLE1DDE);
	if (SUCCEEDED(hr))
	{
		IFileOpenDialog* pFileDlg = NULL;

		// Create the FileOpenDialog object.
		hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL,
							  IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileDlg));
		if (!SetTyp(fTyp, pFileDlg, L"Vyhledej soubor pro načtení"))
		{
			pFileDlg->Release();
			return false;
		}

		FILEOPENDIALOGOPTIONS opt;
		pFileDlg->GetOptions(&opt);
		if (pFileDlg->SetOptions(opt | FOS_STRICTFILETYPES) != S_OK)
		{
			pFileDlg->Release();
			return false;
		}

		if (SUCCEEDED(hr))
		{
			// Show the Open dialog box.
			hr = pFileDlg->Show(NULL);

			// Get the file name from the dialog box.
			if (SUCCEEDED(hr))
			{
				IShellItem* pItem;
				hr = pFileDlg->GetResult(&pItem);
				if (SUCCEEDED(hr))
				{
					hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); //Volající musí zrušit volání voláním CoTaskMemFree
					pItem->Release();
				}
			}
			pFileDlg->Release();
		}
		CoUninitialize();
	}
	return SUCCEEDED(hr);
}

答案1

得分: 3

根据控制默认文件夹,可以使用Shell命名空间中的几乎任何文件夹作为对话框的默认文件夹(用户选择打开或保存文件时呈现的文件夹)。在调用 Show 之前调用 IFileDialog::SetDefaultFolder 来实现这一点。

默认文件夹是对话框首次由用户从应用程序打开时启动的文件夹。之后,对话框将在用户上次打开的文件夹或上次用于保存项目的文件夹中打开。有关更多详细信息,请参阅状态保持

您可以通过调用 IFileDialog::SetFolder 强制对话框始终在打开时显示相同的文件夹,而不考虑先前的用户操作。 但我们不建议这样做。如果在显示对话框框之前调用 SetFolder,则不会显示用户最近保存或打开的位置。除非有非常具体的原因需要此行为,否则这不是一个好的或预期的用户体验,应该避免使用。在几乎所有情况下,IFileDialog::SetDefaultFolder 是更好的方法。

英文:

Per Controlling the Default Folder:

> Almost any folder in the Shell namespace can be used as the default folder for the dialog (the folder presented when the user chooses to open or save a file). Call IFileDialog::SetDefaultFolder prior to calling Show to do so.
>
> The default folder is the folder in which the dialog starts the first time a user opens it from your application. After that, the dialog will open in the last folder a user opened or the last folder they used to save an item. See State Persistence for more details.
>
> You can force the dialog to always show the same folder when it opens, regardless of previous user action, by calling IFileDialog::SetFolder. However, we do not recommended doing this. If you call SetFolder before you display the dialog box, the most recent location that the user saved to or opened from is not shown. Unless there is a very specific reason for this behavior, it is not a good or expected user experience and should be avoided. In almost all instances, IFileDialog::SetDefaultFolder is the better method.

So, for example:

...
IShellItem *pCurFolder = NULL; 
hr = SHCreateItemFromParsingName(lsCurFolder, NULL, IID_PPV_ARGS(&pCurFolder));
if (SUCCEEDED(hr))
{
	pFileDlg->SetFolder(pCurFolder);
	pCurFolder->Release();
}
hr = pFileDlg->Show(NULL);
...

huangapple
  • 本文由 发表于 2023年5月22日 20:43:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76306324.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定