您现在的位置是: 软件 > 开发者网络 > 程序方舟 > 开发专栏 > VB开发 > 正文
·速成电脑精英(包分配)白领高薪一族从这里开始



-Java套接字编程(下)
-MediaStudio Pro 6.5教程
-三款卸载软件最新试用
-基于Visual C++的Winsock API研究

SENDMESSAGE函数巧应用
2001-11-24· ·陈锐··yesky

上一页  1 2 3  

 

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
  End If
  Exit Sub
  errLab:
  Resume Next
  End Sub
 

  运行上面的程序,当光标在TreeView1上面移动时,TreeView1的ToolTips就会根据光标所在的不同标题行而变动。

  以上程序在Win98、Win2000,VB6下运行通过。

上一页  1 2 3  

■ 相关内容
 API实现完美的图片出现效果
感谢 访问天极网,如果您觉得该文章涉及版权问题,请看这里!