在這一期的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
end if
exit sub
errlab:
resume next
end sub
執行上面的程式,當游標在treeview1上面移動時,treeview1的tooltips就會根據游標所在的不同標題行而變動。
如何用SendMessage 函式傳遞字串
如何用 sendmessage 函式傳遞字串 一 兩個引數都帶字串的形式 void caaadlg onbutton1 lresult caaadlg oncurrentinfo wparam wparam,lparam lparam 二 只帶乙個字串的形式 cstring strpara t fg...
如何用SendMessage 函式傳遞字串
一 兩個引數都帶字串的形式 void caaadlg onbutton1 lresult caaadlg oncurrentinfo wparam wparam,lparam lparam 二 只帶乙個字串的形式 cstring strpara t fggg sendmessage hwnd,wm ...
SendMessage函式在兩個程式間的訊息傳遞
利用sendmessage函式傳送wm copydata訊息,從而實現不同程序間傳遞資料。用兩個應用程式,乙個是用來傳送訊息的應用程式。乙個是用來接受訊息的應用程式。傳送訊息的應用程式的設計 2 按鈕控制項事件過程中,先利用findwindow查詢接受訊息的應用程式,如果找到了,就用sendmesa...