imports Microsoft.VisualBasic
dim s as string= "苹果软件工作室"
s = Strings.StrConv(s, VbStrConv.Wide, 0) '半角转全角
s = Strings.StrConv(s, VbStrConv.TraditionalChinese, 0) ' 简体转繁体
s = Strings.StrConv(s, VbStrConv.ProperCase, 0) '首字母大写
s = Strings.StrConv(s, VbStrConv.Narrow , 0) '全角转半角
s = Strings.StrConv(s, VbStrConv.SimplifiedChinese , 0) '繁体转简体
以下代码为API方式自动把输入法转换为半角形式
'声明一些API函数
<Runtime.InteropServices.DllImport("imm32.dll")> _
Private Shared Function ImmGetContext(ByVal hwnd As IntPtr) As IntPtr
End Function
<Runtime.InteropServices.DllImport("imm32.dll")> _
Private Shared Function ImmGetOpenStatus(ByVal himc As IntPtr) As Boolean
End Function
<Runtime.InteropServices.DllImport("imm32.dll")> _
Private Shared Function ImmSetOpenStatus(ByVal himc As IntPtr, ByVal b As Boolean) As Boolean
End Function
<Runtime.InteropServices.DllImport("imm32.dll")> _
Private Shared Function ImmGetConversionStatus(ByVal himc As IntPtr, ByRef lpdw As Integer, ByRef lpdw2 As Integer) As Boolean
End Function
<Runtime.InteropServices.DllImport("imm32.dll")> _
Private Shared Function ImmSimulateHotKey(ByVal hwnd As IntPtr, ByVal lngHotkey As Integer) As Integer
End Function
Private Const IME_CMODE_FULLSHAPE As Integer = &H8
Private Const IME_CHOTKEY_SHAPE_TOGGLE As Integer = &H11
'下面这个函数是检查输入法的全角半角状态
Private Sub ChangeControlIme(ByVal h As IntPtr)
Dim HIme As IntPtr = ImmGetContext(h)
If ImmGetOpenStatus(HIme) Then '如果输入法处于打开状态
Dim iMode As Integer = 0
Dim iSentence As Integer = 0
Dim bSuccess As Boolean = ImmGetConversionStatus(HIme, iMode, iSentence) '检索输入法信息
If bSuccess Then
If (iMode And IME_CMODE_FULLSHAPE) > 0 Then '如果是全角
ImmSimulateHotKey(h, IME_CHOTKEY_SHAPE_TOGGLE) '转换成半角
End If
End If
End If
End Sub
Private Sub TextBox2_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.Enter
ChangeControlIme(sender.Handle)
End Sub