判断是否是中文字符的函数如下
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Byte, ByVal Source As String, ByVal Length As Long)
'该函数判断传入字符是否是GBK汉字库中的字符
'如果是则返回True,否则返回False
Private Function IS_GBK_HZ(ByVal sHZ As String) As Boolean
Dim aHZ(1) As Byte
sHZ = Left(sHZ, 1)
If Len(sHZ) > 0 Then
CopyMemory aHZ(0), sHZ, 2
IS_GBK_HZ = (aHZ(0) >= &H81) And (aHZ(0) < &HFE) And (aHZ(1) >= &H40) And (aHZ(1) < &HFF)
End If
End Function
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Byte, ByVal Source As String, ByVal Length As Long)
'该函数判断传入字符是否是GBK汉字库中的字符
'如果是则返回True,否则返回False
Private Function IS_GBK_HZ(ByVal sHZ As String) As Boolean
Dim aHZ(1) As Byte
sHZ = Left(sHZ, 1)
If Len(sHZ) > 0 Then
CopyMemory aHZ(0), sHZ, 2
IS_GBK_HZ = (aHZ(0) >= &H81) And (aHZ(0) < &HFE) And (aHZ(1) >= &H40) And (aHZ(1) < &HFF)
End If
End Function
If (KeyAscii > 0) And (KeyAscii <> 8) Then
KeyAscii = 0
End If
Select Case KeyAscii
Case 8 Case 13
Text4.SetFocus
Case Else
If hex(KeyAscii)<&h4E00 and hex(KeyAscii)>&9FAF Then
Text3.Text = Text3.Text
msgbox "这是你的要求",,"提示信息"
Else
KeyAscii = 0
End If
End Select
End Sub
to MonkeyLin
记得资源共享
private sub command1_click()
for i=0 to len(text1.text)
if IS_GBK_HZ(mid(text1.text,i,1))=false then 'IS_GBK_HZ是ltpao(啊炮)的程序
msgbox"it is not Chinese"
end if
next i
end sub
可惜的是该程序没把标点符号去掉.
Private Function IS_GBK_HZ(ByVal sHZ As String) As Boolean
Dim aHZ(1) As Byte
sHZ = Left(sHZ, 1)
If Len(sHZ) > 0 Then
CopyMemory aHZ(0), sHZ, 2
IS_GBK_HZ = (((aHZ(0) >= &H81) And (aHZ(0) < &HA1)) Or ((aHZ(0) >= &HAA) And (aHZ(0) < &HFE))) And (aHZ(1) >= &H40) And (aHZ(1) < &HFF)
End If
End Function
Private Sub Text1_Change()
Debug.Print Right(Text1.Text, 1) & " " & IS_GBK_HZ(Right(Text1.Text, 1))
End Sub
你的测试程序怎样?
private sub command1_click()
if is_gbk_hz(text1.text) then
msgbox "true"
else
msgbox "false"
end if
end sub
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
,如果是这样则有可能在内存拷贝时出错,请把你的声明改为下面的声明
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Byte, ByVal Source As String, ByVal Length As Long)