VB 汉字拼音及声调

这段VB代码实现了一个功能,能够将输入的汉字转换为全拼、无声调全拼以及拼音首字母。通过调用`GetChineseSpell`函数,结合`ImmEscape`和`ImmGetConversionList`等API,完成对微软拼音输入法的交互,从而获取汉字的拼音信息。
 
  1. Private Sub Command1_Click()
  2.     Text2 = "全拼+声调:" & GetChineseSpell(Text1, 0) & vbCrLf & "全拼:" & GetChineseSpell(Text1, 1) & vbCrLf & "拼音首字母:" & GetChineseSpell(Text1, 2)
  3. End Sub
  4. '===================================
  5. Option Explicit
  6. Private Const IME_ESC_MAX_KEY = 
  7. Private Const IME_ESC_IME_NAME = 
  8. Private Const GCL_REVERSECONVERSION = 
  9. Private Declare Function GetKeyboardLayoutList Lib "user32" (ByVal nBuff As Long, lpList As LongAs Long
  10. Private Declare Function ImmEscape Lib "imm32.dll" Alias "ImmEscapeA" (ByVal hkl As LongByVal himc As LongByVal un As Long, lpv As Any) As Long
  11. Private Declare Function ImmGetConversionList Lib "imm32.dll" Alias "ImmGetConversionListA" (ByVal hkl As LongByVal himc As LongByVal lpsz As String, lpCandidateList As Any, ByVal dwBufLen As LongByVal uFlag As LongAs Long
  12. Private Declare Function IsDBCSLeadByte Lib "kernel32" (ByVal bTestChar As ByteAs Long
  13. Public Function GetChineseSpell(ByVal CHINESE As StringOptional PYTYPE As Integer = 0, Optional Delimiter As String = " "As String
  14. If Len(Trim(CHINESE)) > 0 Then
  15.  Dim i As Long
  16.  Dim s As String
  17.  s = Space(255)
  18.  Dim IMEInstalled As Boolean
  19.  Dim j As Long
  20.  Dim a() As Long
  21.  ReDim a(255) As Long
  22.  j = GetKeyboardLayoutList(255, a(LBound(a)))
  23.  For i = LBound(a) To LBound(a) + j - 1
  24.    If ImmEscape(a(i), 0, IME_ESC_IME_NAME, ByVal s) Then
  25.      If Trim("微软拼音输入法") = Replace(Trim(s), Chr(0), ""Then
  26.       IMEInstalled = True
  27.       Exit For
  28.      End If
  29.    End If
  30.  Next i
  31.  If IMEInstalled Then
  32.    CHINESE = Trim(CHINESE)
  33.    Dim sChar As String
  34.    Dim Buffer0() As Byte
  35.    Dim bBuffer0() As Byte
  36.    Dim bBuffer() As Byte
  37.    Dim k As Long
  38.    Dim l As Long
  39.    Dim m As Long
  40.    For j = 0 To Len(CHINESE) - 1
  41.      sChar = Mid(CHINESE, j + 1, 1)
  42.    '  If Not InStr("《》,。/?、][{}“”‘’;:!·〈〉「」『』|〖〗【】()[]{}…—.,""'';:?//!", sChar) > 0 Then
  43.      Buffer0 = StrConv(sChar, vbFromUnicode)
  44.      If IsDBCSLeadByte(Buffer0(0)) Then
  45.       k = ImmEscape(a(i), 0, IME_ESC_MAX_KEY, Null)
  46.       If k Then
  47.         l = ImmGetConversionList(a(i), 0, sChar, 0, 0, GCL_REVERSECONVERSION)
  48.         If l Then
  49.          s = Space(255)
  50.          If ImmGetConversionList(a(i), 0, sChar, ByVal s, l, GCL_REVERSECONVERSION) Then
  51.            
  52.            bBuffer0 = StrConv(s, vbFromUnicode)
  53.            ReDim bBuffer(k * 2 - 1)
  54.            For m = bBuffer0(24) To bBuffer0(24) + k * 2 - 1
  55.              bBuffer(m - bBuffer0(24)) = bBuffer0(m)
  56.            Next m
  57.            sChar = Trim(StrConv(bBuffer, vbUnicode))
  58.            If InStr(sChar, vbNullChar) Then
  59.             sChar = Trim(Left(sChar, InStr(sChar, vbNullChar) - 1))
  60.            End If
  61.           End If
  62.          End If
  63.          
  64.         End If
  65.       End If
  66.     ' End If
  67.      GetChineseSpell = GetChineseSpell & Switch(PYTYPE = 0, sChar, PYTYPE = 1, Left(sChar, Len(sChar) - 1), PYTYPE = 2, UCase(Left(sChar, 1))) & IIf(PYTYPE = 2, "", Delimiter)  ''返回全拼
  68.      Next j
  69.  Else ''没安装“微软拼音输入法”,返回一个空格
  70.     GetChineseSpell = " "
  71.  End If
  72. Else
  73.  GetChineseSpell = "" ''输入为空字符串
  74. End If
  75. End Function
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值