呵呵,不知道你现在没有空格了,整段文字选单字的话,给你参考一下。 Private Sub Text1_Click() Dim s$, ss$, n&, s1$, s2$ s = Form1.Text1.Text n = Form1.Text1.SelStart: If n = 0 Then n = 1 ss = Mid(s, n, 1) Form1.Text2.Text = Form1.Text2.Text & ss End Sub
代码多点,但是看得清楚; 也可以改一下(VB娃娃)的代码,一样方法 Dim posR As Integer, posL As Integer Dim strTmp As String, blnTmp As Boolean posR = Text1.SelStart + 1 posL = Text1.SelStart
Do Until Mid(Text1.Text, posR, 1) = " " If posR > Len(Text1.Text) Then Exit Do If (Asc(Mid(Text1.Text, posR, 1)) >= 65 And Asc(Mid(Text1.Text, posR, 1)) <= 90) Or (Asc(Mid(Text1.Text, posR, 1)) >= 97 And Asc(Mid(Text1.Text, posR, 1)) <= 122) Then strTmp = strTmp & Mid(Text1.Text, posR, 1) posR = posR + 1 Else strTmp = Mid(Text1.Text, posR, 1) blnTmp = True Exit Do End If Loop
If posL = 0 Then posL = 1 Do Until Mid(Text1.Text, posL, 1) = " " If (Asc(Mid(Text1.Text, posL, 1)) >= 65 And Asc(Mid(Text1.Text, posL, 1)) <= 90) Or (Asc(Mid(Text1.Text, posL, 1)) >= 97 And Asc(Mid(Text1.Text, posL, 1)) <= 122) Then If posL > 0 Then strTmp = Mid(Text1.Text, posL, 1) & strTmp posL = posL - 1 End If If posL = 0 Then Exit Do Else If blnTmp = False Then'已经取过一个汉字就不用再考虑向左取词了 strTmp = Mid(Text1.Text, posL, 1) End If Exit Do End If Loop
(2)得到鼠标所在窗口的handle,调用getwindowtext() sendmessage(wm_gettext)等等api获取文字,这样大部分控件的也能得到。
Dim s$, ss$, n&, s1$, s2$
s = Form1.Text1.Text
n = Form1.Text1.SelStart
s1 = Left(s, n): s2 = Mid(s, n + 1)
ss = Mid(s1, InStrRev(s1, Space(1)) + 1) & Left(s2, IIf(InStr(s2, Space(1)) = 0, Len(s2), InStr(s2, Space(1)) - 1))
Form1.Text2.Text = Form1.Text2.Text & ss
End Sub
昨天那个有带空格。Form1.Text2.Text = Form1.Text2.Text & ss & space(1)'去掉& space(1)
Mid(s1, InStrRev(s1, Space(1)) + 1) 和
Left(s2, IIf(InStr(s2, Space(1)) = 0, Len(s2), InStr(s2, Space(1)) - 1))
其实我的代码可以看做他的代码的解释。你取词一般也就几个或十几个字符,循环并不慢。
如果取字符串,他的代码速度快。
Private Sub Text1_Click()
Dim s$, ss$, n&, s1$, s2$
s = Form1.Text1.Text
n = Form1.Text1.SelStart: If n = 0 Then n = 1
ss = Mid(s, n, 1)
Form1.Text2.Text = Form1.Text2.Text & ss
End Sub
也可以改一下(VB娃娃)的代码,一样方法
Dim posR As Integer, posL As Integer
Dim strTmp As String, blnTmp As Boolean
posR = Text1.SelStart + 1
posL = Text1.SelStart
Do Until Mid(Text1.Text, posR, 1) = " "
If posR > Len(Text1.Text) Then Exit Do
If (Asc(Mid(Text1.Text, posR, 1)) >= 65 And Asc(Mid(Text1.Text, posR, 1)) <= 90) Or (Asc(Mid(Text1.Text, posR, 1)) >= 97 And Asc(Mid(Text1.Text, posR, 1)) <= 122) Then
strTmp = strTmp & Mid(Text1.Text, posR, 1)
posR = posR + 1
Else
strTmp = Mid(Text1.Text, posR, 1)
blnTmp = True
Exit Do
End If
Loop
If posL = 0 Then posL = 1
Do Until Mid(Text1.Text, posL, 1) = " "
If (Asc(Mid(Text1.Text, posL, 1)) >= 65 And Asc(Mid(Text1.Text, posL, 1)) <= 90) Or (Asc(Mid(Text1.Text, posL, 1)) >= 97 And Asc(Mid(Text1.Text, posL, 1)) <= 122) Then
If posL > 0 Then
strTmp = Mid(Text1.Text, posL, 1) & strTmp
posL = posL - 1
End If
If posL = 0 Then Exit Do
Else
If blnTmp = False Then'已经取过一个汉字就不用再考虑向左取词了
strTmp = Mid(Text1.Text, posL, 1)
End If
Exit Do
End If
Loop
Text2.Text = Text2.Text & IIf(Text2.Text = "", strTmp, " " & strTmp)