颜色下拉COMBOBOX控件(vb.net)

王朝vb·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

前不久,有位朋友发帖,寻求颜色选择的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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航