怎么得到一个汉字的首拼音字母

lcs810 2003-01-20 01:32:30
各位大虾好,小弟想问怎么得到一个汉字的首拼音字母,如:吻别 得到的结果应为'wb'
'你好' 得到的结果应为'nh' ,有没有办法在VB中实现。谢谢了!
...全文
6 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
yvqq 2003-01-20
Public Function getHzPy(hzStr As String) As String
On Error Resume Next
'declare variable
Dim myHzm As Integer
Dim qm As Integer
Dim wm As Integer
Dim hznm As String
Dim str As String
Dim temp As String
str = ""
Do While Len(hzStr) > 0
' If Len(hzStr) > 1 Then
' myHzm = Asc(Left(hzStr, 1))
' Else
' myHzm = Asc(hzStr)
' End If
myHzm = Asc(Left(hzStr, 1))
hzStr = Right(hzStr, Len(hzStr) - 1)
If myHzm >= 0 And myHzm < 256 Then
'字母
str = str + Chr(myHzm)
GoTo nextloop
Else
'汉字
qm = (myHzm + 65536) \ 256 '取区码
wm = (myHzm + 65536) Mod 256 '取位码
'十进制到十六进制
hznm = tento(qm, 16) & tento(wm, 16)
End If
If "B0A1" <= hznm And hznm <= "B0C4" Then
str = str + "A"
ElseIf "B0C5" <= hznm And hznm <= "B2C0" Then
str = str + "B"
ElseIf "B2C1" <= hznm And hznm <= "B4ED" Then
str = str + "C"
ElseIf "B4EE" <= hznm And hznm <= "B6E9" Then
str = str + "D"
ElseIf "B6EA" <= hznm And hznm <= "B7A1" Then
str = str + "E"
ElseIf "B7A2" <= hznm And hznm <= "B8C0" Then
str = str + "F"
ElseIf "B8C1" <= hznm And hznm <= "B9FD" Then
str = str + "G"
ElseIf "B9FE" <= hznm And hznm <= "BBF6" Then
str = str + "H"
ElseIf "BBF7" <= hznm And hznm <= "BFA5" Then
str = str + "J"
ElseIf "BFA6" <= hznm And hznm <= "C0AB" Then
str = str + "K"
ElseIf "C0AC" <= hznm And hznm <= "C2E7" Then
str = str + "L"
ElseIf "C2E8" <= hznm And hznm <= "C4C2" Then
str = str + "M"
ElseIf "C4C3" <= hznm And hznm <= "C5B5" Then
str = str + "N"
ElseIf "C5B6" <= hznm And hznm <= "C5BD" Then
str = str + "O"
ElseIf "C5BE" <= hznm And hznm <= "C6D9" Then
str = str + "P"
ElseIf "C6DA" <= hznm And hznm <= "C8BA" Then
str = str + "Q"
ElseIf "C8BB" <= hznm And hznm <= "C8F5" Then
str = str + "R"
ElseIf "C8F6" <= hznm And hznm <= "CBF9" Then
str = str + "S"
ElseIf "CBFA" <= hznm And hznm <= "CDD9" Then
str = str + "T"
ElseIf "CDDA" <= hznm And hznm <= "CEF3" Then
str = str + "W"
ElseIf "CEF4" <= hznm And hznm <= "D188" Then
str = str + "X"
ElseIf "D1B9" <= hznm And hznm <= "D4D0" Then
str = str + "Y"
ElseIf "D4D1" <= hznm And hznm <= "D7F9" Then
str = str + "Z"
Else
str = str + hznm
End If
nextloop:
Loop
getHzPy = str
End Function
'************************辅助函数,可以从十进制转换到任意进制**********************
'//入口为十进制数,要转换的进制,返回为该进制数
Public Function tento(m As Integer, n As Integer) As String
Dim q As Integer
Dim r As Integer
tento = ""
Dim bStr As String
Do
Call myDivide(m, n, q, r)
If r > 9 Then
bStr = Chr(55 + r)
Else
bStr = str(r)
End If
tento = Trim(bStr) & tento
m = q
Loop While q <> 0
End Function

'************************辅助过程,得到任意两个数的商和余数***************************
Public Sub myDivide(num1 As Integer, num2 As Integer, q As Integer, r As Integer)
If num2 = 0 Then
MsgBox ("非法除数")
Exit Sub
End If
If num1 / num2 >= 0 Then
q = Int(num1 / num2)
Else
q = Int(num1 / num2) + 1
End If
r = num1 Mod num2
End Sub
回复
zyl910 2003-01-20
回复人: fzx_qd(无底洞) ( ) 信誉:100 2002-11-18 14:16:00 得分:0


各位也不用找了!哥们直接贴出来得了!

'用于实现汉字与拼音转换的类模块

Private Const IME_ESC_MAX_KEY = &H1005
Private Const IME_ESC_IME_NAME = &H1006
Private Const GCL_REVERSECONVERSION = &H2

