【实例说明】 聊天工具中,实现聊天记录自动卷动,让最新记录显示在TextBox。
【编程思路】 API函数来实现。自动卷动TextBox可以不用API函数来实现,设置TextBox的一些属性即可。
【设计步骤】 1.新建一个标准工程,创建一个新窗体,命名为Form1。
2.在窗体上放置一个Label控件和两个TextBox控件。
3.源程序 [素材源程序下载]
Option Explicit
'常数声明
Private Const EM_LINESCROLL = &HB6
'API声明
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
'用API实现自动卷动TextBox
Private Sub subMove()
Call SendMessage(Text1.hwnd, EM_LINESCROLL, 0, 1)
End Sub
'发送消息
Private Sub Text2_KeyDown(KeyCode As Integer, Shift As Integer)
If Shift = 2 And KeyCode = 13 Then
If Len(Text1.Text) = 0 Then
Text1.Text = Date & Space(2) & Time & vbCrLf & Text2.Text
Else
Text1.Text = Text1.Text & vbCrLf & Date & Space(2) & Time & Text2.Text
End If
Call subMove
'Text2清空
Text2.Text = ""
End If
End Sub
|