-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathMiscHelper.bas
More file actions
352 lines (224 loc) · 7.33 KB
/
MiscHelper.bas
File metadata and controls
352 lines (224 loc) · 7.33 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
Attribute VB_Name = "MiscHelper"
'--------------------------------------------------------------------------------
' Component : MiscHelper
' Project : ViDock
'
' Description: Functions that had nowhere else to live :(
' TODO: Create dedicated modules for each function no matter
' how slim they might be
'
'--------------------------------------------------------------------------------
Option Explicit
Public pngIndex As Long
Public Function CreateShortcut(szShortcutPath As String, _
szTargetPath As String, _
szArguments As String)
On Error GoTo Handler
Dim objShell
Dim oShellLink
Set objShell = CreateObject("WScript.Shell")
Set oShellLink = objShell.CreateShortcut(szShortcutPath)
oShellLink.TargetPath = szTargetPath
oShellLink.Arguments = szArguments
oShellLink.save
CreateShortcut = FileExists(szShortcutPath)
Handler:
End Function
Public Function GetWindowsOSVersion() As OSVERSIONINFO
Dim osv As OSVERSIONINFO
osv.dwOSVersionInfoSize = Len(osv)
If GetVersionEx(osv) = 1 Then
GetWindowsOSVersion = osv
End If
End Function
Public Sub StayOnTop(frmForm As Form, fOnTop As Boolean)
Dim lState As Long
If fOnTop Then
lState = HWND_TOPMOST
Else
lState = HWND_NOTOPMOST
End If
Call SetWindowPos(frmForm.hWnd, lState, 0, 0, 0, 0, SWP_NOSIZE Or SWP_NOREPOSITION Or SWP_NOACTIVATE)
End Sub
Public Function StripDecimal(ByRef theNumber As Single) As Long
Dim decimalPosition As Long
decimalPosition = InStr(theNumber, ".")
If decimalPosition > 0 Then
StripDecimal = Left(theNumber, decimalPosition)
Else
StripDecimal = theNumber
End If
End Function
Public Function GetFilenameFromPath(ByVal theFilePath As String) As String
Dim theDelim As String
If InStr(theFilePath, "\") > 0 Then
theDelim = "\"
ElseIf InStr(theFilePath, "/") > 0 Then
theDelim = "/"
End If
GetFilenameFromPath = Right(theFilePath, Len(theFilePath) - InStrRev(theFilePath, theDelim))
End Function
Function IsPathAFolder(ByVal szPathSpec As String) As Boolean
On Error GoTo Handler
If Right(szPathSpec, 1) <> "\" Then
szPathSpec = szPathSpec & "\"
End If
IsPathAFolder = Dir(szPathSpec) <> vbNullString
Handler:
End Function
Function AddToCollectionAtPosition(ByRef theCollection As Collection, _
theItem, _
desiredPosition As Long, _
Optional theKey)
If desiredPosition = 1 And theCollection.Count > 0 Then
theCollection.Add theItem, theKey, 1
ElseIf desiredPosition > theCollection.Count Then
theCollection.Add theItem, theKey
Else
theCollection.Add theItem, theKey, , desiredPosition - 1
End If
End Function
Function ExistInCol(ByRef cTarget As Collection, sKey) As Boolean
On Error GoTo Handler
ExistInCol = Not (IsEmpty(cTarget(sKey)))
Exit Function
Handler:
ExistInCol = False
End Function
Public Function RTS2(ByVal Number As Long, ByVal significance As Long)
'Round number up or down to the nearest multiple of significance
Dim d As Double
Number = Number + (significance / 2)
d = Number / significance
d = Round(d, 0)
RTS2 = d
End Function
'Public Function GetPngCodecCLSID() As clsid
'
' Dim thisCLSID As New GDIPImageEncoderList
'
' GetPngCodecCLSID = thisCLSID.EncoderForMimeType("image/png").CodecCLSID
'
'End Function
Public Function isset(srcAny) As Boolean
On Error GoTo Handler
Dim thisVarType As VbVarType: thisVarType = VarType(srcAny)
If thisVarType = vbObject Then
If Not srcAny Is Nothing Then
isset = True
Exit Function
End If
ElseIf thisVarType = vbArray Or thisVarType = 8200 Then
If UBound(srcAny) > 0 Then
isset = True
Exit Function
End If
Else
isset = IsEmpty(srcAny)
Exit Function
End If
Handler:
isset = False
End Function
Public Function parseInt(srcData) As Long
If (IsNumeric(srcData)) Then
parseInt = CLng(srcData)
Exit Function
Else
parseInt = -1
End If
End Function
Public Function TrimNull(ByVal StrIn As String) As String
Dim nul As Long
' Truncate input string at first null.
' If no nulls, perform ordinary Trim.
nul = InStr(StrIn, vbNullChar)
Select Case nul
Case Is > 1
TrimNull = Left(StrIn, nul - 1)
Case 1
TrimNull = ""
Case 0
TrimNull = Trim(StrIn)
End Select
End Function
Public Function StrEnd(ByVal sData As String, _
ByVal sDelim As String, _
Optional iOffset As Integer = 1)
If InStr(sData, sDelim) = 0 Then
'Delim not present
StrEnd = sData
Exit Function
End If
Dim iLen As Integer, iDLen As Integer
iLen = Len(sData) + 1
iDLen = Len(sDelim)
If iLen = 1 Or iDLen = 0 Then
StrEnd = False
Exit Function
End If
While Mid(sData, iLen, iDLen) <> sDelim And iLen > 1
iLen = iLen - 1
Wend
If iLen = 0 Then
StrEnd = False
Exit Function
End If
StrEnd = Mid(sData, iLen + iOffset)
End Function
Public Function FileExists(sSource As String, _
Optional ByVal allowFsDirection As Boolean = True) As Boolean
If sSource = vbNullString Then
Exit Function
End If
Dim WFD As WIN32_FIND_DATA
Dim hFile As Long
hFile = FindFirstFile(sSource, WFD)
FileExists = hFile <> INVALID_HANDLE_VALUE
Call FindClose(hFile)
If FileExists = False And Is64bit And allowFsDirection = False Then
Dim win64Token As Win64FSToken: Set win64Token = New Win64FSToken
FileExists = FileExists(sSource, True)
win64Token.EnableFS
End If
End Function
Public Function ShowWindowTimeout(ByRef hWnd As Long, ByRef nCmdShow As ESW)
If Not IsWindowHung(hWnd) Then
ShowWindow hWnd, nCmdShow
End If
End Function
Public Function IsWindowHung(hWnd As Long) As Boolean
Dim lResult As Long
Dim lReturn As Long
lReturn = SendMessageTimeout(hWnd, WM_NULL, 0&, 0&, SMTO_ABORTIFHUNG Or SMTO_BLOCK, 1000, lResult)
If lReturn Then
IsWindowHung = False
Exit Function
End If
IsWindowHung = True
End Function
Public Function Exists(col, index) As Boolean
On Error GoTo ExistsTryNonObject
Dim o As Object
Set o = col(index)
Exists = True
Exit Function
ExistsTryNonObject:
Exists = ExistsNonObject(col, index)
End Function
Private Function ExistsNonObject(col, index) As Boolean
On Error GoTo ExistsNonObjectErrorHandler
Dim v As Variant
v = col(index)
ExistsNonObject = True
Exit Function
ExistsNonObjectErrorHandler:
ExistsNonObject = False
End Function
Public Sub RepaintWindow(ByRef hWnd As Long)
'verified it works
If IsWindowHung(hWnd) Then Exit Sub
If hWnd <> 0 Then
Call RedrawWindow(hWnd, ByVal 0&, ByVal 0&, RDW_ERASE Or RDW_INVALIDATE Or RDW_ALLCHILDREN Or RDW_UPDATENOW)
End If
End Sub