用代碼實現ListView控件的行間隔顏色!
2024-07-21 02:24:39
供稿:網友
listview控件在寫程序的時候經常會用到,是一個很實用的控件。不過在顯示的數據比較多的時候(以報表風格顯示數據,類似于資源管理器的詳細查看),看起來不方便。大家可能對論壇比較熟悉,大多數論壇顯示的時候都是每行以不同的顏色進行顯示,以區分出不同行的數據。其實我們也可以借鑒一下這個方法,用程序來實現listview控件的行間隔顏色。
崔占民
email:[email protected]
首先在窗口中添加一個listview控件,方法:菜單->工程->部件->microsoft window common control 6.0 (后面為版本號)。再添加一個picture控件,改名為picgreenbar。
實現的代碼如下:
option explicit
private sub form_load()
dim i as integer
dim ifontheight as long
dim ibarheight as integer
dim j as integer
dim itmx as listitem
dim colhead as columnheader
listview1.columnheaders.add , , "this is just a simple example"
listview1.columnheaders(1).width = 3000
'添加一些實驗數據
for j = 1 to 33
set itmx = listview1.listitems.add()
itmx.text = "this is item number " & cstr(j)
next j
me.scalemode = vbtwips
picgreenbar.scalemode = vbtwips
picgreenbar.borderstyle = vbbsnone
picgreenbar.autoredraw = true
picgreenbar.visible = false
picgreenbar.font = listview1.font
ifontheight = picgreenbar.textheight("b") + screen.twipsperpixely
ibarheight = (ifontheight * 1)
picgreenbar.width = listview1.width
'======
picgreenbar.height = ibarheight * 2
picgreenbar.scalemode = vbuser
picgreenbar.scaleheight = 2
picgreenbar.scalewidth = 1
'draw the actual bars
picgreenbar.line (0, 0)-(1, 1), vbwhite, bf
picgreenbar.line (0, 1)-(1, 2), rgb(227, 241, 226), bf
'======
listview1.picturealignment = lvwtile
listview1.picture = picgreenbar.image
end sub
代碼量不多,不過很實用。