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