-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathForm1.vb
327 lines (299 loc) · 14.3 KB
/
Form1.vb
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
Imports Scripting
Imports System.IO
Public Class Form1
Dim AutoRun As Boolean = False
Dim AutoMin As Boolean = False
Dim TimerFileName As String
Dim TimerFullPath As String
Dim TimerFileExt As String
'读ini API函数
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Int32, ByVal lpFileName As String) As Int32
'写ini API函数
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Int32
'读取ini文件内容
Public Function GetINI(ByVal Section As String, ByVal AppName As String, ByVal lpDefault As String, ByVal FileName As String) As String
Dim Str As String = ""
Str = LSet(Str, 256)
GetPrivateProfileString(Section, AppName, lpDefault, Str, Len(Str), FileName)
Return Microsoft.VisualBasic.Left(Str, InStr(Str, Chr(0)) - 1)
End Function
'写ini文件操作
Public Function WriteINI(ByVal Section As String, ByVal AppName As String, ByVal lpDefault As String, ByVal FileName As String) As Long
WriteINI = WritePrivateProfileString(Section, AppName, lpDefault, FileName)
End Function
Private Sub Button_SelPs_Click(sender As Object, e As EventArgs) Handles Button_SelPs.Click
OFD.ShowDialog()
TextBox_Ps.Text = OFD.FileName
End Sub
Private Sub Button_SelDir_Click(sender As Object, e As EventArgs) Handles Button_SelDir.Click
FB.ShowDialog()
TextBox_Dir.Text = FB.SelectedPath
If TextBox_Dir.Text <> "" Then
Button_Start.Enabled = True
Button_Clear.Enabled = True
ToolStripMenuItem_Start.Enabled = True
GetFNumAndFSize(TextBox_Dir.Text)
If AutoRun = True Then
Call Button_Start_Click(Nothing, Nothing)
End If
End If
End Sub
Private Sub Button_Start_Click(sender As Object, e As EventArgs) Handles Button_Start.Click
If TextBox_Ps.Text <> "" Then
If TextBox_Dir.Text <> "" Then
If My.Computer.FileSystem.FileExists(TextBox_Ps.Text) = True Then
If My.Computer.FileSystem.DirectoryExists(TextBox_Dir.Text) = True Then
FS.Path = TextBox_Dir.Text
FS.EnableRaisingEvents = True
Button_Stop.Enabled = True
ToolStripMenuItem_Stop.Enabled = True
ToolStripMenuItem_Start.Enabled = False
Button_Start.Enabled = False
Button_SelDir.Enabled = False
Button_SelPs.Enabled = False
Button_Clear.Enabled = False
ToolStripStatusLabel_State.Text = "服务已启动"
ToolStripStatusLabel_State.ForeColor = Color.Green
If AutoMin = True Then
Me.WindowState = FormWindowState.Minimized
End If
Else
MsgBox("目录不存在,请重新选择!", MsgBoxStyle.OkOnly, "注意")
End If
Else
MsgBox("Photoshop路径设置错误,请重新选择!", MsgBoxStyle.OkOnly, "注意")
End If
Else
MsgBox("请选择图片目录!!!", MsgBoxStyle.OkOnly, "注意")
End If
Else
MsgBox("请选择Photoshop路径!!!", MsgBoxStyle.OkOnly, "注意")
End If
End Sub
Private Sub Button_Stop_Click(sender As Object, e As EventArgs) Handles Button_Stop.Click
FS.EnableRaisingEvents = False
Button_Stop.Enabled = False
Button_Start.Enabled = True
Button_SelDir.Enabled = True
Button_SelPs.Enabled = True
Button_Clear.Enabled = True
ToolStripMenuItem_Start.Enabled = True
ToolStripMenuItem_Stop.Enabled = False
ToolStripStatusLabel_State.Text = "服务未启动"
ToolStripStatusLabel_State.ForeColor = Color.Red
End Sub
Private Sub NotifyIcon1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
Me.Show()
Me.WindowState = FormWindowState.Normal
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
LoadTheConfig()
If TextBox_Ps.Text <> "" Then
If TextBox_Dir.Text <> "" Then
Button_Start.Enabled = True
Button_Clear.Enabled = True
ToolStripMenuItem_Start.Enabled = True
GetFNumAndFSize(TextBox_Dir.Text)
If AutoRun = True Then
Call Button_Start_Click(Nothing, Nothing)
End If
End If
End If
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
If CheckBox_AutoMin.Checked = True Then
Me.Hide()
End If
End Sub
Private Sub Form1_SizeChanged(sender As Object, e As EventArgs) Handles Me.SizeChanged
If Me.WindowState = FormWindowState.Minimized Then
Me.Hide()
End If
End Sub
Private Sub FS_Created(sender As Object, e As IO.FileSystemEventArgs) Handles FS.Created
Dim FExt As String
FExt = GetFileExt(e.Name)
Dim PicExt As String = TextBox_Ext.Text
If InStr(PicExt, FExt, Microsoft.VisualBasic.CompareMethod.Text) > 0 Then
'Dim OldFileName As String = e.Name
'Dim NewFileName As String = RandomName() & "." & FExt
''MsgBox(OldFileName & "qqq" & NewFileName)
'Try
' Rename(e.FullPath, TextBox_Dir.Text & "\" & NewFileName)
' ListBox_Log.Items.Add("文件:" & NewFileName & " " & "时间:" & My.Computer.Clock.LocalTime)
' GetFNumAndFSize(TextBox_Dir.Text)
' NotifyIcon1.ShowBalloonTip(3000, "Open With Photoshop", "正在打开" & NewFileName, ToolTipIcon.Info)
' Dim PsPath As String = TextBox_Ps.Text
' Dim PicPath As String = TextBox_Dir.Text & "\" & NewFileName
' Shell(PsPath & " " & PicPath)
'Catch ex As Exception
' NotifyIcon1.ShowBalloonTip(3000, "Open With Photoshop", "文件正在下载/复制中……" & Chr(10) & "3秒后重试……", ToolTipIcon.Error)
' Timer1.Interval = 3000
' Timer1.Enabled = True
' TimerOldName = e.Name
'End Try
OpenWithPs(e.FullPath, e.Name, FExt)
End If
End Sub
Sub GetFNumAndFSize(FPath As String)
If FPath.Length <= 3 Then
Button_Start.Enabled = False
Button_Clear.Enabled = False
TextBox_Dir.Text = ""
MsgBox("请勿选择驱动器!", MsgBoxStyle.OkOnly, "注意")
Else
Dim Fso As New Scripting.FileSystemObject
Dim FNum As Integer = 0
Dim FSize As Double = 0
FNum = Fso.GetFolder(FPath).Files.Count
FSize = (Fso.GetFolder(FPath).Size) / 1024
If FSize >= 1000 Then
FSize = FSize / 1024
If FSize >= 1000 Then
FSize = FSize / 1024
ToolStripStatusLabel_FileSize.Text = "目录共 " & FNum & " 个文件,总大小 " & Format(FSize, "0.00") & "GB"
Else
ToolStripStatusLabel_FileSize.Text = "目录共 " & FNum & " 个文件,总大小 " & Format(FSize, "0.00") & "MB"
End If
Else
ToolStripStatusLabel_FileSize.Text = "目录共 " & FNum & " 个文件,总大小 " & Format(FSize, "0.00") & "KB"
End If
End If
End Sub
Function GetFileExt(FileName As String) As String
Dim L As Integer = FileName.Length
Dim b As Integer = 0
Dim a As String = ""
For i = L To 1 Step -1
a = Mid(FileName, i, 1)
If a = "." Then
b = i
Exit For
End If
Next
If b = 0 Then
GetFileExt = "NoExtension"
Else
GetFileExt = Mid(FileName, b + 1, L - b)
End If
End Function
Private Sub Button_Clear_Click(sender As Object, e As EventArgs) Handles Button_Clear.Click
If MsgBox("是否删除所选目录下所有文件(不包括文件夹)?" & Chr(10) & "删除的文件将被移入回收站中!", MsgBoxStyle.OkCancel, "删除") = MsgBoxResult.Ok Then
Dim curDir As New DirectoryInfo(TextBox_Dir.Text)
Dim fileInfo As FileInfo()
fileInfo = curDir.GetFiles()
For Each FilePath In fileInfo
Dim fullpath As String = TextBox_Dir.Text & "\" & (FilePath).ToString
If IO.File.Exists(fullpath) = True Then
My.Computer.FileSystem.DeleteFile(fullpath, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin, FileIO.UICancelOption.DoNothing)
End If
Next
ListBox_Log.Items.Clear()
End If
GetFNumAndFSize(TextBox_Dir.Text)
End Sub
Sub LoadTheConfig()
If Dir(Application.StartupPath & "\Config.ini") = "" Then
MsgBox("配置文件Config.ini丢失,将自动创建!")
Dim b() As Byte = My.Resources.Config '将资源文件转换为Byte()
Dim s As IO.Stream = System.IO.File.Create("Config.ini") '设定文件创建位置
s.Write(b, 0, b.Length) '文件写入
s.Close() '关闭文件
Else
Dim ConfigPath As String = Application.StartupPath & "\Config.ini"
TextBox_Ps.Text = GetINI("Config", "PsPath", "", ConfigPath)
TextBox_Dir.Text = GetINI("Config", "TempDir", "", ConfigPath)
TextBox_Ext.Text = GetINI("Config", "Extension", "", ConfigPath)
If GetINI("Config", "AutoRun", "", ConfigPath) = 1 Then
CheckBox_AutoRun.Checked = True
ElseIf GetINI("Config", "AutoRun", "", ConfigPath) = 0 Then
CheckBox_AutoRun.Checked = False
Else
CheckBox_AutoRun.Checked = False
End If
If GetINI("Config", "AutoMin", "", ConfigPath) = 1 Then
CheckBox_AutoMin.Checked = True
ElseIf GetINI("Config", "AutoMin", "", ConfigPath) = 0 Then
CheckBox_AutoMin.Checked = False
Else
CheckBox_AutoMin.Checked = False
End If
End If
End Sub
Private Sub Button_Save_Click(sender As Object, e As EventArgs) Handles Button_Save.Click
Try
Dim ConfigPath As String
ConfigPath = Application.StartupPath + "\Config.ini"
WriteINI("Config", "PsPath", TextBox_Ps.Text, ConfigPath)
WriteINI("Config", "TempDir", TextBox_Dir.Text, ConfigPath)
WriteINI("Config", "Extension", TextBox_Ext.Text, ConfigPath)
If CheckBox_AutoRun.Checked = True Then
WriteINI("Config", "AutoRun", 1, ConfigPath)
Else
WriteINI("Config", "AutoRun", 0, ConfigPath)
End If
If CheckBox_AutoMin.Checked = True Then
WriteINI("Config", "AutoMin", 1, ConfigPath)
Else
WriteINI("Config", "AutoMin", 0, ConfigPath)
End If
MsgBox("配置保存完毕!", MsgBoxStyle.OkOnly, "成功")
Catch ex As Exception
MsgBox("发生未知错误,请手动编辑!", MsgBoxStyle.OkOnly, "失败")
End Try
End Sub
Private Sub ToolStripMenuItem_Start_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem_Start.Click
Call Button_Start_Click(Nothing, Nothing)
End Sub
Private Sub ToolStripMenuItem_Stop_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem_Stop.Click
Call Button_Stop_Click(Nothing, Nothing)
End Sub
Private Sub ToolStripMenuItem_Exit_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem_Exit.Click
Call Button_Stop_Click(Nothing, Nothing)
Me.Close()
End Sub
Private Sub CheckBox_AutoRun_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox_AutoRun.CheckedChanged
If CheckBox_AutoRun.Checked = True Then
AutoRun = True
Else
AutoRun = False
End If
End Sub
Private Sub CheckBox_AutoMin_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox_AutoMin.CheckedChanged
If CheckBox_AutoMin.Checked = True Then
AutoMin = True
Else
AutoMin = False
End If
End Sub
Function RandomName() As String
Dim rand = New System.Random().Next(100, 999).ToString()
Dim time = Date.Now.ToString("yyyymmddhhmmss")
RandomName = time & rand
End Function
Sub OpenWithPs(FullPath As String, FileName As String, FileExt As String)
Dim OldFileName As String = FileName
Dim NewFileName As String = RandomName() & "." & FileExt
'MsgBox(OldFileName & "qqq" & NewFileName)
Try
Rename(FullPath, TextBox_Dir.Text & "\" & NewFileName)
ListBox_Log.Items.Add("文件:" & NewFileName & " " & "时间:" & My.Computer.Clock.LocalTime)
GetFNumAndFSize(TextBox_Dir.Text)
NotifyIcon1.ShowBalloonTip(3000, "Open With Photoshop", "正在打开" & NewFileName, ToolTipIcon.Info)
Dim PsPath As String = TextBox_Ps.Text
Dim PicPath As String = TextBox_Dir.Text & "\" & NewFileName
Shell(PsPath & " " & PicPath)
Catch ex As Exception
NotifyIcon1.ShowBalloonTip(1500, "Open With Photoshop", "文件正在下载/复制中……" & Chr(10) & "2秒后重试……", ToolTipIcon.Error)
TimerFileName = FileName
TimerFullPath = FullPath
TimerFileExt = FileExt
Timer1.Interval = 2000
Timer1.Enabled = True
End Try
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Timer1.Enabled = False
OpenWithPs(TimerFullPath, TimerFileName, TimerFileExt)
End Sub
End Class