前不久,有位朋友發帖,尋求顏色選擇的combobox 的制作方法,經過試驗,整理了出來,僅供參考。
(注:如有引用,請注明出處和作者)
--閔峰(2005/08/06東莞)
private sub filllistboxwithcolors()
me.combobox1.drawmode = drawmode.ownerdrawfixed
me.combobox1.dropdownstyle = comboboxstyle.dropdownlist
me.combobox1.itemheight = 15
'避免閃爍beginupdate
me.combobox1.beginupdate()
combobox1.items.clear()
dim pi as reflection.propertyinfo
for each pi in gettype(color).getproperties(reflection.bindingflags.public or reflection.bindingflags.static)
me.combobox1.items.add(pi.name)
next
combobox1.endupdate()
end sub
private sub combobox1_drawitem(byval sender as object, byval e as system.windows.forms.drawitemeventargs) handles combobox1.drawitem
if e.index < 0 then exit sub
dim rect as rectangle = e.bounds '每一項的邊框
'繪制項如果被選中則顯示高亮顯示背景,否則用白色
if e.state and drawitemstate.selected then
e.graphics.fillrectangle(systembrushes.highlight, rect)
else
e.graphics.fillrectangle(systembrushes.window, rect)
end if
dim colorname as string = combobox1.items(e.index)
dim b as new solidbrush(color.fromname(colorname))
'縮小選定項區域()
rect.inflate(-16, -2)
'填充顏色(文字對應的顏色)
e.graphics.fillrectangle(b, rect)
'繪制邊框()
e.graphics.drawrectangle(pens.black, rect)
dim b2 as brush
'確定顯示的文字的顏色()
if cint(b.color.r) + cint(b.color.g) + cint(b.color.b) > 128 * 3 then
b2 = brushes.black
else
b2 = brushes.white
end if
e.graphics.drawstring(colorname, me.combobox1.font, b2, rect.x, rect.y)
end sub
private sub form1_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
filllistboxwithcolors()
end sub
新聞熱點
疑難解答
圖片精選