-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm6.vb
More file actions
926 lines (827 loc) · 41 KB
/
Form6.vb
File metadata and controls
926 lines (827 loc) · 41 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
Imports System.IO
Public Class Form6
Private currentColumn As Integer = -1 '存储当前排序的列和顺序
Private currentOrder As SortOrder = SortOrder.Ascending '存储当前排序的列和顺序
Private originalNames As New Dictionary(Of Integer, String)()
' 在类级别添加一个列表来存储重命名失败的文件信息
Private renameFailedFiles As New List(Of (index As Integer, fileName As String, newName As String))
Dim Publicpath As String
' 在Form6类中添加以下方法和调用,实现ListViewPre的双缓冲
' 1. 添加扩展ListView的双缓冲支持
Private Sub EnableDoubleBuffering(listView As ListView)
Dim prop As Reflection.PropertyInfo = GetType(Control).GetProperty("DoubleBuffered", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
If prop IsNot Nothing Then
prop.SetValue(listView, True, Nothing)
End If
End Sub
Private Sub BindContextMenuToAllTextBoxes(parent As Control, menu As ContextMenuStrip)
For Each ctrl As Control In parent.Controls
If TypeOf ctrl Is TextBox Then
ctrl.ContextMenuStrip = menu
End If
' 如果控件里还有子控件,递归处理
If ctrl.HasChildren Then
BindContextMenuToAllTextBoxes(ctrl, menu)
End If
Next
End Sub
' 获取触发菜单的 TextBox
Private Function GetTargetTextBox() As TextBox
Return TryCast(ContextMenuStrip6.SourceControl, TextBox)
End Function
' 撤销
Private Sub 撤销UToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 撤销ToolStripMenuItem.Click
Dim tb = GetTargetTextBox()
If tb IsNot Nothing Then tb.Undo()
End Sub
' 剪切
Private Sub 剪切PToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 剪切ToolStripMenuItem.Click
Dim tb = GetTargetTextBox()
If tb IsNot Nothing Then tb.Cut()
End Sub
' 复制
Private Sub 复制CToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 复制ToolStripMenuItem.Click
Dim tb = GetTargetTextBox()
If tb IsNot Nothing Then tb.Copy()
End Sub
' 粘贴
Private Sub 粘贴TToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 粘贴ToolStripMenuItem.Click
Dim tb = GetTargetTextBox()
If tb IsNot Nothing Then tb.Paste()
End Sub
' 删除
Private Sub 删除DToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 删除ToolStripMenuItem.Click
Dim tb = GetTargetTextBox()
If tb IsNot Nothing AndAlso tb.SelectionLength > 0 Then
tb.SelectedText = ""
End If
End Sub
' 全选
Private Sub 全选AToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 全选ToolStripMenuItem.Click
Dim tb = GetTargetTextBox()
If tb IsNot Nothing Then tb.SelectAll()
End Sub
' 2. 在Form6_Load中调用
Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.SelectedIndex = 0
ListViewPre.AllowDrop = True
Publicpath = ""
' 开启ListViewPre双缓冲
EnableDoubleBuffering(ListViewPre)
' 添加工具提示,展示所有可用格式
Dim toolTip As New ToolTip()
toolTip.ToolTipIcon = ToolTipIcon.Info
toolTip.ToolTipTitle = "格式说明"
toolTip.SetToolTip(ComboBox1,
"{name} - 原文件名(xxxx)" & vbCrLf &
"{index} - 序号(!)" & vbCrLf &
"{0index} - 补齐0的序号(0!)" & vbCrLf &
"{year} - 年(yyyy)" & vbCrLf &
"{month} - 月(M)" & vbCrLf &
"{0month} - 补齐0的月(0M)" & vbCrLf &
"{date} - 日期(yyyyMd)" & vbCrLf &
"{0date} - 补齐0的日期(yyyy0M0d)" & vbCrLf &
"{season} - 季(春/夏/秋/冬)" & vbCrLf &
"{folder} - 当前文件夹名称")
ContextMenuStrip1.Renderer = New ModernMenuRenderer()
ContextMenuStrip3.Renderer = New ModernMenuRenderer()
ContextMenuStrip6.Renderer = New ModernMenuRenderer()
BindContextMenuToAllTextBoxes(Me, ContextMenuStrip6)
End Sub
Private Sub bksbutton_Click(sender As Object, e As EventArgs) Handles bksbutton.Click
If ListViewPre.SelectedItems.Count > 0 Then '确保 ListView2 中有选中的项
Dim result As DialogResult = MessageBox.Show("确定要移除选定项吗?", "确认移除", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If result = DialogResult.Yes Then
'从 ListView2 中删除选中的项
Dim index As Integer = ListViewPre.SelectedItems(0).Index
For Each selectedItem As ListViewItem In ListViewPre.SelectedItems
ListViewPre.Items.Remove(selectedItem)
Next
If ListViewPre.Items.Count > 0 Then
If index < ListViewPre.Items.Count Then
ListViewPre.Items(index).Selected = True
ListViewPre.Items(index).Focused = True
Else
ListViewPre.Items(ListViewPre.Items.Count - 1).Selected = True
ListViewPre.Items(ListViewPre.Items.Count - 1).Focused = True
End If
End If
' 重新排序动态序号
UpdateDynamicIndex()
End If
Else
MessageBox.Show("选择一个项。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Private Sub addButton_Click(sender As Object, e As EventArgs) Handles loadButton.Click
If (ModifierKeys And Keys.Shift) = Keys.Shift Then
' Shift+点击,浏览文件夹
Using fbd As New FolderBrowserDialog()
If fbd.ShowDialog() = DialogResult.OK Then
Publicpath = ""
拖入重命名(fbd.SelectedPath)
' 显示路径到TextBox2
TextBox2.Text = fbd.SelectedPath
TextBox2.SelectionStart = TextBox2.Text.Length
TextBox2.ScrollToCaret()
End If
End Using
Exit Sub
End If
If (ModifierKeys And Keys.Control) = Keys.Control Then
' Ctrl+点击,从ListViewLT拉取数据
ListViewPre.Items.Clear()
originalNames.Clear()
For i As Integer = 0 To Form1.ListViewLT.Items.Count - 1
Dim item As ListViewItem = Form1.ListViewLT.Items(i)
Dim newItem As New ListViewItem((i + 1).ToString())
newItem.SubItems.Add(item.SubItems(2).Text) ' 只添加文件名
newItem.SubItems.Add(item.SubItems(2).Text) ' 新增:原始文件名
newItem.Tag = item.Tag
ListViewPre.Items.Add(newItem)
originalNames(i) = item.SubItems(2).Text
Next
Publicpath = Form1.openText.Text
TextBox2.Text = "来自 PicoFilter 加载页"
TextBox2.SelectionStart = TextBox2.Text.Length
TextBox2.ScrollToCaret()
Console.WriteLine("ListViewPre 中的项目数量:" & ListViewPre.Items.Count)
Exit Sub
End If
Publicpath = ""
转到重命名()
' 拉取时显示PicoFilter
TextBox2.Text = "来自 PicoFilter 筛选页"
TextBox2.SelectionStart = TextBox2.Text.Length
TextBox2.ScrollToCaret()
End Sub
Public Sub 转到重命名()
ListViewPre.Items.Clear()
originalNames.Clear()
For i As Integer = 0 To Form1.ListViewRT.Items.Count - 1
Dim item As ListViewItem = Form1.ListViewRT.Items(i)
Dim newItem As New ListViewItem((i + 1).ToString())
newItem.SubItems.Add(item.SubItems(2).Text) ' 第二列:当前文件名
newItem.SubItems.Add(item.SubItems(2).Text) ' 第三列:原始文件名
newItem.Tag = item.Tag
ListViewPre.Items.Add(newItem)
originalNames(i) = item.SubItems(2).Text
Next
Publicpath = Form1.openText.Text
TextBox2.Text = "PicoFilter"
TextBox2.SelectionStart = TextBox2.Text.Length
TextBox2.ScrollToCaret()
Console.WriteLine("ListViewPre 中的项目数量:" & ListViewPre.Items.Count)
End Sub
' ApplyButton_Click:批量应用格式
Private Sub ApplyButton_Click(sender As Object, e As EventArgs) Handles ApplyButton.Click
' 如果列表内存在项目,点击后全部选中
If ListViewPre.Items.Count > 0 Then
For Each item As ListViewItem In ListViewPre.Items
item.Selected = True
Next
End If
' 以下为原有批量应用格式的代码(如需恢复可取消注释)
End Sub
' Button7_Click:对选中项应用格式
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
Dim formatString As String = ComboBox1.Text
Dim folderName As String = If(Not String.IsNullOrEmpty(Publicpath),
Path.GetFileName(Publicpath.TrimEnd(Path.DirectorySeparatorChar)),
"")
If String.IsNullOrWhiteSpace(formatString) Then Return
If ListViewPre.SelectedItems.Count = 0 Then
MessageBox.Show("请选择至少一个项目进行修改。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
Return
End If
Dim startIndex As Integer = If(Integer.TryParse(TextBoxStart.Text, Nothing), CInt(TextBoxStart.Text), 1)
Dim stepSize As Integer = CInt(NumericUpDown1.Value) ' 获取步长值
Dim currentMonth As String = DateTime.Now.Month.ToString
Dim paddedMonth As String = DateTime.Now.Month.ToString.PadLeft(2, "0"c)
Dim currentDate As String = DateTime.Now.ToString("yyyyMMdd")
Dim paddedDate As String = DateTime.Now.ToString("yyyyMMdd").PadLeft(8, "0"c)
Dim currentYear As String = DateTime.Now.Year.ToString
Dim currentSeason As String = ""
Select Case currentMonth
Case "3", "4", "5" : currentSeason = "春"
Case "6", "7", "8" : currentSeason = "夏"
Case "9", "10", "11" : currentSeason = "秋"
Case "12", "1", "2" : currentSeason = "冬"
End Select
' 修改maxIndexLength的计算,考虑步长的影响
Dim maxIndex As Integer = startIndex + (ListViewPre.SelectedItems.Count - 1) * stepSize
Dim maxIndexLength As Integer = maxIndex.ToString().Length
For Each item As ListViewItem In ListViewPre.SelectedItems
Dim i As Integer = item.Index
Dim originalName As String
If originalNames.ContainsKey(i) Then
originalName = originalNames(i)
Else
originalName = item.SubItems(1).Text
originalNames(i) = originalName
End If
Dim originalName0 As String = IO.Path.GetFileNameWithoutExtension(originalName)
Dim fileExtension As String = IO.Path.GetExtension(originalName)
' 修改indexValue的计算,使用步长
Dim indexValue As Integer = startIndex + (i * stepSize)
Dim indexStr As String = indexValue.ToString()
Dim paddedIndex As String = indexStr.PadLeft(maxIndexLength, "0"c)
Dim newName As String = formatString.Replace("{prefix}", "") _
.Replace("{0name}", originalName) _
.Replace("{name}", originalName0) _
.Replace("{suffix}", "") _
.Replace("{index}", indexStr) _
.Replace("{0index}", paddedIndex) _
.Replace("{season}", currentSeason) _
.Replace("{year}", currentYear) _
.Replace("{date}", currentDate) _
.Replace("{0date}", paddedDate) _
.Replace("{month}", currentMonth) _
.Replace("{0month}", paddedMonth) _
.Replace("{folder}", folderName)
newName &= fileExtension
item.SubItems(1).Text = newName
' 检查是否有改动,添加*到序号
If newName <> originalName Then
item.BackColor = Color.Lavender
End If
Next
End Sub
' Button4_Click:还原并去掉所有*
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
' 仅还原选中项
For Each item As ListViewItem In ListViewPre.SelectedItems
' 使用第三列保存的原始文件名进行还原(避免依赖索引)
Dim originalName As String = ""
If item.SubItems.Count >= 3 Then
originalName = item.SubItems(2).Text
End If
If Not String.IsNullOrEmpty(originalName) Then
item.SubItems(1).Text = originalName
End If
' 去掉变更标记背景色
item.BackColor = Color.White
Next
' 不再 Clear originalNames(避免破坏其它逻辑)
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
ComboBox1.Text = “{0index}”
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
ComboBox1.Text = "{name}_{0date}"
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs)
If ListViewPre.Items.Count > 0 Then
Dim result As DialogResult = MessageBox.Show("确认要关闭吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If result = DialogResult.Yes Then
Me.Close()
End If
ElseIf ListViewPre.Items.Count = 0 Then
Me.Close()
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If ListViewPre.Items.Count > 0 Then
Using folderDialog As New FolderBrowserDialog()
folderDialog.Description = "选择一个位置,新建文件夹以保存重命名副本。" ' 设置对话框标题
If folderDialog.ShowDialog() = DialogResult.OK Then
Dim targetPath As String = folderDialog.SelectedPath
Dim sourcePath As String = Publicpath
' 设置进度条
With MetroProgressBar1
.Minimum = 0
.Maximum = ListViewPre.Items.Count
.Value = 0
.Visible = True
End With
Dim failedFiles As New List(Of String) ' 存储失败文件的信息
Dim processedCount As Integer = 0
For Each item As ListViewItem In ListViewPre.Items
Try
' 使用第三列原始文件名来确定原路径(避免索引映射问题)
Dim origName As String = If(item.SubItems.Count >= 3, item.SubItems(2).Text, item.SubItems(1).Text)
Dim originalFilePath As String = IO.Path.Combine(sourcePath, origName)
Dim newFilePath As String = IO.Path.Combine(targetPath, item.SubItems(1).Text)
If IO.File.Exists(originalFilePath) Then
IO.File.Copy(originalFilePath, newFilePath, True) ' 复制文件
End If
Catch ex As Exception
' 记录失败文件的信息,尽量从第三列读取原名
Dim failedName As String = If(item.SubItems.Count >= 3, item.SubItems(2).Text, item.SubItems(1).Text)
failedFiles.Add(failedName)
Finally
processedCount += 1
MetroProgressBar1.Value = processedCount
Application.DoEvents() ' 允许UI更新
End Try
Next
MetroProgressBar1.Visible = False
If failedFiles.Count > 0 Then
' 构建错误信息字符串
Dim failedFilesList As String = ""
For Each failedFile In failedFiles
failedFilesList &= failedFile & vbCrLf
Next
MessageBox.Show($"部分重命名完成,其中{failedFiles.Count}个文件重命名失败。{vbCrLf}{failedFilesList}",
"提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
' 修改这里:添加询问是否打开目标文件夹
If MessageBox.Show("重命名已完成。点击按钮打开", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = DialogResult.Yes Then
Try
Process.Start("explorer.exe", targetPath)
Catch ex As Exception
MessageBox.Show("无法打开目标文件夹。" & vbCrLf & ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
Else
' 修改这里:添加询问是否打开目标文件夹
If MessageBox.Show("重命名已完成。点击按钮打开", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = DialogResult.Yes Then
Try
Process.Start("explorer.exe", targetPath)
Catch ex As Exception
MessageBox.Show("无法打开目标文件夹。" & vbCrLf & ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End If
End If
End Using
End If
End Sub
Private Sub ListViewPre_ColumnClick(sender As Object, e As ColumnClickEventArgs) Handles ListViewPre.ColumnClick
列表排序法(ListViewPre, e.Column)
End Sub
' 排序方法
Private Sub 列表排序法(listView As ListView, column As Integer)
' 检查是否点击同一列
If column = currentColumn Then
If currentOrder = SortOrder.Ascending Then
currentOrder = SortOrder.Descending
Else
currentOrder = SortOrder.Ascending
End If
Else
currentColumn = column
currentOrder = SortOrder.Ascending
End If
' 更新排序指示箭头
更新列标题(listView)
' 使用自定义比较器进行排序
listView.ListViewItemSorter = New 列表比较器(currentColumn, currentOrder)
listView.Sort()
End Sub
' 更新列标题显示排序箭头
Private Sub 更新列标题(listView As ListView)
For i As Integer = 0 To listView.Columns.Count - 1
Dim columnHeader As ColumnHeader = listView.Columns(i)
' 清除所有列标题的箭头
columnHeader.Text = columnHeader.Text.Replace("▲", "").Replace("▼", "")
' 仅为列 1, 2, 添加箭头
If i = 0 Or i = 1 Or i = 2 Then
If i = currentColumn Then
If currentOrder = SortOrder.Ascending Then
columnHeader.Text &= "▲"
Else
columnHeader.Text &= "▼"
End If
End If
End If
Next
End Sub
Public Class 列表比较器
Implements IComparer
Private col As Integer
Private order As SortOrder
Public Sub New(column As Integer, order As SortOrder)
Me.col = column
Me.order = order
End Sub
Public Function Compare(x As Object, y As Object) As Integer Implements IComparer.Compare
Dim returnVal As Integer = 0
If TypeOf x Is ListViewItem AndAlso TypeOf y Is ListViewItem Then
Dim item1 As ListViewItem = CType(x, ListViewItem)
Dim item2 As ListViewItem = CType(y, ListViewItem)
Select Case col
Case 0 ' 序号列(按整数排序)
Dim num1 As Integer = Integer.Parse(item1.SubItems(col).Text)
Dim num2 As Integer = Integer.Parse(item2.SubItems(col).Text)
returnVal = num1.CompareTo(num2)
Case 1 ' 文件名列(自然排序)
returnVal = 自然排序(item1.SubItems(col).Text, item2.SubItems(col).Text)
Case 2 ' 文件名2(自然排序)
returnVal = 自然排序(item1.SubItems(col).Text, item2.SubItems(col).Text)
End Select
End If
If order = SortOrder.Descending Then ' 根据排序顺序调整结果
returnVal *= -1
End If
Return returnVal
End Function
' ===== 新增:自然排序方法 =====
Private Function 自然排序(strA As String, strB As String) As Integer
Dim regex As New System.Text.RegularExpressions.Regex("(\d+)|(\D+)")
Dim matchesA = regex.Matches(strA)
Dim matchesB = regex.Matches(strB)
Dim i As Integer = 0
While i < matchesA.Count AndAlso i < matchesB.Count
Dim partA As String = matchesA(i).Value
Dim partB As String = matchesB(i).Value
Dim numA, numB As Integer
If Integer.TryParse(partA, numA) AndAlso Integer.TryParse(partB, numB) Then
' 数字部分 → 按数值比较
If numA <> numB Then
Return numA.CompareTo(numB)
End If
Else
' 非数字部分 → 按字符串比较
Dim cmp As Integer = String.Compare(partA, partB, StringComparison.CurrentCultureIgnoreCase)
If cmp <> 0 Then
Return cmp
End If
End If
i += 1
End While
' 如果前面都一样 → 长度短的在前
Return matchesA.Count.CompareTo(matchesB.Count)
End Function
End Class
Private Sub absbButton_CheckStateChanged(sender As Object, e As EventArgs) Handles absbButton.CheckStateChanged
If absbButton.Checked = True Then
Me.Location = New Point(Form1.Location.X + Form1.Width, Form1.Location.Y)
Else
'Me.CenterToScreen()
End If
End Sub
Private Sub CheckBox6_CheckStateChanged(sender As Object, e As EventArgs) Handles topButton.CheckStateChanged
If topButton.Checked = True Then
TopMost = True
topButton.ImageIndex = 1
Else
TopMost = False
topButton.ImageIndex = 0
End If
End Sub
Private Sub moreButton_Click(sender As Object, e As EventArgs) Handles moreButton.Click
If ListViewPre.SelectedItems.Count = 0 Then Exit Sub
' 取消排序器,保证移动的是当前显示顺序
ListViewPre.ListViewItemSorter = Nothing
ListViewPre.BeginUpdate()
' 选中项按索引升序排列,避免顺序错乱
Dim selectedItems = ListViewPre.SelectedItems.Cast(Of ListViewItem).OrderBy(Function(x) x.Index).ToList()
For Each item In selectedItems
Dim idx = item.Index
If idx > 0 Then
ListViewPre.Items.RemoveAt(idx)
ListViewPre.Items.Insert(idx - 1, item)
End If
Next
' 恢复选中
For Each item In selectedItems
item.Selected = True
Next
ListViewPre.EndUpdate()
UpdateDynamicIndex()
End Sub
Private Sub mnsButton_Click(sender As Object, e As EventArgs) Handles mnsButton.Click
If ListViewPre.SelectedItems.Count = 0 Then Exit Sub
' 取消排序器,保证移动的是当前显示顺序
ListViewPre.ListViewItemSorter = Nothing
ListViewPre.BeginUpdate()
' 选中项按索引降序排列,避免顺序错乱
Dim selectedItems = ListViewPre.SelectedItems.Cast(Of ListViewItem).OrderByDescending(Function(x) x.Index).ToList()
For Each item In selectedItems
Dim idx = item.Index
If idx < ListViewPre.Items.Count - 1 Then
ListViewPre.Items.RemoveAt(idx)
ListViewPre.Items.Insert(idx + 1, item)
End If
Next
' 恢复选中
For Each item In selectedItems
item.Selected = True
Next
ListViewPre.EndUpdate()
UpdateDynamicIndex()
End Sub
Private Sub UpdateDynamicIndex()
For i As Integer = 0 To ListViewPre.Items.Count - 1
ListViewPre.Items(i).SubItems(0).Text = (i + 1).ToString() ' 更新动态序号
Next
End Sub
Private Sub ComboBox1_TextChanged(sender As Object, e As EventArgs) Handles ComboBox1.TextChanged
'If ComboBox1.Text = "(无)" Or ComboBox1.Text = "" Then
' ApplyButton.Visible = False
' Button7.Visible = False
'Else
' ApplyButton.Visible = True
' Button7.Visible = True
'End If
End Sub
Private Sub ListViewPre_DoubleClick(sender As Object, e As EventArgs) Handles ListViewPre.DoubleClick
If ListViewPre.SelectedItems.Count > 0 Then
Dim selectedItem As ListViewItem = ListViewPre.SelectedItems(0)
Dim fileName As String = selectedItem.SubItems(1).Text '获取选中的文件名
Dim folderPath As String = Publicpath '文件夹路径
TextBox1.Text = Publicpath
Dim filePath As String = Path.Combine(folderPath, fileName) '拼接完整的文件路径
Console.WriteLine("打开文件:" & filePath)
Try
Process.Start(filePath) ' 使用默认程序打开文件
Catch ex As Exception
MessageBox.Show("无法打开。" & vbCrLf & ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub
' 当文件拖入窗口时触发,判断是否是文件夹
Private Sub ListViewPre_DragEnter(sender As Object, e As DragEventArgs) Handles ListViewPre.DragEnter
' 判断拖入的是否是文件夹
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
Dim droppedItems() As String = CType(e.Data.GetData(DataFormats.FileDrop), String())
If Directory.Exists(droppedItems(0)) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End If
End Sub
' 当文件夹被拖入并放开鼠标时触发
Private Sub ListViewPre_DragDrop(sender As Object, e As DragEventArgs) Handles ListViewPre.DragDrop
Dim files As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
' 确保拖入的不是单个文件,而是文件夹
If files.Length > 0 AndAlso Directory.Exists(files(0)) Then
Dim folderPath As String = files(0) ' 获取文件夹路径
Publicpath = ""
拖入重命名(folderPath)
End If
End Sub
' 读取文件夹内的所有图片文件并填充 ListViewPre
Private Sub 拖入重命名(folderPath As String)
ListViewPre.Items.Clear() ' 清空旧数据
originalNames.Clear()
' 定义允许的图片文件扩展名
Dim allowedExtensions As String() = {".jpg", ".png", ".gif", ".bmp", ".ico"}
' 获取文件夹内所有文件
Dim filePaths As String() = Directory.GetFiles(folderPath)
For Each filePath As String In filePaths
' 获取文件扩展名
Dim extension As String = Path.GetExtension(filePath).ToLower()
' 检查文件扩展名是否在允许的列表中
If allowedExtensions.Contains(extension) Then
Dim fileName As String = Path.GetFileName(filePath)
Dim newItem As New ListViewItem((ListViewPre.Items.Count + 1).ToString()) ' 第一栏显示动态序号
newItem.SubItems.Add(fileName) ' 第二栏是文件名
newItem.SubItems.Add(fileName) ' 新增:原始文件名
newItem.Tag = filePath ' 保存完整文件路径,方便后续重命名
ListViewPre.Items.Add(newItem)
originalNames(ListViewPre.Items.Count - 1) = fileName ' 存储原始文件名
End If
Next
Publicpath = folderPath
' 显示路径到TextBox2
TextBox2.Text = folderPath
TextBox2.SelectionStart = TextBox2.Text.Length
TextBox2.ScrollToCaret()
Console.WriteLine("ListViewPre 中的项目数量:" & ListViewPre.Items.Count)
End Sub
Private Sub Form6_SizeChanged(sender As Object, e As EventArgs) Handles Me.SizeChanged
Me.MinimumSize = New Size(371, 582)
If Me.WindowState = FormWindowState.Maximized Then
ListViewPre.Columns(0).Width = 60
ListViewPre.Columns(1).Width = 400
ListViewPre.Columns(2).Width = 400
ElseIf Me.WindowState = FormWindowState.Normal Then
ListViewPre.Columns(0).Width = 60
ListViewPre.Columns(1).Width = 150
ListViewPre.Columns(2).Width = 150
End If
End Sub
Private Sub ToolStripMenuItem17_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem17.Click
If ListViewPre.SelectedItems.Count = 1 Then
Dim selectedItem As ListViewItem = ListViewPre.SelectedItems(0)
Dim fileName As String = selectedItem.SubItems(1).Text '获取选中的文件名
Dim folderPath As String = Publicpath '文件夹路径
TextBox1.Text = Publicpath
Dim filePath As String = Path.Combine(folderPath, fileName) '拼接完整的文件路径
Console.WriteLine("打开文件:" & filePath)
Try
Process.Start(filePath) ' 使用默认程序打开文件
Catch ex As Exception
MessageBox.Show("无法打开。" & vbCrLf & ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub
Private Sub 移除选中项DToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 移除选中项DToolStripMenuItem.Click
If ListViewPre.SelectedItems.Count > 0 Then '确保 ListView2 中有选中的项
Dim result As DialogResult = MessageBox.Show("确定要移除选定项吗?", "确认移除", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If result = DialogResult.Yes Then
'从 ListView2 中删除选中的项
Dim index As Integer = ListViewPre.SelectedItems(0).Index
For Each selectedItem As ListViewItem In ListViewPre.SelectedItems
ListViewPre.Items.Remove(selectedItem)
Next
If ListViewPre.Items.Count > 0 Then
If index < ListViewPre.Items.Count Then
ListViewPre.Items(index).Selected = True
ListViewPre.Items(index).Focused = True
Else
ListViewPre.Items(ListViewPre.Items.Count - 1).Selected = True
ListViewPre.Items(ListViewPre.Items.Count - 1).Focused = True
End If
End If
' 重新排序动态序号
UpdateDynamicIndex()
End If
Else
MessageBox.Show("选择一个项。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Private Sub ToolStripMenuItem14_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem14.Click
' 如果列表内存在项目,点击后全部选中
If ListViewPre.Items.Count > 0 Then
For Each item As ListViewItem In ListViewPre.Items
item.Selected = True
Next
End If
End Sub
' 在 ListView0
Private Sub ListView0_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListViewPre.SelectedIndexChanged
If ListViewPre.SelectedItems.Count = 1 Then
ListViewPre.ContextMenuStrip = ContextMenuStrip3
Else
ListViewPre.ContextMenuStrip = ContextMenuStrip1
End If
End Sub
Private Sub ToolStripMenuItem2_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem2.Click
If ListViewPre.SelectedItems.Count > 0 Then '确保 ListView2 中有选中的项
Dim result As DialogResult = MessageBox.Show("确定要移除选定项吗?", "确认移除", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If result = DialogResult.Yes Then
'从 ListView2 中删除选中的项
Dim index As Integer = ListViewPre.SelectedItems(0).Index
For Each selectedItem As ListViewItem In ListViewPre.SelectedItems
ListViewPre.Items.Remove(selectedItem)
Next
If ListViewPre.Items.Count > 0 Then
If index < ListViewPre.Items.Count Then
ListViewPre.Items(index).Selected = True
ListViewPre.Items(index).Focused = True
Else
ListViewPre.Items(ListViewPre.Items.Count - 1).Selected = True
ListViewPre.Items(ListViewPre.Items.Count - 1).Focused = True
End If
End If
' 重新排序动态序号
UpdateDynamicIndex()
End If
Else
MessageBox.Show("选择一个项。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
' 自定义现代风格渲染器
Public Class ModernMenuRenderer
Inherits ToolStripProfessionalRenderer
Public Sub New()
MyBase.New(New ModernColorTable())
End Sub
' 新增:自定义左侧图标区域渐变色
Protected Overrides Sub OnRenderImageMargin(e As ToolStripRenderEventArgs)
Dim marginRect As Rectangle = e.AffectedBounds
' 你可以自定义渐变色,这里以蓝紫渐变为例
Using brush As New Drawing2D.LinearGradientBrush(
marginRect,
Color.Lavender, ' 渐变起始色
Color.Lavender, ' 渐变结束色
Drawing2D.LinearGradientMode.Horizontal)
e.Graphics.FillRectangle(brush, marginRect)
End Using
End Sub
Protected Overrides Sub OnRenderSeparator(e As ToolStripSeparatorRenderEventArgs)
Dim g = e.Graphics
Dim bounds = e.Item.ContentRectangle
Dim y = bounds.Top + bounds.Height \ 2
Using pen As New Pen(Color.Lavender, 1)
g.DrawLine(pen, bounds.Left + 25, y, bounds.Right - 4, y)
End Using
End Sub
End Class
' 自定义颜色表
Public Class ModernColorTable
Inherits ProfessionalColorTable
Public Overrides ReadOnly Property MenuItemSelected As Color
Get
Return Color.Lavender
End Get
End Property
Public Overrides ReadOnly Property MenuItemBorder As Color
Get
Return Color.Lavender
End Get
End Property
Public Overrides ReadOnly Property MenuBorder As Color
Get
Return Color.Lavender
End Get
End Property
End Class
Private Sub ToolStripMenuItem9_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem9.Click
ListViewPre.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
End Sub
Private Sub 还原列宽OToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 还原列宽OToolStripMenuItem.Click
If Me.WindowState = FormWindowState.Normal Then
ListViewPre.Columns(0).Width = 60
ListViewPre.Columns(1).Width = 240
ElseIf Me.WindowState = FormWindowState.Maximized Then
ListViewPre.Columns(0).Width = 60
ListViewPre.Columns(1).Width = 600
End If
End Sub
Private Sub loadButton_MouseDown(sender As Object, e As MouseEventArgs) Handles loadButton.MouseDown
If e.Button = MouseButtons.Right Then
' Shift+点击,浏览文件夹
Using fbd As New FolderBrowserDialog()
If fbd.ShowDialog() = DialogResult.OK Then
Publicpath = ""
拖入重命名(fbd.SelectedPath)
' 显示路径到TextBox2
TextBox2.Text = fbd.SelectedPath
TextBox2.SelectionStart = TextBox2.Text.Length
TextBox2.ScrollToCaret()
End If
End Using
ElseIf e.Button = MouseButtons.Middle Then
ListViewPre.Items.Clear()
originalNames.Clear()
For i As Integer = 0 To Form1.ListViewLT.Items.Count - 1
Dim item As ListViewItem = Form1.ListViewLT.Items(i)
Dim newItem As New ListViewItem((i + 1).ToString())
newItem.SubItems.Add(item.SubItems(2).Text) ' 只添加文件名
newItem.SubItems.Add(item.SubItems(2).Text) ' 新增:原始文件名
newItem.Tag = item.Tag
ListViewPre.Items.Add(newItem)
originalNames(i) = item.SubItems(2).Text
Next
Publicpath = Form1.openText.Text
TextBox2.Text = "来自 PicoFilter 加载页"
TextBox2.SelectionStart = TextBox2.Text.Length
TextBox2.ScrollToCaret()
Console.WriteLine("ListViewPre 中的项目数量:" & ListViewPre.Items.Count)
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ListViewPre.Items.Count = 0 Then
MessageBox.Show("没有可重命名的文件。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
If String.IsNullOrEmpty(Publicpath) OrElse Not IO.Directory.Exists(Publicpath) Then
MessageBox.Show("源文件夹无效。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
Dim confirm = MessageBox.Show("即将覆盖原文件,是否继续?", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
If confirm <> DialogResult.Yes Then Exit Sub
Dim failedFiles As New List(Of String)
Dim processedCount As Integer = 0
With MetroProgressBar1
.Minimum = 0
.Maximum = ListViewPre.Items.Count
.Value = 0
.Visible = True
End With
For Each item As ListViewItem In ListViewPre.Items
Try
' 使用第三列原始文件名来获取 oldName(避免排序后索引不一致)
Dim oldName As String = If(item.SubItems.Count >= 3, item.SubItems(2).Text, item.SubItems(1).Text)
Dim newName As String = item.SubItems(1).Text
If oldName = newName Then
' 跳过未更名项
processedCount += 1
MetroProgressBar1.Value = processedCount
Continue For
End If
Dim oldPath As String = IO.Path.Combine(Publicpath, oldName)
Dim newPath As String = IO.Path.Combine(Publicpath, newName)
If IO.File.Exists(oldPath) Then
' 若目标文件已存在,先删除
If IO.File.Exists(newPath) Then IO.File.Delete(newPath)
IO.File.Move(oldPath, newPath)
End If
Catch ex As Exception
Dim failedName As String = If(item.SubItems.Count >= 3, item.SubItems(2).Text, item.SubItems(1).Text)
failedFiles.Add(failedName)
Finally
processedCount += 1
MetroProgressBar1.Value = processedCount
Application.DoEvents()
End Try
Next
MetroProgressBar1.Visible = False
If failedFiles.Count > 0 Then
Dim failedFilesList As String = String.Join(vbCrLf, failedFiles)
MessageBox.Show($"部分文件重命名失败:{vbCrLf}{failedFilesList}", "未完成", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Else
If MessageBox.Show("文件重命名完成,点击按钮打开文件夹", "完成", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = DialogResult.Yes Then
Try
Process.Start("explorer.exe", Publicpath)
Catch ex As Exception
MessageBox.Show("无法打开文件夹。" & vbCrLf & ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End If
End Sub
End Class