SENDMESSAGE函數(shù)巧應用發(fā)布時間:2008-04-03 10:38:02 來源:編程愛好者網(wǎng)站 作者:編程愛好者網(wǎng)站 點擊:250在這一期的SendMessage函數(shù)應用中,,我將向大家介紹如何利用消息函數(shù)來擴展樹型列表(TreeView)控件的功能相信對于樹型列表控件大家十分的熟悉,,典型的應用就是Windows資源管理器中的目錄列表。在VB中,,樹型列表控件包含在Microsoft Windows Common Control 6.0(也可能是5.0,,視你的VB或者系統(tǒng)版本而定)中。在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 + 2Array 然后再作如下調(diào)用: Call SendMessage(TreeView1.hwnd, TVM-SETBKCOLOR, 0, RGB(255, 0, 0)) 上面的SendMessage調(diào)用將TreeView1的背景顏色設置為紅色。 大家可能注意到了,。在上面的Sendmessage函數(shù)定義中,,我們將lParam定義為 ByVal lParam As Long,而不是象前面的那些范例那樣定義為Any或者String類型,,關于這個問題,,我會在最后的一章中做介紹。 2,、設置樹型列表控件標題行高度 利用TVM_SETITEMHEIGHT消息可以設定控件的標題行的高度,,該消息的定義及調(diào)用方法如下: 定義: 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 調(diào)用: CallSendMessage(TreeView1.hwnd, TVM-SETITEMHEIGHT, 60, 0) 上面的代碼將TreeView1的標題行高度設置到60像素高 3、為樹型列表控件中不同的標題行設置不同的提示 在第一期的ListBox控件介紹中,,我向大家介紹了如何為列表中的每一個標題行設置不同的提示(ToolTips),,在這里為要向大家介紹如何為樹型列表控件中的每一個標題設置不同的提示。 同ListBox控件不通,,樹型列表控件中并沒有根據(jù)光標位置獲得標題行索引的消息,,我們需要另外想辦法。在TVM類消息中有一個TVM_HITTEST消息,,發(fā)送該消息可以檢測控件表面上的某一點,,如果該點位于一個標題上,則返回該標題的句柄,。而利用TVM_GETITEM消息,,則可以根據(jù)標題句柄返回該標題行的文本。所以結(jié)合利用這兩個消息可以獲取光標所在標題行的標題文本,。具體的范例代碼如下: 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 `分配一定的內(nèi)存空間用以存儲Item的標題 hStr = GlobalAlloc(GMEM-FIXED, 1024) If hStr > 0 Then tv.mask = TVIF_TEXT `獲取標題文本 tv.HTreeItem = hItem `Item句柄 tv.pszText = hStr tv.cchTextMax = 1023 `發(fā)送TVM_GETITEM獲得標題文本 CallSendMessageRef(TreeView1.hwnd, TVM-GETITEM, 0, tv) `將標題文本拷貝到字符串a(chǎn)str中 CopyMemory astr, hStr, 1024 bstr = Left$(astr, (InStr(astr, Chr(0)) - 1)) TreeView1.ToolTipText = bstr `釋放分配的內(nèi)存空間 GlobalFree hStr End If Exit Sub errLab: Resume Next End Sub 運行上面的程序,當光標在TreeView1上面移動時,,TreeView1的ToolTips就會根據(jù)光標所在的不同標題行而變動,。 以上程序在WinArray8,、Win2000,VB6下運行通過 |
|