Public Class Form1 Private Sub FillListBoxWithColors() Me.ComboBox1.DrawMode = DrawMode.OwnerDrawFixed Me.ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList Me.ComboBox1.ItemHeight = 15 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(-2, -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 End Class
用在 聊天室.选择颜色的例子上有
C# 的却没有
Public Class Form1 Private Sub FillListBoxWithColors()
Me.ComboBox1.DrawMode = DrawMode.OwnerDrawFixed
Me.ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
Me.ComboBox1.ItemHeight = 15
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(-2, -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
End Class
有没有C#代码啊,VB的我看不太懂!
public class Form1
{
private void FillListBoxWithColors()
{
this.ComboBox1.DrawMode = DrawMode.OwnerDrawFixed;
this.ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
this.ComboBox1.ItemHeight = 15;
this.ComboBox1.BeginUpdate();
ComboBox1.Items.Clear();
Reflection.PropertyInfo pi;
foreach ( pi in typeof(Color).GetProperties(Reflection.BindingFlags.Public | Reflection.BindingFlags.Static)) {
this.ComboBox1.Items.Add(pi.Name);
}
ComboBox1.EndUpdate();
}
private void ComboBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
if (e.Index < 0)
return; // TODO: might not be correct. Was : Exit Sub
Rectangle rect = e.Bounds;
//每一项的边框
//绘制项假如被选中则显示高亮显示背景, 否则用白色
if (e.State & DrawItemState.Selected) {
e.Graphics.FillRectangle(SystemBrushes.Highlight, rect);
}
else {
e.Graphics.FillRectangle(SystemBrushes.Window, rect);
}
string ColorName = ComboBox1.Items(e.Index);
SolidBrush b = new SolidBrush(Color.FromName(ColorName));
rect.Inflate(-2, -2);
//缩小选定项区域()
e.Graphics.FillRectangle(b, rect);
//填充颜色(文字对应的颜色)
e.Graphics.DrawRectangle(Pens.Black, rect);
//绘制边框()
Brush b2;
//确定显示的文字的颜色()
if ((int)b.Color.R + (int)b.Color.G + (int)b.Color.B > 128 * 3) {
b2 = Brushes.Black;
}
else {
b2 = Brushes.White;
}
e.Graphics.DrawString(ColorName, this.ComboBox1.Font, b2, rect.X, rect.Y);
}
private void form1_load(object sender, System.EventArgs e)
{
FillListBoxWithColors();
}
}