c#新手笔记之颜色下拉框

颜色填充下拉框

代码:

using  System.Reflection;

 
private   void  FillColorToCmb(ComboBox cmb)
        
{
            cmb.DrawMode 
= DrawMode.OwnerDrawFixed;
            cmb.DropDownStyle 
= ComboBoxStyle.DropDownList;
            cmb.DrawItem 
+=new DrawItemEventHandler(cmb_DrawItem);
            cmb.ItemHeight 
= 18;
            cmb.BeginUpdate();
            cmb.Items.Clear();           
            
foreach(PropertyInfo pi in typeof(Color).GetProperties(BindingFlags.Public|BindingFlags.Static))
            
{
                cmb.Items.Add(pi.Name);
            }

            cmb.EndUpdate();
        }


        
void   cmb_DrawItem( object  sender, DrawItemEventArgs e)
        
{
            
if(e.Index<0)
                
return;
            e.DrawBackground();
            Rectangle rect 
= new Rectangle(4, e.Bounds.Top + 2, e.Bounds.Height+10, e.Bounds.Height - 4);         
            ComboBox cmb 
= (ComboBox)sender;
            
string colorName = cmb.Items[e.Index].ToString();
            SolidBrush b 
= new SolidBrush(Color.FromName(colorName));
            e.Graphics.FillRectangle(b, rect);
            e.Graphics.DrawRectangle(Pens.Black, rect);
            Font myFont 
= new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular);
            e.Graphics.DrawString(colorName, myFont, Brushes.Black,
                
new RectangleF(e.Bounds.X + rect.Width + 4, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
            e.DrawFocusRectangle();
        }
 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值