gpt4 book ai didi

c++ - 在 Windows 7 上使用 IFileDialog 的问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:33:34 25 4
gpt4 key购买 nike

在我的 Windows 7 或 Vista 上运行的 MFC Windows 应用程序中使用通用项对话框时,我遇到了一些奇怪的(至少对我而言)行为。

根据 MSDN http://msdn.microsoft.com/en-us/library/windows/desktop/bb776913(v=vs.85).aspx我正在使用新界面来显示文件打开和保存对话框:

bool OpenFileDialog(CString& strFile, CString strTitle, CStringArray& astrFilter, CStringArray& astrFilterExtension, ULONG nFlags, HWND hParentWnd)
{
USES_CONVERSION;

INT_PTR nResult = 0;
INT_PTR nFilterCount = astrFilter.GetCount();

IFileDialog* pfod = 0;
HRESULT hr = ::CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfod));

if(SUCCEEDED(hr))
{
// New dialog starting with Vista/Windows 7
COMDLG_FILTERSPEC* pOpenTypes = 0;

if((nFilterCount > 0) && (nFilterCount == astrFilterExtension.GetCount()))
{
pOpenTypes = new COMDLG_FILTERSPEC[nFilterCount];

for(int nIdx = 0; nIdx < nFilterCount; nIdx++)
{
pOpenTypes[nIdx].pszName = astrFilter[nIdx].GetBuffer();
pOpenTypes[nIdx].pszSpec = astrFilterExtension[nIdx].GetBuffer();
}
}

// Set the file types to display.
if(pOpenTypes)
{
hr = pfod->SetFileTypes(nFilterCount, pOpenTypes);

if(SUCCEEDED(hr))
hr = pfod->SetFileTypeIndex(0);
}

if(!strFile.IsEmpty())
pfod->SetFileName(strFile);

if(!strTitle.IsEmpty())
pfod->SetTitle(strTitle);

if(SUCCEEDED(hr))
{
// Ensure the dialog only returns file system paths.
DWORD dwFlags;
hr = pfod->GetOptions(&dwFlags);

if(SUCCEEDED(hr))
{
dwFlags |= FOS_FORCEFILESYSTEM;

if(nFlags & OFN_FILEMUSTEXIST)
dwFlags |= FOS_FILEMUSTEXIST;

if(nFlags & OFN_PATHMUSTEXIST)
dwFlags |= FOS_PATHMUSTEXIST;

hr = pfod->SetOptions(dwFlags);

if(SUCCEEDED(hr))
{
// Create an event handling object, and hook it up to the dialog.
IFileDialogEvents* pfde = NULL;
DWORD dwCookie;

// Actually only added for debugging purposes

/*hr = CDialogEventHandler_CreateInstance(IID_PPV_ARGS(&pfde));

if(SUCCEEDED(hr))
{
// Hook up the event handler.
hr = pfod->Advise(pfde, &dwCookie);

if(!SUCCEEDED(hr))
{
pfde->Release();
pfde = 0;
}
}*/

// Now show the dialog. Usually called with hParent == 0
if(hParentWnd)
hr = pfod->Show(::GetWindow(hParentWnd, GW_OWNER));
else
hr = pfod->Show(0);

// do something with the path when the dialog was closed...

所以如果我想从普通驱动器中选择一个文件,对话框就会出现并且工作正常。我可以浏览文件夹并选择我想要的任何文件。离开对话框时,我还获得了正确的文件信息。

但它不适用于左侧导航 Pane 中的库之一。每当我尝试选择文档、视频或图片等库时,对话框不会更新显示文件夹/库内容的右侧 Pane 。

我注意到,在文件打开/保存对话框中单击库时,会触发 IFileDialogEvents 接口(interface)的 OnFolderChanging() 事件,但不会触发 OnFolderChange() 和 OnSelectionChange()。如果我在像 C 这样的“正常”驱动器上单击并导航,则会触发这些事件。

我还尝试在我的 InitInstance 方法中尽早调用对话框,以避免对我的其他代码产生可能的副作用,但这也无济于事。

有没有人有同样的行为并且能够解决这个问题?

非常感谢!

最佳答案

于是我终于找到了这个问题的答案。为应用程序创建新的 MFC 项目是解决此问题的实际提示。原因是“Stack reserve size”太大了。旧的VS6.0 项目中的设置将堆栈大小增加到100MB 以上。显然,当保留的堆栈大小太大时,基于 IFileDialog 的对话框无法正常工作(其他事情可能也无法按预期工作)。因此,在我的案例中,我不得不将其设置回 15MB。

关于c++ - 在 Windows 7 上使用 IFileDialog 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8855971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com