-
Notifications
You must be signed in to change notification settings - Fork 1
/
OpenSaveDlg.bas
219 lines (198 loc) · 7.11 KB
/
OpenSaveDlg.bas
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
Attribute VB_Name = "OpenSaveDlg"
Option Explicit
'Enum for the Flags of the BrowseForFolder API function
Enum BrowseForFolderFlags
BIF_RETURNONLYFSDIRS = &H1
BIF_DONTGOBELOWDOMAIN = &H2
BIF_STATUSTEXT = &H4
BIF_BROWSEFORCOMPUTER = &H1000
BIF_BROWSEFORPRINTER = &H2000
BIF_BROWSEINCLUDEFILES = &H4000
BIF_EDITBOX = &H10
BIF_RETURNFSANCESTORS = &H8
End Enum
'BrowseInfo is a type used with the SHBrowseForFolder API call
Private Type BROWSEINFO
hwndOwner As Long
pidlRoot As Long
pszDisplayName As Long
lpszTitle As Long
ulFlags As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End Type
'Shell APIs from Shell32.dll file:
'SHBrowseForFolder - Gets the Browse For Folder Dialog
Private Declare Function SHBrowseForFolder Lib "Shell32" (lpBI As BROWSEINFO) As Long
Private Declare Function SHGetPathFromIDList Lib "Shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
'lstrcat API function appends a string to another - that means that some API functions
'need their string in the numeric way like this does, so its kind of converts strings
'to numbers
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
Public Const imageextentions As String = "*.bmp;*.gif;*.jpg;*.jpeg;*.jpe;*.jfif;*.png"
Public Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Public Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
Public Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
Flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Public SaveFileDialog As OPENFILENAME
Public OpenFileDialog As OPENFILENAME
Private rv As Long
Private sv As Long
Private Enum CdlgExt_Flags
cdlCCFullOpen = &H2
cdlCCHelpButton = &H8
cdlCCPreventFullOpen = &H4
cdlCCRGBInit = &H1
End Enum
Private mFlags As CdlgExt_Flags
Private Type CHOOSECOLOR 'Color Dialog
lStructSize As Long
hwndOwner As Long
hInstance As Long
RGBResult As Long
lpCustColors As String
Flags As Long
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Private Declare Function ChooseColorAPI Lib "comdlg32.dll" Alias "ChooseColorA" (pChoosecolor As CHOOSECOLOR) As Long
'Color
Public Function ShowColor(mhOwner As Long, Optional mRGBResult As Long) As Long
Dim CC As CHOOSECOLOR, CustomColors() As Byte, uFlag As Long, i As Long, RetValue As Long
ReDim CustomColors(0 To 16 * 4 - 1) As Byte
For i = LBound(CustomColors) To UBound(CustomColors)
CustomColors(i) = 255 ' white
Next i
uFlag = mFlags And (&H1 Or &H2 Or &H4 Or &H8)
With CC
.lStructSize = Len(CC)
.hwndOwner = mhOwner
.hInstance = App.hInstance
.lpCustColors = StrConv(CustomColors, vbUnicode)
.Flags = uFlag
.RGBResult = mRGBResult
RetValue = ChooseColorAPI(CC)
If RetValue = 0 Then
ShowColor = -1
Else
CustomColors = StrConv(.lpCustColors, vbFromUnicode)
mRGBResult = .RGBResult
ShowColor = mRGBResult
End If
End With
End Function
Public Function Open_File(hWnd As Long) As String
rv& = GetOpenFileName(OpenFileDialog)
If (rv&) Then
Open_File = Replace(Trim$(OpenFileDialog.lpstrFile), Chr(0), Empty)
Else
Open_File = ""
End If
End Function
Public Function AutoSaveLoad(hWnd As Long, ByVal Filter As String, Optional Title As String, Optional InitDir As String, Optional Load As Boolean) As String
Dim tempstr() As String, tempstr2 As String
Filter = Replace(Filter, "|", Chr(0))
If Load Then
If Len(Title) = 0 Then Title = "Load file"
InitOpen Filter, Title, InitDir
AutoSaveLoad = Open_File(hWnd)
Else
If Len(Title) = 0 Then Title = "Save file"
InitSave Filter, Title, InitDir
If InStr(Filter, Chr(0)) > 0 Then
tempstr = Split(Filter, Chr(0))
Filter = Replace(tempstr(1), "*.", Empty)
End If
AutoSaveLoad = Save_File(hWnd, Filter)
End If
End Function
Public Function Save_File(hWnd As Long, Optional defaultextention As String) As String
sv& = GetSaveFileName(SaveFileDialog)
Dim temp As String
temp = ""
If (sv&) Then
temp = Trim$(SaveFileDialog.lpstrFile)
temp = Left(temp, Len(temp) - 1)
If InStrRev(temp, ".") = 0 And Len(defaultextention) > 0 Then temp = temp & "." & defaultextention
If Dir(temp) <> Empty Then If MsgBox("File already exists. Do you wish to over write it?" & vbNewLine & temp, vbYesNo, "File exists") = vbNo Then temp = ""
Save_File = temp
End If
End Function
Public Sub InitSave(Filter As String, Title As String, Optional InitDir As String)
With SaveFileDialog
.lStructSize = Len(SaveFileDialog)
.hInstance = App.hInstance
.lpstrFilter = Filter
.lpstrFile = Space$(254)
.nMaxFile = 255
.lpstrFileTitle = Space$(254)
.nMaxFileTitle = 255
.lpstrInitialDir = IIf(InitDir <> Empty, InitDir, CurDir)
.lpstrTitle = Title
.Flags = 0
End With
End Sub
Public Sub InitOpen(Filter As String, Title As String, Optional InitDir As String)
Filter = Replace(Filter, "|", Chr(0))
With OpenFileDialog
.lStructSize = Len(OpenFileDialog)
.hInstance = App.hInstance
.lpstrFilter = Filter
.lpstrFile = Space$(254)
.nMaxFile = 255
.lpstrFileTitle = Space$(254)
.nMaxFileTitle = 255
.lpstrInitialDir = IIf(InitDir <> Empty, InitDir, CurDir)
.lpstrTitle = Title
.Flags = 0
End With
End Sub
Public Function BrowseForFolder(hWnd As Long, Optional Title As String, Optional Flags As BrowseForFolderFlags) As String
On Error Resume Next
'Variables for use:
Dim iNull As Integer
Dim IDList As Long
Dim Result As Long
Dim Path As String
Dim bi As BROWSEINFO
If Flags = 0 Then Flags = BIF_RETURNONLYFSDIRS
'Type Settings
With bi
.hwndOwner = hWnd
.lpszTitle = lstrcat(Title, "")
.ulFlags = Flags
End With
'Execute the BrowseForFolder shell API and display the dialog
IDList = SHBrowseForFolder(bi)
'Get the info out of the dialog
If IDList Then
Path = String$(300, 0)
Result = SHGetPathFromIDList(IDList, Path)
iNull = InStr(Path, vbNullChar)
If iNull Then Path = Left$(Path, iNull - 1)
End If
'If Cancel button was clicked, error occured or My Computer was selected then Path = ""
BrowseForFolder = Path
End Function