-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowsUtils.cpp
More file actions
97 lines (83 loc) · 2.91 KB
/
WindowsUtils.cpp
File metadata and controls
97 lines (83 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "pch.h"
#include "WindowsUtils.h"
// 保存ダイアログ
bool WindowsUtils::SaveDialog(const std::string& extension, const std::string& extensionDesc, std::string& result)
{
// wstring <-> string 変換クラス
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> cv;
// フィルター文字列定義
std::wstringstream filter;
std::wstring wextension = cv.from_bytes(extension);
filter << cv.from_bytes(extensionDesc) << L" (*." << wextension << L")" << std::ends;
filter << L"*." << wextension << std::ends;
filter << L"All Files" << L" (*." << L"*" << L")" << std::ends;
filter << L"*." << L"*" << std::ends;
filter << std::ends << std::ends;
std::wstring filterstr = filter.str();
// 忘れるとデフォルトファイル名に変な文字列が表示される
wchar_t filename[256] = L"\0";
// 構造体を0でクリア
// ファイルダイアログ設定
OPENFILENAMEW ofn = { 0 };
ofn.lStructSize = sizeof(OPENFILENAMEW);
ofn.lpstrFilter = filterstr.c_str();
ofn.lpstrFile = filename;
ofn.nMaxFile = sizeof(filename);
ofn.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
ofn.lpstrDefExt = wextension.c_str();
// ダイアログ表示
bool b = (GetSaveFileNameW(&ofn) == TRUE);
result = cv.to_bytes(std::wstring(filename));
return b;
}
// 開くダイアログ
bool WindowsUtils::OpenDialog(const std::string& extension, const std::string& extensionDesc, std::string& result)
{
// wstring <-> string 変換クラス
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> cv;
// フィルター文字列定義
std::wstringstream filter;
std::wstring wextension = cv.from_bytes(extension);
filter << cv.from_bytes(extensionDesc) << L" (*." << wextension << L")" << std::ends;
filter << L"*." << wextension << std::ends;
filter << L"All Files" << L" (*." << L"*" << L")" << std::ends;
filter << L"*." << L"*" << std::ends;
filter << std::ends << std::ends;
std::wstring filterstr = filter.str();
// 忘れるとデフォルトファイル名に変な文字列が表示される
wchar_t filename[256] = L"\0";
// 構造体を0でクリア
// ファイルダイアログ設定
OPENFILENAMEW ofn = { 0 };
ofn.lStructSize = sizeof(OPENFILENAMEW);
ofn.lpstrFilter = filterstr.c_str();
ofn.lpstrFile = filename;
ofn.nMaxFile = sizeof(filename);
ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = wextension.c_str();
// ダイアログ表示
bool b = (GetOpenFileNameW(&ofn) == TRUE);
result = cv.to_bytes(std::wstring(filename));
return b;
}
// ファイルネーム取り出し
std::string WindowsUtils::GetFileName(const std::string& name, const std::string& extension)
{
std::string filename = name;
// ディレクトリのパスを除去
// ピリオドがディレクトリ名に入っていることもあるので先に除去
const size_t last_slash_idx = filename.find_last_of("\\/");
if (std::string::npos != last_slash_idx)
{
filename.erase(0, last_slash_idx + 1);
}
// 拡張子があれば取り除く
{
const size_t period_idx = filename.rfind("." + extension);
if (std::string::npos != period_idx)
{
filename.erase(period_idx);
}
}
return filename;
}