SendMessage函数巧应用

2008-02-23 06:59:53来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

在这一期的SendMessage函数应用中,我将向大家介绍如何利用消息函数来扩展树型列表(TreeView)控件的功能相信对于树型列表控件大家十分的熟悉,典型的应用就是Windows资源管理器中的目录列表。在VB中,树型列表控件包含在Microsoft Windows Common Control 6.0(也可能是5.0,视你的VB或者系统版本而定)中。在Windows API中,有一系列的以TVM_ 开头的消息值,这些消息就是扩展树型列表控件所特定的消息值,下面向大家介绍其中的一些应用。
1、设置树型列表控件的背景颜色
首先做如下的定义:
Private Declare Function SendMessage Lib "user32" Alias "Send MessageA" (ByVal hwnd As Long,ByVal wMsg As Long, ByVal wParam As Long,ByVal lParam As Long) As Long
Const TV-FIRST = &H1100
Const TVM-SETBKCOLOR = TV_FIRST + 29
然后再作如下调用:
Call SendMessage(TreeView1.hwnd, TVM-SETBKCOLOR, 0, RGB(255, 0, 0))
上面的SendMessage调用将TreeView1的背景颜色设置为红色。
大家可能注意到了。在上面的Sendmessage函数定义中,我们将lParam定义为 ByVal lParam As Long,而不是象前面的那些范例那样定义为Any或者String类型,关于这个问题,我会在最后的一章中做介绍。
2、设置树型列表控件标题行高度
利用TVM_SETITEMHEIGHT消息可以设定控件的标题行的高度,该消息的定义及调用方法如下:
定义:
Const TV_FIRST = &H1100
Const TVM-SETITEMHEIGHT = TV_FIRST + 27
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
调用:
CallSendMessage(TreeView1.hwnd, TVM-SETITEMHEIGHT, 60, 0)
上面的代码将TreeView1的标题行高度设置到60像素高
3、为树型列表控件中不同的标题行设置不同的提示
在第一期的ListBox控件介绍中,我向大家介绍了如何为列表中的每一个标题行设置不同的提示(ToolTips),在这里为要向大家介绍如何为树型列表控件中的每一个标题设置不同的提示。
同ListBox控件不通,树型列表控件中并没有根据光标位置获得标题行索引的消息,我们需要另外想办法。在TVM类消息中有一个TVM_HITTEST消息,发送该消息可以检测控件表面上的某一点,如果该点位于一个标题上,则返回该标题的句柄。而利用TVM_GETITEM消息,则可以根据标题句柄返回该标题行的文本。所以结合利用这两个消息可以获取光标所在标题行的标题文本。具体的范例代码如下:
Option Explicit Private Type TPoint
x As Long
y As Long
End Type
Private Type TVHITTESTINFO
pt As TPoint
flags As Long
hItem As Long
End Type
Private Type TVITEM
mask As Long
HTreeItem As Long
state As Long
stateMask As Long
pszText As Long
cchTextMax As Long
iImage As Long
iSelectedImage As Long
cChildren As Long
lParam As Long
End Type
Const TV-FIRST = &H1100
Const TVM_HITTEST = TV-FIRST + 17
Const TVM_GETITEM = TV-FIRST + 12
Const TVHT-ONITEMLABEL = &H4
Const TVIF-TEXT = &H1
Const GMEM-FIXED = &H0
Private Declare Function Send MessageRef Lib"user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long,ByVal wParam As Long,lParam As Any) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal Destination As String,ByVal Source As Long,ByVal Length As Long)
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
Dim hItemPrv As Long
Private Sub Form_Load()
Dim ndX As Node
`加入若干Item
Set ndX = TreeView1.Nodes.Add(, , "R", "Root")
Set ndX = TreeView1.Nodes.Add("R", tvwChild, "Key1", "Node1")
Set ndX = TreeView1.Nodes.Add("Key1", tvwChild, "SubKey1", "SubNode1")
Set ndX = TreeView1.Nodes.Add("SubKey1", tvwChild, "SubKeys1", "SubNode1")
Set ndX = TreeView1.Nodes.Add("Key1", tvwChild, "SubKey2", "SubNode2")
Set ndX = TreeView1.Nodes.Add("Key1", tvwChild, "SubKey3", "SubNode3")
Set ndX = TreeView1.Nodes.Add("Key1", tvwChild, "SubKey4", "SubNode4")
End Sub
Private Sub TreeView1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim ptA As TPoint
Dim tf As TVHITTESTINFO
Dim tv As TVITEM
Dim hStr As Long
Dim hItem As Long
Dim astr As String * 1024
Dim bstr
On Error GoTo errLab
`获得当前光标所在的位置坐标
ptA.x = Int(x / Screen.TwipsPERPixelX)
ptA.y = Int(y / Screen.TwipsPerPixelY)
tf.pt = ptA
tf.flags = TVHT_ONITEMLABEL
`获得光标所在的Item的句柄
hItem = SendMessageRef(TreeView1.hwnd, TVM_HITTEST, 0, tf)
`如果未获得句柄或者同上一次是同一个Item的句柄则退出
If ((hItem <= 0) Or (hItem = hItemPrv)) Then Exit Sub
hItemPrv = hItem
`分配一定的内存空间用以存储Item的标题
hStr = GlobalAlloc(GMEM-FIXED, 1024)
If hStr > 0 Then
tv.mask = TVIF_TEXT
`获取标题文本
tv.HTreeItem = hItem
`Item句柄
tv.pszText = hStr
tv.cchTextMax = 1023
`发送TVM_GETITEM获得标题文本
CallSendMessageRef(TreeView1.hwnd, TVM-GETITEM, 0, tv)
`将标题文本拷贝到字符串astr中
CopyMemory astr, hStr, 1024
bstr = Left$(astr, (InStr(astr, Chr(0)) - 1))
TreeView1.ToolTipText = bstr
`释放分配的内存空间
GlobalFree hStr

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:标量变量和数组变量

下一篇:复本操作的对象、属性和方法(2)--网络数据库的复制和同步(3)