Skip to content

Commit

Permalink
打开对话框改成使用IFileDialog #1582
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongyang219 committed Jan 20, 2025
1 parent 3da2c85 commit c08ef11
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 2 deletions.
112 changes: 112 additions & 0 deletions TrafficMonitor/FileDialogEx.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include "stdafx.h"
#include "FileDialogEx.h"
#include <sstream>

CFileDialogEx::CFileDialogEx(BOOL bOpenFileDialog, LPCWSTR lpszDefExt, LPCWSTR lpszFilter)
: pFileDialog(nullptr), m_bOpenFileDialog(bOpenFileDialog)
{
// 创建 IFileDialog 实例
HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pFileDialog));

if (pFileDialog)
{
// 设置对话框选项
DWORD dwOptions;
pFileDialog->GetOptions(&dwOptions);
pFileDialog->SetOptions(dwOptions | FOS_FORCEFILESYSTEM); // 仅文件系统项

if (bOpenFileDialog)
{
pFileDialog->SetOptions(dwOptions | FOS_FILEMUSTEXIST); // 打开文件时文件必须存在
}

// 设置默认扩展名
if (lpszDefExt)
{
pFileDialog->SetDefaultExtension(lpszDefExt);
}

// 解析并设置过滤器
if (lpszFilter)
{
ParseFilter(lpszFilter);
for (size_t i = 0; i < m_filterDescriptions.size() && i < m_filterSpecsStrings.size(); i++)
{
COMDLG_FILTERSPEC filterSpec = { m_filterDescriptions[i].c_str(), m_filterSpecsStrings[i].c_str()};
m_filterSpecs.push_back(filterSpec);
}
pFileDialog->SetFileTypes(m_filterSpecs.size(), m_filterSpecs.data());
}
}
}

CFileDialogEx::~CFileDialogEx()
{
if (pFileDialog)
{
pFileDialog->Release();
}
}

int CFileDialogEx::DoModal(HWND hWndOwner)
{
if (!pFileDialog) return -1;

// 显示对话框
HRESULT hr = pFileDialog->Show(hWndOwner);
if (FAILED(hr))
return -1;

// 获取选定的文件路径
IShellItem* pItem = nullptr;
hr = pFileDialog->GetResult(&pItem);
if (SUCCEEDED(hr))
{
PWSTR pszFilePath = nullptr;
hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
if (SUCCEEDED(hr))
{
m_filePath = pszFilePath;
CoTaskMemFree(pszFilePath);
}
pItem->Release();
}

return (SUCCEEDED(hr)) ? 1 : 0;
}

std::wstring CFileDialogEx::GetPathName() const
{
return m_filePath;
}

std::wstring CFileDialogEx::GetFileName() const
{
// 获取文件名
size_t pos = m_filePath.find_last_of(L"\\");
if (pos != std::wstring::npos) {
return m_filePath.substr(pos + 1);
}
return m_filePath;
}

void CFileDialogEx::ParseFilter(LPCWSTR lpszFilter)
{
m_filterSpecs.clear();
m_filterDescriptions.clear();
m_filterSpecsStrings.clear();

// 使用 | 分割过滤器字符串
std::wstringstream filterStream(lpszFilter);
std::wstring description, spec;

while (std::getline(filterStream, description, L'|'))
{
if (std::getline(filterStream, spec, L'|'))
{
// 将描述和模式存储在 m_filterDescriptions 和 m_filterSpecsStrings 中
m_filterDescriptions.push_back(description);
m_filterSpecsStrings.push_back(spec);
}
}
}
25 changes: 25 additions & 0 deletions TrafficMonitor/FileDialogEx.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

class CFileDialogEx {
public:
CFileDialogEx(BOOL bOpenFileDialog, LPCWSTR lpszDefExt = NULL, LPCWSTR lpszFilter = NULL);
~CFileDialogEx();

// 显示对话框
int DoModal(HWND hWndOwner = NULL);

// 获取选定的文件路径和文件名
std::wstring GetPathName() const;
std::wstring GetFileName() const;

private:
void ParseFilter(LPCWSTR lpszFilter);

private:
IFileDialog* pFileDialog;
std::wstring m_filePath;
BOOL m_bOpenFileDialog;
std::vector<COMDLG_FILTERSPEC> m_filterSpecs; // 存储过滤器描述和模式
std::vector<std::wstring> m_filterDescriptions; // 存储过滤器描述
std::vector<std::wstring> m_filterSpecsStrings; // 存储过滤器模式
};
3 changes: 2 additions & 1 deletion TrafficMonitor/MainWndSettingsDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "afxdialogex.h"
#include "CMFCColorDialogEx.h"
#include "DisplayTextSettingDlg.h"
#include "FileDialogEx.h"

