SimpleWindow
载入中...
搜索中...
未找到
FileDialog.h
1#pragma once
2
3#include "List.h"
4#include "Window.h"
5
6namespace sw
7{
11 enum class FileDialogFlags : DWORD {
12 // The File Name list box allows multiple selections. If you also set the OFN_EXPLORER flag,
13 // the dialog box uses the Explorer-style user interface; otherwise, it uses the old-style
14 // user interface.
15 // If the user selects more than one file, the lpstrFile buffer returns the path to the
16 // current directory followed by the file names of the selected files. The nFileOffset member
17 // is the offset, in bytes or characters, to the first file name, and the nFileExtension
18 // member is not used. For Explorer-style dialog boxes, the directory and file name strings
19 // are NULL separated, with an extra NULL character after the last file name. This format
20 // enables the Explorer-style dialog boxes to return long file names that include spaces.
21 // For old-style dialog boxes, the directory and file name strings are separated by spaces
22 // and the function uses short file names for file names with spaces. You can use the
23 // FindFirstFile function to convert between long and short file names.
24 // If you specify a custom template for an old-style dialog box, the definition of the File
25 // Name list box must contain the LBS_EXTENDEDSEL value.
26 AllowMultiSelect = 0x00000200,
27
28 // If the user specifies a file that does not exist, this flag causes the dialog box to
29 // prompt the user for permission to create the file. If the user chooses to create the
30 // file, the dialog box closes and the function returns the specified name; otherwise,
31 // the dialog box remains open. If you use this flag with the OFN_ALLOWMULTISELECT flag,
32 // the dialog box allows the user to specify only one nonexistent file.
33 CreatePrompt = 0x00002000,
34
35 // Prevents the system from adding a link to the selected file in the file system directory
36 // that contains the user's most recently used documents. To retrieve the location of this
37 // directory, call the SHGetSpecialFolderLocation function with the CSIDL_RECENT flag.
38 DontAddTorecent = 0x02000000,
39
40 // Enables the hook function specified in the lpfnHook member.
41 EnableHook = 0x00000020,
42
43 // Causes the dialog box to send CDN_INCLUDEITEM notification messages to your OFNHookProc hook
44 // procedure when the user opens a folder. The dialog box sends a notification for each item in
45 // the newly opened folder. These messages enable you to control which items the dialog box
46 // displays in the folder's item list.
47 EnableIncludeNotify = 0x00400000,
48
49 // Enables the Explorer-style dialog box to be resized using either the mouse or the keyboard.
50 // By default, the Explorer-style Open and Save As dialog boxes allow the dialog box to be resized
51 // regardless of whether this flag is set. This flag is necessary only if you provide a hook
52 // procedure or custom template. The old-style dialog box does not permit resizing.
53 EnableSizing = 0x00800000,
54
55 // The lpTemplateName member is a pointer to the name of a dialog template resource in the module
56 // identified by the hInstance member. If the OFN_EXPLORER flag is set, the system uses the specified
57 // template to create a dialog box that is a child of the default Explorer-style dialog box. If the
58 // OFN_EXPLORER flag is not set, the system uses the template to create an old-style dialog box that
59 // replaces the default dialog box.
60 EnableTemplate = 0x00000040,
61
62 // The hInstance member identifies a data block that contains a preloaded dialog box template.
63 // The system ignores lpTemplateName if this flag is specified. If the OFN_EXPLORER flag is set,
64 // the system uses the specified template to create a dialog box that is a child of the default
65 // Explorer-style dialog box. If the OFN_EXPLORER flag is not set, the system uses the template to
66 // create an old-style dialog box that replaces the default dialog box.
67 EnableTemplateHandle = 0x00000080,
68
69 // Indicates that any customizations made to the Open or Save As dialog box use the Explorer-style
70 // customization methods. For more information, see Explorer-Style Hook Procedures and Explorer-Style
71 // Custom Templates.
72 // By default, the Open and Save As dialog boxes use the Explorer-style user interface regardless of
73 // whether this flag is set. This flag is necessary only if you provide a hook procedure or custom template,
74 // or set the OFN_ALLOWMULTISELECT flag.
75 // If you want the old-style user interface, omit the OFN_EXPLORER flag and provide a replacement old-style
76 // template or hook procedure. If you want the old style but do not need a custom template or hook procedure,
77 // simply provide a hook procedure that always returns FALSE.
78 Explorer = 0x00080000,
79
80 // The user typed a file name extension that differs from the extension specified by lpstrDefExt.
81 // The function does not use this flag if lpstrDefExt is NULL.
82 ExtensionDifferent = 0x00000400,
83
84 // The user can type only names of existing files in the File Name entry field. If this flag is specified and
85 // the user enters an invalid name, the dialog box procedure displays a warning in a message box. If this flag
86 // is specified, the OFN_PATHMUSTEXIST flag is also used. This flag can be used in an Open dialog box. It cannot
87 // be used with a Save As dialog box.
88 FileMustExist = 0x00001000,
89
90 // Forces the showing of system and hidden files, thus overriding the user setting to show or not show hidden
91 // files. However, a file that is marked both system and hidden is not shown.
92 ForceShowHidden = 0x10000000,
93
94 // Hides the Read Only check box.
95 HideReadOnly = 0x00000004,
96
97 // For old-style dialog boxes, this flag causes the dialog box to use long file names. If this flag is not
98 // specified, or if the OFN_ALLOWMULTISELECT flag is also set, old-style dialog boxes use short file names
99 // (8.3 format) for file names with spaces. Explorer-style dialog boxes ignore this flag and always display
100 // long file names.
101 LongNames = 0x00200000,
102
103 // Restores the current directory to its original value if the user changed the directory while searching for files.
104 // This flag is ineffective for GetOpenFileName.
105 NoChangeDir = 0x00000008,
106
107 // Directs the dialog box to return the path and file name of the selected shortcut (.LNK) file. If this value
108 // is not specified, the dialog box returns the path and file name of the file referenced by the shortcut.
109 NoDereferenceLinks = 0x00100000,
110
111 // For old-style dialog boxes, this flag causes the dialog box to use short file names (8.3 format). Explorer-style
112 // dialog boxes ignore this flag and always display long file names.
113 NoLongNames = 0x00040000,
114
115 // Hides and disables the Network button.
116 NoNetworkButton = 0x00020000,
117
118 // The returned file does not have the Read Only check box selected and is not in a write-protected directory.
119 NoReadOnlyReturn = 0x00008000,
120
121 // The file is not created before the dialog box is closed. This flag should be specified if the application saves
122 // the file on a create-nonmodify network share. When an application specifies this flag, the library does not
123 // check for write protection, a full disk, an open drive door, or network protection. Applications using this flag
124 // must perform file operations carefully, because a file cannot be reopened once it is closed.
125 NoTestFileCreate = 0x00010000,
126
127 // The common dialog boxes allow invalid characters in the returned file name. Typically, the calling application
128 // uses a hook procedure that checks the file name by using the FILEOKSTRING message. If the text box in the edit
129 // control is empty or contains nothing but spaces, the lists of files and directories are updated. If the text box
130 // in the edit control contains anything else, nFileOffset and nFileExtension are set to values generated by parsing
131 // the text. No default extension is added to the text, nor is text copied to the buffer specified by lpstrFileTitle.
132 // If the value specified by nFileOffset is less than zero, the file name is invalid. Otherwise, the file name is valid,
133 // and nFileExtension and nFileOffset can be used as if the OFN_NOVALIDATE flag had not been specified.
134 NoValidate = 0x00000100,
135
136 // Causes the Save As dialog box to generate a message box if the selected file already exists. The user must confirm
137 // whether to overwrite the file.
138 OverwritePrompt = 0x00000002,
139
140 // The user can type only valid paths and file names. If this flag is used and the user types an invalid path and
141 // file name in the File Name entry field, the dialog box function displays a warning in a message box.
142 PathMustExist = 0x00000800,
143
144 // Causes the Read Only check box to be selected initially when the dialog box is created. This flag indicates the
145 // state of the Read Only check box when the dialog box is closed.
146 ReadOnly = 0x00000001,
147
148 // Specifies that if a call to the OpenFile function fails because of a network sharing violation, the error is ignored
149 // and the dialog box returns the selected file name. If this flag is not set, the dialog box notifies your hook procedure
150 // when a network sharing violation occurs for the file name specified by the user. If you set the OFN_EXPLORER flag,
151 // the dialog box sends the CDN_SHAREVIOLATION message to the hook procedure. If you do not set OFN_EXPLORER, the dialog
152 // box sends the SHAREVISTRING registered message to the hook procedure.
153 ShareAware = 0x00004000,
154
155 // Causes the dialog box to display the Help button. The hwndOwner member must specify the window to receive the
156 // HELPMSGSTRING registered messages that the dialog box sends when the user clicks the Help button. An Explorer-style
157 // dialog box sends a CDN_HELP notification message to your hook procedure when the user clicks the Help button.
158 ShowHelp = 0x00000010,
159 };
160
164 _SW_ENUM_ENABLE_BIT_OPERATIONS(FileDialogFlags);
165
173 std::wstring name;
174
178 std::wstring filter;
179
183 std::wstring defaultExt;
184 };
185
190 {
191 private:
195 std::vector<wchar_t> _buffer;
196
200 std::vector<std::wstring> _defaultExts;
201
202 public:
206 FileFilter() = default;
207
211 FileFilter(std::initializer_list<FileFilterItem> filters);
212
219 bool AddFilter(const std::wstring &name, const std::wstring &filter, const std::wstring &defaultExt = L"");
220
226 int SetFilter(std::initializer_list<FileFilterItem> filters);
227
231 void Clear();
232
236 wchar_t *GetFilterStr();
237
241 const wchar_t *GetDefaultExt(int index);
242 };
243
247 class FileDialog : public IDialog
248 {
249 private:
253 OPENFILENAMEW _ofn{};
254
258 std::vector<wchar_t> _buffer;
259
263 std::wstring _title;
264
268 std::wstring _initialDir;
269
273 FileFilter _filter;
274
275 public:
280
285
290
295
300
305
310
315
320
321 public:
326
331 void SetFilter(const FileFilter &filter);
332
336 virtual void Close() override;
337
341 virtual void Show() override;
342
347 virtual int ShowDialog(Window *owner = nullptr) override = 0;
348
353 virtual int ShowDialog(Window &owner) = 0;
354
355 protected:
359 OPENFILENAMEW *GetOFN();
360
364 wchar_t *GetBuffer();
365
370
375 virtual void ProcessFileName(std::wstring &fileName);
376 };
377
382 {
383 public:
388
393 virtual int ShowDialog(Window *owner = nullptr) override;
394
399 virtual int ShowDialog(Window &owner) override;
400 };
401
406 {
407 private:
411 std::wstring _initialFileName;
412
413 public:
418
423
428 virtual int ShowDialog(Window *owner = nullptr) override;
429
434 virtual int ShowDialog(Window &owner) override;
435
436 protected:
441 virtual void ProcessFileName(std::wstring &fileName) override;
442
443 private:
447 void _SetInitialFileName();
448 };
449}
“打开文件”对话框与“另存为”对话框的基类
Definition FileDialog.h:248
FileDialog()
初始化FileDialog
const Property< int > FilterIndex
当前筛选器的索引,索引值从0开始
Definition FileDialog.h:304
const Property< bool > MultiSelect
是否允许多选
Definition FileDialog.h:314
const ReadOnlyProperty< sw::List< std::wstring > > FileNames
所有选中的文件路径
Definition FileDialog.h:319
const ReadOnlyProperty< FileFilter * > Filter
筛选器
Definition FileDialog.h:299
const Property< std::wstring > Title
对话框标题,设为空字符串可显示默认标题
Definition FileDialog.h:289
void SetFilter(const FileFilter &filter)
设置筛选器
const Property< int > BufferSize
储存文件名的缓冲区大小,值不能小于MAX_PATH
Definition FileDialog.h:279
virtual int ShowDialog(Window &owner)=0
显示对话框,并指定所有者窗口
void ClearBuffer()
清空缓冲区,显示对话框前必须调用此函数
virtual void ProcessFileName(std::wstring &fileName)
处理文件路径,获取文件路径时会先调用这个函数对返回值进行处理
const ReadOnlyProperty< std::wstring > FileName
选中文件的路径
Definition FileDialog.h:309
virtual void Show() override
FileDialog默认不支持该函数,调用该函数不会执行任何操作
virtual void Close() override
FileDialog默认不支持该函数,调用该函数不会执行任何操作
OPENFILENAMEW * GetOFN()
获取OPENFILENAMEW指针
const Property< std::wstring > InitialDir
初始目录
Definition FileDialog.h:294
wchar_t * GetBuffer()
获取指向缓冲区的指针
const Property< FileDialogFlags > Flags
对话框标志
Definition FileDialog.h:284
virtual int ShowDialog(Window *owner=nullptr) override=0
显示对话框,并指定所有者窗口
文件筛选器
Definition FileDialog.h:190
int SetFilter(std::initializer_list< FileFilterItem > filters)
清空现有筛选器并重新设置筛选器
FileFilter()=default
默认构造函数
bool AddFilter(const std::wstring &name, const std::wstring &filter, const std::wstring &defaultExt=L"")
添加筛选器
const wchar_t * GetDefaultExt(int index)
获取指定索引处筛选器的默认扩展名
void Clear()
清空所有已添加的筛选器
FileFilter(std::initializer_list< FileFilterItem > filters)
初始话并设置筛选器
wchar_t * GetFilterStr()
获取OPENFILENAMEW结构体lpstrFilter格式的字符串
对话框接口
Definition IDialog.h:11
“打开文件”对话框
Definition FileDialog.h:382
OpenFileDialog()
初始化OpenFileDialog
virtual int ShowDialog(Window *owner=nullptr) override
显示对话框,并指定所有者窗口
virtual int ShowDialog(Window &owner) override
显示对话框,并指定所有者窗口
属性
Definition Property.h:1743
只读属性
Definition Property.h:1828
“另存为”对话框
Definition FileDialog.h:406
virtual int ShowDialog(Window *owner=nullptr) override
显示对话框,并指定所有者窗口
virtual int ShowDialog(Window &owner) override
显示对话框,并指定所有者窗口
const Property< std::wstring > InitialFileName
初始文件名
Definition FileDialog.h:417
virtual void ProcessFileName(std::wstring &fileName) override
处理文件路径,获取文件路径时会先调用这个函数对返回值进行处理
SaveFileDialog()
初始化SaveFileDialog
窗口
Definition Window.h:35
文件筛选器信息
Definition FileDialog.h:169
std::wstring name
文本
Definition FileDialog.h:173
std::wstring defaultExt
默认扩展名,当SaveFileDialog用户没有填写扩展名时会使用该值作为扩展名
Definition FileDialog.h:183
std::wstring filter
筛选器字符串,有多个类型时用分号分隔
Definition FileDialog.h:178