VB API 第二课 之 字符串大小写转换

直接上代码解释更加清楚

//说明:本实例用到4个API函数CharLower,CharLowerBuff,CharUpper,CharUpperBuff,分别是对字符串进行大小写转换。进行每个字符串大小写转换。

用到4个按钮,2个编辑框控件2个标签控件。

Option Explicit

Private Declare Function CharLower Lib "user32" Alias "CharLowerA" (ByVal lpsz As String) As String

Private Declare Function CharLowerBuff Lib "user32" Alias "CharLowerBuffA" (ByVal lpsz As String, ByVal cchLength As Long) As Long

Private Declare Function CharUpper Lib "user32" Alias "CharUpperA" (ByVal lpsz As String) As String

Private Declare Function CharUpperBuff Lib "user32" Alias "CharUpperBuffA" (ByVal lpsz As String, ByVal cchLength As Long) As Long

Dim c1 As Integer '大写的字符的个数
Dim c2 As Integer '小写的字符的个数
Dim s1 As String '字符串

Private Sub Command1_Click()
Text2.Text = CharUpper(Text1.Text) '字符串全部转换大写
c2 = 0 '小写的字符个数就为0

End Sub

Private Sub Command2_Click()
Text2.Text = CharLower(Text1.Text) '字符串全部转换小写
c1 = 0 '大写字符的个数为0
End Sub

Private Sub Command3_Click()
If c1 < Len(Text1.Text) Then '如果大写的字符个数小于编辑框字符的个数
c1 = c1 + 1 '大写的字符个数加1
c2 = Len(Text1.Text) - c1 '小写的个数就减一
Else
MsgBox "全部字符转换完毕!"
Exit Sub
End If
s1 = Text1.Text '已转换大写编辑框1的字符串赋值给s1
CharUpperBuff s1, c1 '第一个参数表示要转换的字符串,大写字符的个数
Text2.Text = s1 's1赋值给编辑框2的内容

End Sub

Private Sub Command4_Click() '同上
If c2 < Len(Text1.Text) Then
c2 = c2 + 1
c1 = Len(Text1.Text) - c2
Else
MsgBox "全部字符转换完毕!"
Exit Sub
End If
s1 = Text2.Text
CharLowerBuff s1, c2
Text2.Text = s1

End Sub

Private Sub Form_Load()
Text1.Text = ""
Text2.Text = ""
c1 = 0
c2 = 0

End Sub

 如图所示:

下节课字体函数的应用

posted @ 2014-10-05 15:08  Delphi爱好者2014  阅读(3011)  评论(1编辑  收藏  举报