// CMainWndSettingsDlg 对话框

Expand Down Expand Up @@ -500,7 +501,7 @@ void CMainWndSettingsDlg::OnBnClickedBrowseButton()
{
// TODO: 在此添加控件通知处理程序代码
CString szFilter = CCommon::LoadText(IDS_EXE_FILTER);
CFileDialog fileDlg(TRUE, NULL, NULL, 0, szFilter, this);
CFileDialogEx fileDlg(TRUE, NULL, szFilter);
if (IDOK == fileDlg.DoModal())
{
m_data.double_click_exe = fileDlg.GetPathName();
Expand Down
3 changes: 2 additions & 1 deletion TrafficMonitor/TaskBarSettingsDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "SetItemOrderDlg.h"
#include "WindowsSettingHelper.h"
#include "TrafficMonitorDlg.h"
#include "FileDialogEx.h"

// CTaskBarSettingsDlg 对话框

Expand Down Expand Up @@ -677,7 +678,7 @@ void CTaskBarSettingsDlg::OnBnClickedBrowseButton()
{
// TODO: 在此添加控件通知处理程序代码
CString szFilter = CCommon::LoadText(IDS_EXE_FILTER);
CFileDialog fileDlg(TRUE, NULL, NULL, 0, szFilter, this);
CFileDialogEx fileDlg(TRUE, NULL, szFilter);
if (IDOK == fileDlg.DoModal())
{
m_data.double_click_exe = fileDlg.GetPathName();
Expand Down
2 changes: 2 additions & 0 deletions TrafficMonitor/TrafficMonitor.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,7 @@
<ClInclude Include="DrawCommonFactory.h" />
<ClInclude Include="DrawTextManager.h" />
<ClInclude Include="Dxgi1Support2.h" />
<ClInclude Include="FileDialogEx.h" />
<ClInclude Include="FilePathHelper.h" />
<ClInclude Include="HighResolutionTimer.h" />
<ClInclude Include="HistoryTrafficCalendarDlg.h" />
Expand Down Expand Up @@ -881,6 +882,7 @@
<ClCompile Include="DrawCommonFactory.cpp" />
<ClCompile Include="DrawTextManager.cpp" />
<ClCompile Include="Dxgi1Support2.cpp" />
<ClCompile Include="FileDialogEx.cpp" />
<ClCompile Include="FilePathHelper.cpp" />
<ClCompile Include="HistoryTrafficCalendarDlg.cpp" />
<ClCompile Include="HistoryTrafficFile.cpp" />
Expand Down
9 changes: 9 additions & 0 deletions TrafficMonitor/TrafficMonitor.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@
<Filter Include="源文件和头文件\公共的类\WindowsWebExperienceDetector">
<UniqueIdentifier>{4f233c31-4fef-48c1-8176-845ddd21debb}</UniqueIdentifier>
</Filter>
<Filter Include="源文件和头文件\公共的类\FileDialogEx">
<UniqueIdentifier>{0745b192-f366-4a31-b238-d372d5b62492}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<Text Include="ReadMe.txt" />
Expand Down Expand Up @@ -493,6 +496,9 @@
<ClInclude Include="SkinAutoAdaptSettingDlg.h">
<Filter>源文件和头文件\对话框类\SkinDlg</Filter>
</ClInclude>
<ClInclude Include="FileDialogEx.h">
<Filter>源文件和头文件\头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="StaticEx.cpp">
Expand Down Expand Up @@ -729,6 +735,9 @@
<ClCompile Include="SkinAutoAdaptSettingDlg.cpp">
<Filter>源文件和头文件\对话框类\SkinDlg</Filter>
</ClCompile>
<ClCompile Include="FileDialogEx.cpp">
<Filter>源文件和头文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="TrafficMonitor.rc">
Expand Down

0 comments on commit c08ef11

Please sign in to comment.