Commit 3751fb7a authored by captainwong's avatar captainwong

添加获取系统特殊文件夹功能;另存为可设置默认文件夹、默认文件名

parent afce9bf3
#pragma once #pragma once
#include <Windows.h>
#include <string>
namespace jlib { namespace jlib {
inline bool get_file_open_dialog_result(std::wstring& path, inline bool get_file_open_dialog_result(std::wstring& path,
HWND hWnd = nullptr, HWND hWnd = nullptr,
const std::wstring& default_folder = L"",
const std::wstring& ext = L"", const std::wstring& ext = L"",
UINT cFileTypes = 0, UINT cFileTypes = 0,
const COMDLG_FILTERSPEC *rgFilterSpec = nullptr) { const COMDLG_FILTERSPEC *rgFilterSpec = nullptr) {
...@@ -20,8 +23,17 @@ inline bool get_file_open_dialog_result(std::wstring& path, ...@@ -20,8 +23,17 @@ inline bool get_file_open_dialog_result(std::wstring& path,
IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen)); IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen));
if (SUCCEEDED(hr)) { if (SUCCEEDED(hr)) {
if (!default_folder.empty()) {
IShellItem *psiFolder;
LPCWSTR szFilePath = SysAllocStringLen(default_folder.data(), default_folder.size());
hr = SHCreateItemFromParsingName(szFilePath, NULL, IID_PPV_ARGS(&psiFolder));
if (SUCCEEDED(hr))
hr = pFileOpen->SetDefaultFolder(psiFolder);
}
if (!ext.empty()) { if (!ext.empty()) {
pFileOpen->SetDefaultExtension(ext.c_str()); pFileOpen->SetDefaultExtension(ext.data());
} }
if (cFileTypes != 0 && rgFilterSpec) { if (cFileTypes != 0 && rgFilterSpec) {
...@@ -63,6 +75,8 @@ inline bool get_file_open_dialog_result(std::wstring& path, ...@@ -63,6 +75,8 @@ inline bool get_file_open_dialog_result(std::wstring& path,
inline bool get_save_as_dialog_path(std::wstring& path, inline bool get_save_as_dialog_path(std::wstring& path,
HWND hWnd = nullptr, HWND hWnd = nullptr,
const std::wstring& default_folder = L"",
const std::wstring& default_name = L"",
const std::wstring& ext = L"", const std::wstring& ext = L"",
UINT cFileTypes = 0, UINT cFileTypes = 0,
const COMDLG_FILTERSPEC *rgFilterSpec = nullptr) { const COMDLG_FILTERSPEC *rgFilterSpec = nullptr) {
...@@ -81,8 +95,20 @@ inline bool get_save_as_dialog_path(std::wstring& path, ...@@ -81,8 +95,20 @@ inline bool get_save_as_dialog_path(std::wstring& path,
if (SUCCEEDED(hr)) { if (SUCCEEDED(hr)) {
if (!default_folder.empty()) {
IShellItem *psiFolder;
LPCWSTR szFilePath = SysAllocStringLen(default_folder.data(), default_folder.size());
hr = SHCreateItemFromParsingName(szFilePath, NULL, IID_PPV_ARGS(&psiFolder));
if (SUCCEEDED(hr))
hr = pFileSave->SetDefaultFolder(psiFolder);
}
if (!default_name.empty()) {
pFileSave->SetFileName(default_name.data());
}
if (!ext.empty()) { if (!ext.empty()) {
pFileSave->SetDefaultExtension(ext.c_str()); pFileSave->SetDefaultExtension(ext.data());
} }
if (cFileTypes != 0 && rgFilterSpec) { if (cFileTypes != 0 && rgFilterSpec) {
......
#pragma once #pragma once
#include <string> #include <string>
#include <algorithm> #include <algorithm>
#include <Windows.h>
#include <ShlObj.h>
#pragma comment(lib, "Shell32.lib")
namespace jlib { namespace jlib {
...@@ -38,6 +42,24 @@ inline std::string integrate_path(const std::string& path, char replace_by = '_' ...@@ -38,6 +42,24 @@ inline std::string integrate_path(const std::string& path, char replace_by = '_'
return ret; return ret;
} }
inline std::wstring get_special_folder(int csidl) {
wchar_t path[MAX_PATH] = { 0 };
if (SHGetSpecialFolderPathW(nullptr, path, csidl, false)) {
return std::wstring(path);
}
return std::wstring();
}
inline std::string get_special_folder_a(int csidl) {
char path[MAX_PATH] = { 0 };
if (SHGetSpecialFolderPathA(nullptr, path, csidl, false)) {
return std::string(path);
}
return std::string();
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment