Access开发培训
网站公告
·Access专家课堂QQ群号:151711184    ·Access快速开发平台下载地址及教程    ·欢迎加入Access专家课堂微信群!    ·如何快速搜索本站文章|示例|资料    
您的位置: 首页 > 技术文章 > 技术类

[转]SendMessage函数的常用消息及其应用4

时 间:2008-12-04 08:12:55
作 者:andymark   ID:1350  城市:深圳
摘 要:SendMessage函数的常用消息及其应用
正 文:

3、为树型列表控件中不同的标题行设置不同的提示
  介绍如何为树型列表控件中的每一个标题设置不同的提示。同 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 SendMessageRef 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获得标题文本
    Call SendMessageRef(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就会根据光标所在的不
同标题行而变动。


SendMessage函数应用(三)
  介绍关于RichTextBox(简称RTF)控件的消息和ListView控件的消息控制。

1.在Windows消息中,以EM_开头的消息是专门控制TextBox类控件和RTF类控件的。下面介绍的是如何利
用 EM_ 消息实现向 RichTextBox 中粘贴文本和位图。在 EM_消息中,有一个 EM_PASTESPECIAL 消息
可以从剪贴板中获取文本或者位图类数据并拷贝到 RTF 上。
  运行程序,点击Command1,可以将剪贴板中的数据(位图或者文本)拷贝到RichTextBox1上。
  在 EM_PASTESPECIAL 消息中,wParam 定义剪贴板数据类型,lParam 定义一个 repastespecial
类型的数据,其中的 dwAspect 说明数据以何种方式显示到 RTF 对象上,如果是 DVASPECT_CONTENT,
则将整个数据显示在 RTF 上,如果是 DVASPECT_ICON ,则只在 RTF 上显示一个图标。
  经试,repastespecial 格式没有作用,因此参数4设为0即可。

Private Type repastespecial
  dwAspect As Long '显示方式
  dwParam As Long
End Type

Const WM_USER = &H400
Const EM_PASTESPECIAL = 1088
Const CF_BITMAP = 2
Const CF_TEXT = 1
Const DVASPECT_CONTENT = 1
Const DVASPECT_ICON = 4

Private Sub Command1_Click()
Dim re As repastespecial
re.dwAspect = DVASPECT_CONTENT
If Clipboard.GetFormat(vbCFText) Then ''剪贴板中为文本信息
 Call SendMessage(RichTextBox1.hwnd, EM_PASTESPECIAL, CF_TEXT, re)
ElseIf Clipboard.GetFormat(vbCFBitmap) Then ''剪贴板中为位图信息
 Call SendMessage(RichTextBox1.hwnd, EM_PASTESPECIAL, CF_BITMAP, re)
End If
End Sub



Access软件网官方交流QQ群 (群号:54525238)       Access源码网店

常见问答:

技术分类:

相关资源:

专栏作家

关于我们 | 服务条款 | 在线投稿 | 友情链接 | 网站统计 | 网站帮助