DrawComboBox

using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Microsoft.Win32;

#region FormNotepad_Load
private void FormNotepad_Load(object sender, EventArgs e)
{
    try
    {
        //colorComboBox.BeginUpdate();
        //foreach(KnownColor kc in Enum.GetValues(typeof(KnownColor)))
        //{
        //    Color bc = Color.FromKnownColor(kc);
        //    if (!bc.IsSystemColor && bc != Color.Transparent)
        //        colorComboBox.Items.Add(bc);
        //}
        //colorComboBox.EndUpdate();
        var query = from kc in Enum.GetNames(typeof(KnownColor))
                    let bc = Color.FromName(kc)
                    where !bc.IsSystemColor && bc != Color.Transparent // 禁用系统颜色和透明颜色。
                    select (bc as object); // 装箱转换。
        colorComboBox.Items.AddRange(query.ToArray());
        colorComboBox.ComboBox.DrawMode = DrawMode.OwnerDrawFixed; // 控件中的所有元素都是手动绘制的,并且元素大小都相等。
        colorComboBox.ComboBox.DrawItem += new DrawItemEventHandler(colorComboBox_DrawItem);
        using (RegistryKey colorKey = Registry.CurrentUser.CreateSubKey(@"Software/UserSubKey/Color"))
        {
            colorComboBox.SelectedItem = Color.FromName(colorKey.GetValue("BackColor") + "");
        }
    }
    catch (Exception se)
    {
        MessageBox.Show(this, se.Message, "写字板", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }
}
#endregion

#region FormNotepad_FormClosing
private void FormNotepad_FormClosing(object sender, FormClosingEventArgs e)
{
    using (RegistryKey subKey = Registry.CurrentUser.CreateSubKey(@"Software/UserSubKey/Color"))
    {
        subKey.SetValue("BackColor", richText.BackColor.Name);
    }
}
#endregion

#region RichTextBox.Color
private void colorComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
    richText.BackColor = (Color)colorComboBox.SelectedItem;
}

private void colorComboBox_DrawItem(object sender, DrawItemEventArgs e)
{
    using (Graphics g = e.Graphics)
    using (SolidBrush newBrush = new SolidBrush(e.BackColor))
    {
        if ((e.State & DrawItemState.Selected) != 0) // 取交集。
            newBrush.Color = Color.Cyan;
        Rectangle newClip = e.Bounds;
        g.FillRectangle(newBrush, newClip);
        newClip.Offset(1, 1);
        newClip.Width = 52;
        newClip.Height -= 3;
        g.DrawRectangle(SystemPens.ControlText, newClip);
        newClip.Offset(1, 1);
        newClip.Width -= 1;
        newClip.Height -= 1;
        Color newColor = (e.Index > -1) ? (Color)colorComboBox.Items[e.Index] : Color.White;
        newBrush.Color = newColor;
        g.FillRectangle(newBrush, newClip);
        newBrush.Color = e.ForeColor;
        g.DrawString(newColor.Name, e.Font, newBrush, newClip.X + 72, newClip.Y);
    }
}
#endregion 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值