Private Sub mOwnEdit_KeyUp(KeyCode As Integer, Shift As Integer)
If LockKey Then '若已锁定上下键,则使光标回到原位
fOwnEdit.SelStart = LockStart
fOwnEdit.SetFocus
LockKey = False '解除锁定
End If
End Sub
Private Sub mOwnEdit_LostFocus()
If bListting Then fBox.GoBack '若文本框失去焦点,隐藏
End Sub
Private Sub mOwnEdit_MouseDown(Button As Integer, _
Shift As Integer, x As Single, y As Single)
If Not bListting Then Exit Sub
'若鼠标点向当前单词外的区域,则隐藏
If fOwnEdit.SelStart < lEnd Or fOwnEdit.SelStart > lThis Then fBox.GoBack
End Sub
Private Sub UserControl_Initialize()
iNum = 0
fBox.List1(0).Visible = False
End Sub
3)在窗体fBox中加入代码:
Option Explicit
Public lThisList As ListBox '当前列表框
Private Declare Function GetCaretPos Lib "user32" _
(lpPoint As POINTAPI) As Long
Private Declare Function ClientToScreen Lib "user32" _
(ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private Type POINTAPI 'GetCaretPos和ClientToScreen要用到此结构
x As Long
y As Long
End Type
Dim mPoint As POINTAPI
Public Sub ReceiStr(sListStr As String)
Dim i As Integer
'在列表框中查找匹配项
For i = 0 To lThisList.ListCount - 1
If Left(Replace(lThisList.List(i), sListStr, "$"), 1) = "$" Then
'头字符为"$",证明lThisList.List(i)起始位置含sListStr串
lThisList.ListIndex = i '加亮对应列表项
Exit Sub
End If
Next
lThisList.ListIndex = -1 '未找到
End Sub
Public Sub PopOut(sKey As String)
On Error Resume Next
Dim x As Integer, y As Integer
If Not lThisList Is Nothing Then _
lThisList.Visible = False '隐藏上次弹出的列表框
Set lThisList = Keys.Item(sKey) '设置本次列表框
With lThisList
.Move -10, -10, 2000, 1500 '设置列表框大小
Set Me.Font = .Font '由 fBox 窗体获得字符高度
If Me.TextHeight(.List(0)) * .ListCount < .Height - 30 Then
.Height = Me.TextHeight(.List(0)) * .ListCount + 30
End If
Width = .Width + (Width - ScaleWidth) - 30
Height = .Height + (Height - ScaleHeight) - 30
.Visible = True
End With
GetCaretPos mPoint '取得光标在文本框中的位置
ClientToScreen fOwnEdit.hwnd, mPoint '转换成屏幕位置
Set Me.Font = fOwnEdit.Font '由 fBox 窗体获得字符高度
'在光标位置弹出列表框(注意单位转化)
x = mPoint.x * Screen.TwipsPerPixelX
y = mPoint.y * Screen.TwipsPerPixelY + TextHeight(sKey)
'若超过屏幕范围,调整
If x + Me.Width > Screen.Width Then x = x - Me.Width
If y + Me.Height > Screen.Height Then y = y - Me.Height - Me.TextHeight(sKey)
Me.Move x, y
Show 0, fOwnForm '浮动显示于外部程序窗体
fOwnForm.SetFocus '设回焦点
End Sub
Public Sub GoBack()
'隐藏,复位各项标志
lThisList.ListIndex = -1
bListting = False
lEnd = 0
lThisList.Visible = False
Me.Hide
End Sub
Private Sub List1_Click(Index As Integer)
fOwnForm.SetFocus '设回焦点
End Sub
Private Sub List1_DblClick(Index As Integer)
'双击等效于插入列表项加一个空格
fOwnEdit.SetFocus
SendKeys " "
DoEvents
GoBack
End Sub
Private Sub List1_Scroll(Index As Integer)
fOwnForm.SetFocus '设回焦点
End Sub
至此,控件AutoList控件已大功告成,怎样使用它呢?很简单。
二、在应用程序中演示AutoList控件的功能:
选菜单File\New Project\Stand EXE 新建一个标准EXE工程,并在工程管理器上用鼠标右键单击此工程,在弹出的菜单中选“Set as start up”,将它设为启动工程。在默认的窗体Form1上加入一个TextBox控件,设置其属性值如表中所示。
关闭控件设计窗体,工具箱中AutoList控件的图标变为可用,在Form1上再加入一个AutoList控件。双击窗体在其代码窗体中加入如下代码:
'将AutoList控件加入此窗体,加入少量代码,
'即可使Text1具有“自动列表”功能。
Option Explicit
Private Sub Form_Load()
Text1.Text = ""
Text1.Font.Bold = True
Text1.Font.Size = 20
With AutoList1
Set .OwnForm = Me '控件将浮动于此窗体上
Set .OwnEdit = Text1 '控件输入到此文本框
.Visible = False '控件不显示
'添加分隔符
.AddSeparator " "
.AddSeparator "."
.AddSeparator "="
'添加关键字
.AddKey "as"
.AddKey "form1"
'为关键字“as”添加列表项
.AddList "as", "screen"
.AddList "as", "computer"
.AddList "as", "keyboard"
.AddList "as", "memory"
.AddList "as", "form1"
.AddList "as", "internet"
.AddList "as", "longyuanxian"
'为关键字“form1”添加列表项
.AddList "form1", "autoredraw"
.AddList "form1", "appearance"
.AddList "form1", "drawmode"
.AddList "form1", "zhangqiufeng"
.AddList "form1", "as"
.AddList "form1", "control"
.AddList "form1", "text1"
.AddList "form1", "yuecaowei"
End With
End Sub
Private Sub Form_Resize()
On Error Resume Next
Text1.Move 0, 0, Me.ScaleWidth, Me.ScaleHeight
End Sub
好哪!忙了这么久,看一下自己的成果吧!按下F5键运行,键入“form1”,再键入“.”(或 其它分隔符、回车符)。哇!关键字“form1”对应的列表框弹出来了!再键入“a”,列表项“appearance”变亮了,键入“s”,“as”项立即变亮了(如图所示),试着按上下方向键,亮条便在列表项间移动了,键入空格(或键入任意分隔符),加亮的列表项马上跳出到当前光标处(鼠标双击列表项也可达到此效果)。不光您键入字符时AutoList控件会自动匹配,当您向回删除时它也会自动匹配。例如:您敲“form1”、空格,此时列表框弹出,继续敲入“appce”, AutoList找不到匹配项,没有列表项被加亮,此时按退格键,当删完字母“c” 字母时,“appearance”立即被加亮显示!怎么样?很“酷”吧。总之此控件具有较完善、实用的功能,并且使用起来也很简单。
您若想将某个关键字、列表项或分隔符从AutoList中删除,请用带“Del”前缀的方法;要重置整个控件,请调用FreeMem方法清空它,然后就可以重新加入关键字了。有了这些方法,在实际应用中,您就可以根据需要动态的加入、删除关键字、列表项和分隔符,使控件动态的适应程序的需要。使用AutoList控件,您一定会发现,VB、VC6.0的代码编辑器的“自动列出成员”功能并不难。当然您若在使用中发现AutoList的缺陷,完全可以自由的改进它。
本程序在VB6.0企业版环境下调试运行通过。
在设计时需设置的属性值
控件所在窗体 |
控件名称 |
属性名称 |
设置值 |
fBox |
List1 |
Index |
0 |
|
|
Sorted |
True |
|
|
Appearance |
0-Flat |
Form1 |
Text1 |
Multiline |
True |
|
|
ScrollBars |
3-Both |
AutoList 控件运行时的外观
|