📄 replacefrm-f3.frm
字号:
VERSION 5.00
Begin VB.Form RplFrm
Caption = "字符查找与替换"
ClientHeight = 2565
ClientLeft = 60
ClientTop = 345
ClientWidth = 5175
Icon = "ReplaceFrm-f3.frx":0000
LinkTopic = "Form3"
ScaleHeight = 2565
ScaleWidth = 5175
StartUpPosition = 2 '屏幕中心
Begin VB.CommandButton Command5
Caption = "全部替换"
Height = 375
Left = 3840
TabIndex = 9
Top = 1560
Width = 1095
End
Begin VB.CommandButton Command4
Caption = "替换"
Height = 375
Left = 3840
TabIndex = 8
Top = 1080
Width = 1095
End
Begin VB.CommandButton Command3
Caption = "查找下一个"
Height = 375
Left = 3840
TabIndex = 7
Top = 600
Width = 1095
End
Begin VB.TextBox Text2
Height = 270
Left = 120
TabIndex = 6
Top = 1080
Width = 3495
End
Begin VB.TextBox Text1
Height = 285
Left = 120
TabIndex = 3
Top = 360
Width = 3615
End
Begin VB.CheckBox Check1
Caption = "区分大小写"
Height = 195
Left = 240
TabIndex = 2
Top = 1680
Width = 1335
End
Begin VB.CommandButton Command1
Caption = "查 找"
Height = 375
Left = 3840
TabIndex = 1
Top = 120
Width = 1095
End
Begin VB.CommandButton Command2
Caption = "取 消"
Height = 375
Left = 3840
TabIndex = 0
Top = 2040
Width = 1095
End
Begin VB.Label Label2
Caption = "替换为"
Height = 255
Left = 120
TabIndex = 5
Top = 720
Width = 615
End
Begin VB.Label Label1
Caption = "查找:"
Height = 255
Left = 120
TabIndex = 4
Top = 120
Width = 1695
End
End
Attribute VB_Name = "RplFrm"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim FindStr As String
Dim StartPos As Integer
Sub FindString(ByVal SourceStr As String, ByVal SearchStr As String, ByVal mode As Integer, DesStr As String, FindPos As Integer)
'==本过程用于从SourceStr中查找子串SearchStr
'==返回查找到的子串的第一个字符在源字符串中的位置FindPos,
'==并返回用于下一次查找的源字符串DesStr
If mode = 1 Then
FindPos = InStr(1, SourceStr, SearchStr)
Else
FindPos = InStr(1, SourceStr, SearchStr, vbTextCompare)
End If
If FindPos = 0 Then
DesStr = ""
Exit Sub
End If
DesStr = Mid(SourceStr, FindPos + 1)
End Sub
Private Sub Check1_Click()
'单击“区分大小写”复选框,设置查找方式
If Check1.Value = 1 Then
MatchCase = True
Else
MatchCase = False
End If
End Sub
Private Sub Command1_Click()
'单击“查找”按钮的处理过程:
'1--根据是否“区分大小写”进行不同方式地查找
'2--如果找到,显示被找到的字符,否则,提示未找到信息
If MatchCase = True Then '如果以“区分大小写”方式查找
Call FindString(FrmMain.RichTextBox1.Text, Text1.Text, 1, FindStr, Pos)
Else
Call FindString(FrmMain.RichTextBox1.Text, Text1.Text, 0, FindStr, Pos)
End If
If Pos <> 0 Then '如果找到字符,则显示之
StartPos = Pos - 1 '记录找到子串前面的字符个数
FrmMain.RichTextBox1.SelStart = StartPos
FrmMain.RichTextBox1.SelLength = Len(Text1.Text)
FrmMain.SetFocus
'使“查找”按钮无效,“查找下一个”按钮有效,
Command1.Enabled = False
Command3.Enabled = True
'如果替换字符非空,则使“替换”和“全部替换”按钮有效
If Text2.Text <> "" Then
Command4.Enabled = True
Command5.Enabled = True
End If
Else
MsgBox "没有找到字符 " & Chr$(34) & Text1.Text & Chr$(34)
End If
End Sub
Private Sub Command2_Click()
'单击“取消”按钮,退出“查找”窗体
Unload Me
End Sub
Private Sub Command3_Click()
'单击“查找下一个”按钮时
Dim TempStr As String '记录下一次查找字符串的临时变量
If MatchCase = True Then '如果以“区分大小写”方式查找
Call FindString(FindStr, Text1.Text, 1, TempStr, Pos)
Else
Call FindString(FindStr, Text1.Text, 0, TempStr, Pos)
End If
FindStr = TempStr
If Pos <> 0 Then '如果找到字符,则显示之
StartPos = Pos + StartPos
FrmMain.RichTextBox1.SelStart = StartPos
FrmMain.RichTextBox1.SelLength = Len(Text1.Text)
FrmMain.SetFocus
If Text2.Text <> "" Then
Command4.Enabled = True
Command5.Enabled = True
End If
Else
MsgBox "没有找到字符 " & Chr$(34) & Text1.Text & Chr$(34)
Command3.Enabled = False
Command4.Enabled = False
Command5.Enabled = False
Command1.Enabled = True
End If
End Sub
Private Sub Command4_Click()
'单击“替换”按钮,替换找到的字符串,并调整StartPos的值。
FrmMain.RichTextBox1.SelText = Text2.Text
StartPos = StartPos + Len(Text2.Text) - Len(Text1.Text)
End Sub
Private Sub Command5_Click()
'单击“全部替换”按钮时,
Dim count As Integer
Pos = 0
Do
If MatchCase = True Then '如果以“区分大小写”方式查找
Pos = InStr(Pos + 1, FrmMain.RichTextBox1.Text, Text1.Text)
Else
Pos = InStr(Pos + 1, FrmMain.RichTextBox1.Text, Text1.Text, vbTextCompare)
End If
If Pos <> 0 Then '如果找到字符,则显示之
FrmMain.RichTextBox1.SelStart = Pos - 1
FrmMain.RichTextBox1.SelLength = Len(Text1.Text)
FrmMain.RichTextBox1.SelText = Text2.Text
count = count + 1
Pos = Pos + Len(Text2.Text)
End If
Loop Until Pos = 0
If count = 0 Then
MsgBox "没有找到字符 " & Chr$(34) & Text1.Text & Chr$(34)
Else
MsgBox "共进行了" & count & "次替换。"
End If
Unload Me
End Sub
Private Sub Form_Load()
'加载“查找”窗体的初始化设置
Pos = 0
StartPos = 0
If MatchCase = True Then Check1.Value = 1
FindStr = FrmMain.RichTextBox1.Text
Command3.Enabled = False
Command4.Enabled = False
Dim retValue As Long
Const HWND_TOPMOST = -1
Const SWP_SHOWWINDOW = &H40
retValue = SetWindowPos(Me.hwnd, HWND_TOPMOST, _
Me.CurrentX, Me.CurrentY, 360, 200, SWP_SHOWWINDOW)
End Sub
Private Sub text1_Change()
'当文本框内容发生变化,获取要查找的字符串
SearchStr = Text1.Text
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -