前不久,有位朋友发帖,寻求颜色选择的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
新闻热点
疑难解答
图片精选