-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm7.vb
More file actions
408 lines (359 loc) · 15.8 KB
/
Form7.vb
File metadata and controls
408 lines (359 loc) · 15.8 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
Imports System.IO
Imports System.Text.RegularExpressions
Public Class Form7
' 静态缓存:仅加载一次
Private Shared englishDict As HashSet(Of String) = Nothing
Private Shared chineseDict As HashSet(Of String) = Nothing
'Private isSelecting As Boolean = False
'Private selectStart As Point
'Private selectionRect As Rectangle
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
Private Sub Form7_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' 默认使用基础分组
FlowLayoutPanel1.AutoScroll = True
MetroTabControl1.SelectedTab = MetroTabControl1.TabPages(0)
If absbButton.Checked = True Then
Me.Location = New Point(Form1.Location.X - Me.Width, Form1.Location.Y)
Else
Me.CenterToScreen()
End If
BindContextMenuToAllTextBoxes(Me, ContextMenuStrip6)
ContextMenuStrip6.Renderer = New ModernMenuRenderer()
End Sub
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 - Me.Width, Form1.Location.Y)
Else
'Me.CenterToScreen()
End If
End Sub
''' <summary>
''' 外部调用接口:传入文件名,进行分词并生成标签
''' </summary>
Public Sub LoadFromFileName(fileName As String)
FlowLayoutPanel1.Controls.Clear()
Dim nameOnly As String = Path.GetFileNameWithoutExtension(fileName)
Dim parts As List(Of String)
' 默认使用基础分组(推荐)
parts = BasicSplit(nameOnly)
' 若启用词典分组,请取消下方注释并注释上方 EnsureDictionariesLoaded() parts = DictSplit(nameOnly)
' 生成 CheckBox 控件
For Each word In parts
Dim cb As New CheckBox With {
.Text = word,
.Appearance = Appearance.Button,
.AutoSize = True,
.Padding = New Padding(0),
.Margin = New Padding(4),
.BackColor = Color.White,
.FlatStyle = FlatStyle.Flat,
.Cursor = Cursors.Default,
.Font = New Font("Microsoft YaHei", 15, GraphicsUnit.Pixel),
.ForeColor = Color.FromArgb(43, 43, 43)
}
cb.FlatAppearance.BorderSize = 1
cb.FlatAppearance.BorderColor = Color.White 'FromArgb(43, 43, 43)
cb.FlatAppearance.MouseDownBackColor = Color.LemonChiffon
cb.FlatAppearance.MouseOverBackColor = Color.Silver
cb.FlatAppearance.CheckedBackColor = Color.Gainsboro
FlowLayoutPanel1.Controls.Add(cb)
Next
End Sub
''' <summary>
''' 基础分割法:符号+驼峰+数字+中文字符识别
''' </summary>
Private Function BasicSplit(input As String) As List(Of String)
input = input.Replace("_", " ").Replace("-", " ")
Dim rawParts = Regex.Split(input, "\s+")
Dim result As New List(Of String)
For Each part In rawParts
If String.IsNullOrEmpty(part) Then Continue For
Dim tokens = Regex.Matches(part, "([A-Z]?[a-z]+|[A-Z]+(?![a-z])|\d+|[\u4e00-\u9fa5]+)")
For Each m As Match In tokens
result.Add(m.Value)
Next
Next
Return result
End Function
''' <summary>
''' 最大匹配法(中英文词典分组)—— 保留
''' </summary>
Private Function DictSplit(input As String) As List(Of String)
Dim result As New List(Of String)
Dim i As Integer = 0
While i < input.Length
Dim found As Boolean = False
For j = input.Length To i + 1 Step -1
Dim subStr = input.Substring(i, j - i)
If englishDict.Contains(subStr.ToLower()) OrElse chineseDict.Contains(subStr) Then
result.Add(subStr)
i = j
found = True
Exit For
End If
Next
If Not found Then
result.Add(input(i).ToString())
i += 1
End If
End While
Return result
End Function
'''' <summary>
'''' 加载词典,只加载一次(已注释,可启用)
'''' </summary>
'Private Sub EnsureDictionariesLoaded()
' If englishDict Is Nothing Then
' englishDict = New HashSet(Of String)(StringComparer.OrdinalIgnoreCase)
' If File.Exists("Dictionaries\english-words.txt") Then
' For Each line In File.ReadLines("Dictionaries\english-words.txt")
' Dim word = line.Trim()
' If word <> "" Then englishDict.Add(word)
' Next
' End If
' End If
' If chineseDict Is Nothing Then
' chineseDict = New HashSet(Of String)
' If File.Exists("Dictionaries\cedict_ts.u8") Then
' For Each line In File.ReadLines("Dictionaries\cedict_ts.u8")
' If line.StartsWith("#") Then Continue For
' Dim parts = line.Split(" "c)
' If parts.Length > 0 Then chineseDict.Add(parts(0))
' Next
' End If
' End If
'End Sub
''' <summary>
''' 复制所有选中的词
''' </summary>
Private Sub ButtonCopySelected_Click(sender As Object, e As EventArgs) Handles ButtonCopySelected.Click
If MetroTabControl1.SelectedIndex = 0 Then
Dim selectedWords As New List(Of String)
For Each ctrl As Control In FlowLayoutPanel1.Controls
If TypeOf ctrl Is CheckBox Then
Dim cb As CheckBox = DirectCast(ctrl, CheckBox)
If cb.Checked Then
selectedWords.Add(cb.Text)
End If
End If
Next
If selectedWords.Count = 0 Then
MessageBox.Show("请先选择要复制的词", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Return
End If
Dim combinedText = String.Join("", selectedWords)
Clipboard.SetText(combinedText)
MessageBox.Show("取词已复制:" & vbCrLf & combinedText, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
ElseIf MetroTabControl1.SelectedIndex = 1 Then
Clipboard.SetText(TextBox1.Text)
MessageBox.Show("文本已复制。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
'MessageBox.Show("该功能仅支持「词典拆分」选项卡,请右键复制所选文本。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Private Sub topButton_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 Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim selectedWords As New List(Of String)
For Each ctrl As Control In FlowLayoutPanel1.Controls
If TypeOf ctrl Is CheckBox Then
Dim cb As CheckBox = DirectCast(ctrl, CheckBox)
If cb.Checked Then
selectedWords.Add(cb.Text)
End If
End If
Next
If selectedWords.Count = 0 Then
MessageBox.Show("请先取词。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Return
End If
Dim combinedText = String.Join("", selectedWords)
Form1.searchText.Text = combinedText
Form1.optChange("转到「查找」选项卡以继续", 0)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim selectedWords As New List(Of String)
For Each ctrl As Control In FlowLayoutPanel1.Controls
If TypeOf ctrl Is CheckBox Then
Dim cb As CheckBox = DirectCast(ctrl, CheckBox)
If cb.Checked Then
selectedWords.Add(cb.Text)
End If
End If
Next
If selectedWords.Count = 0 Then
MessageBox.Show("请先选择。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Return
End If
Dim combinedText = String.Join("", selectedWords)
Form1.starText.Text = "{" & combinedText & "}{}{}"
Form1.optChange("转到「星标」选项卡以继续", 0)
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
If MetroTabControl1.SelectedIndex = 0 Then
Button4.Visible = True
ButtonCopySelected.Visible = True
Dim totalCount As Integer = 0
Dim checkedCount As Integer = 0
' 统计当前选中数量
For Each ctrl As Control In FlowLayoutPanel1.Controls
If TypeOf ctrl Is CheckBox Then
Dim cb = DirectCast(ctrl, CheckBox)
totalCount += 1
If cb.Checked Then checkedCount += 1
End If
Next
' 决定是“全选”还是“取消全选”
Dim selectAll As Boolean = checkedCount < totalCount
For Each ctrl As Control In FlowLayoutPanel1.Controls
If TypeOf ctrl Is CheckBox Then
Dim cb = DirectCast(ctrl, CheckBox)
cb.Checked = selectAll
End If
Next
'' 可选:更新按钮文字
'Button4.Text = If(selectAll, "取消选中", "全选选中")
ElseIf MetroTabControl1.SelectedIndex = 1 Then
TextBox1.SelectAll() '选中全部文本
End If
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
If MetroTabControl1.SelectedIndex = 0 Then
Dim sfd As New SaveFileDialog()
Dim selectedWords As New List(Of String)
For Each ctrl As Control In FlowLayoutPanel1.Controls
If TypeOf ctrl Is CheckBox Then
Dim cb As CheckBox = DirectCast(ctrl, CheckBox)
If cb.Checked Then
selectedWords.Add(cb.Text)
End If
End If
Next
If selectedWords.Count = 0 Then
MessageBox.Show("请先选择要复制的词", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Exit Sub
End If
Dim combinedText = String.Join("", selectedWords)
sfd.Title = "保存文本文件"
sfd.Filter = "文本文件 (*.txt)|*.txt|所有文件 (*.*)|*.*"
sfd.FileName = "取词文本.txt" '默认文件名
sfd.DefaultExt = "txt"
If sfd.ShowDialog() = DialogResult.OK Then
System.IO.File.WriteAllText(sfd.FileName, combinedText, System.Text.Encoding.UTF8)
If MessageBox.Show("文本保存成功,点击按钮打开", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = DialogResult.Yes Then
Process.Start(sfd.FileName)
End If
End If
ElseIf MetroTabControl1.SelectedIndex = 1 Then
Dim sfd As New SaveFileDialog()
sfd.Title = "保存文本文件"
sfd.Filter = "文本文件 (*.txt)|*.txt|所有文件 (*.*)|*.*"
sfd.FileName = "文件名文本.txt" '默认文件名
sfd.DefaultExt = "txt"
If sfd.ShowDialog() = DialogResult.OK Then
System.IO.File.WriteAllText(sfd.FileName, TextBox1.Text, System.Text.Encoding.UTF8)
If MessageBox.Show("文本保存成功,点击按钮打开", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = DialogResult.Yes Then
Process.Start(sfd.FileName)
End If
End If
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
End Class