Private Type CANDIDATELIST
dwSize As Long
dwStyle As Long
dwCount As Long
dwSelection As Long
dwPageStart As Long
dwPageSize As Long
dwOffset(1) As Long
End Type

Private Declare Function GetKeyboardLayoutList Lib "user32" (ByVal nBuff As Long, lpList As Long) As Long
Private Declare Function ImmEscape Lib "imm32.dll" Alias "ImmEscapeA" (ByVal hkl As Long, ByVal himc As Long, ByVal un As Long, lpv As Any) As Long
Private Declare Function ImmGetConversionList Lib "imm32.dll" Alias "ImmGetConversionListA" (ByVal hkl As Long, ByVal himc As Long, ByVal lpsz As String, lpCandidateList As Any, ByVal dwBufLen As Long, ByVal uFlag As Long) As Long
Private Declare Function IsDBCSLeadByte Lib "kernel32" (ByVal bTestChar As Byte) As Long

Private Const NUM_OF_BUFFERS = 40
Private Const MSPY = "微软拼音输入法"
Dim imeHandle(1 To NUM_OF_BUFFERS) As Long
Dim imeName(1 To NUM_OF_BUFFERS) As String

Dim mlMSPYIndex As Long
Dim imeCount As Long


Private Sub Init()
Dim i As Long
Dim sName As String

mlMSPYIndex = 0
imeCount = GetKeyboardLayoutList(NUM_OF_BUFFERS, imeHandle(1))
If imeCount Then
For i = 1 To imeCount
sName = String(255, " ")
If ImmEscape(imeHandle(i), 0, IME_ESC_IME_NAME, ByVal sName) Then
If sName <> "" Then sName = Left(sName, InStr(sName, vbNullChar) - 1)
imeName(i) = sName
If sName = MSPY Then
mlMSPYIndex = i
End If
End If
Next i
End If

End Sub




Public Property Get MSPYInstalled() As Boolean
MSPYInstalled = IIf(mlMSPYIndex, True, False)
End Property

Public Property Get MSPYIndex() As Long
MSPYIndex = mlMSPYIndex
End Property

Public Property Get Count() As Long
Count = imeCount
End Property

Public Function GetHandle(ByVal lIndex As Long) As Long
If lIndex >= 1 And lIndex <= imeCount Then
GetHandle = imeHandle(lIndex)
End If
End Function

Public Function GetName(ByVal lIndex As Long) As String
If lIndex >= 1 And lIndex <= imeCount Then
GetName = imeName(lIndex)
End If
End Function

'得到全拼
Public Function GetAllOfPy(ByVal sString As String) As String
On Error GoTo GetAllOfPyErr
Dim lStrLen As Long
Dim i As Long
Dim sChar As String
Dim bChar() As Byte

If MSPYInstalled Then
lStrLen = Len(sString)
GetAllOfPy = ""
If lStrLen Then
For i = 1 To lStrLen
sChar = Mid(sString, i, 1)
bChar = StrConv(sChar, vbFromUnicode)
If IsDBCSLeadByte(bChar(0)) Then
Dim lMaxKey As Long
Dim lGCL As Long

lMaxKey = ImmEscape(imeHandle(mlMSPYIndex), 0, IME_ESC_MAX_KEY, Null)
If lMaxKey Then
Dim tCandi As CANDIDATELIST
lGCL = ImmGetConversionList(imeHandle(mlMSPYIndex), 0, sChar, 0, 0, GCL_REVERSECONVERSION)
If lGCL > 0 Then
Dim bBuffer() As Byte
Dim MaxKey As Long
Dim sBuffer As String
sBuffer = String(255, vbNullChar)
MaxKey = lMaxKey
lGCL = ImmGetConversionList(imeHandle(mlMSPYIndex), 0, sChar, ByVal sBuffer, lGCL, GCL_REVERSECONVERSION)
If lGCL > 0 Then
Dim bPY() As Byte
Dim j As Long

bBuffer = StrConv(sBuffer, vbFromUnicode)

ReDim bPY(MaxKey * 2 - 1)
For j = bBuffer(24) To bBuffer(24) + MaxKey * 2 - 1
bPY(j - bBuffer(24)) = bBuffer(j)
Next j
sChar = StrConv(bPY, vbUnicode)

If InStr(sChar, vbNullChar) Then
sChar = Trim(Left(sChar, InStr(sChar, vbNullChar) - 1))
End If
sChar = Left(sChar, Len(sChar) - 1) & " "
End If
End If
End If
End If
GetAllOfPy = GetAllOfPy & sChar
Next i
End If
Else
GetAllOfPy = sString
End If
Exit Function
GetAllOfPyErr:
GetAllOfPy = sString
End Function

Private Sub Class_Initialize()
Init
End Sub
回复
xyjdn 2003-01-20
有没有办法在VB中实现?
当然可以
回复
发动态
发帖子
VB基础类
创建于2007-09-28

7453

社区成员

VB 基础类
申请成为版主
社区公告
暂无公告