Office 2003 颜色选择器
精确模仿 Office 2003 颜色选择器,包括 ComboBox 和 ToolStripButton
引言
在开发一个具有 Office 2003 外观的项目时,我需要一个颜色选择器,其外观和感觉与 Windows 2003 应用程序完全相同。因此,我创建了一个。
使用代码
该项目有三个可供使用的控件:OfficeColorPicker
、ComboBoxColorPicker
和 ToolStripColorPicker
。前两个控件是 ToolBoxItem
控件,因此使用它们就像将控件从工具箱拖到您的窗体一样简单。第三个控件派生自 ToolStripDropDownButton
,本文稍后将讨论。所有这些控件都有一个 Color
属性,用于获取或设置控件的选定颜色。此外,它们还有一个名为 SelectedColorChanged
的事件,当控件的选定颜色发生变化时会触发该事件。
OfficeColorPicker
此控件保存所有颜色和颜色选择功能。它可以在应用程序中以无模式方式使用,通过这段代码,其中 this
是一个 Form
或任何其他容器
// Creates new instance of the OfficeColorPicker,
// adds it to the form control.
// Note: you may use OfficeColorPicker(Color)
// to start with a specified color.
OfficeColorPicker officeColorPicker = new OfficeColorPicker();
this.Controls.Add(officeColorPicker);
当然,打开颜色选择器的更好方法是使用提供的另外两个控件,将其作为弹出窗口。
ComboBoxColorPicker
此控件派生自 System.Windows.Forms.ComboBox
以在 ComboBox
中显示所选颜色。单击下拉箭头时,它将在弹出窗口中打开 OfficeColorPicker
控件,并具有上下文菜单行为,即在选择或失去焦点时关闭。
要使用此组合框控件
// Creates new instance of the ComboBoxColorPicker,
// adds it to the form control.
// Note: you may use ComboBoxColorPicker(Color)
// to start with a specified color.
ComboBoxColorPicker comboBoxColorPicker =
new ComboBoxColorPicker();
this.Controls.Add(comboBoxColorPicker);
ToolStripColorPicker
此控件派生自 System.Windows.Forms.ToolStripDropDownButton
,以便允许在任何 xxxStrip
控件中使用该按钮。示例包括 ToolStrip
、ContextMenuStrip
、MenuStrip
以及任何其他“知道”保存 ToolStripItem
的控件。要将 ToolStripColorPicker
添加到 ToolStrip
,请使用以下代码
// Creates a new tool strip to hold the ToolStripColorPicker:
ToolStrip toolStrip = new ToolStrip();
this.Controls.Add(toolStrip);
// Creates a new ToolStripColorPicker with starting color white
ToolStripColorPicker toolStripColorPicker =
new ToolStripColorPicker(Color.White);
// Adds the ToolStripColorPicker to the ToolStrip.
toolStrip.Items.Add(toolStripColorPicker);
使用 ToolStripColorPicker
的一种更简单的方法是利用 VS 2005 的设计时支持。只需将 ToolStrip
或任何其他 xxxStrip
控件添加到窗体中,然后使用控件的下拉列表添加 ToolStripColorPicker
VS 2005 使用某种反射来查找可添加的 ToolStripItem
,但这超出了本文的范围。ToolStripColorPicker
具有以下属性
/// <summary>
/// Gets or sets the ToolStripColorPickerDisplayType in order to
/// specify the display style of the button
/// - image, text, underline etc.
/// </summary>
public ToolStripColorPickerDisplayType ButtonDisplayStyle;
/// <summary>
/// Gets or sets the color assign to the color picker control.
/// </summary>
public Color Color;
/// <summary>
/// Gets or sets value indicating whether
/// to render the color name to the tool tip text.
/// </summary>
public bool AddColorNameToToolTip;
/// <summary>
/// Gets or sets the text that appears as a tooltip in the button.
/// the color name will be rendered to the tooltip
/// if the AddColorNameToolTip property set to true.
/// </summary>
new public string ToolTipText;
ButtonDisplayStyle
属性,与 DisplayStyle
不同,用于指定是绘制图像、文本和按钮的下划线,而不是仅绘制图像和文本,如 DisplayStyle
中所示。
事件
如上所述,所有这些控件都有 SelectedColorChanged
事件。当控件的选定颜色发生变化时,会触发此事件。要使用此事件
...
// Attach the event
this.colorPickerControl.SelectedColorChanged +=
new System.EventHandler(colorPickerControl_SelectedColorChanged);
...
// Handle the event
private void (colorPickerControl_SelectedColorChanged);
(object sender, EventArgs e)
{
Color newColor = colorPickerControl.Color;
// Do anything you want with the newColor
...
}
关注点
为了允许上下文菜单行为,我创建了一个 ContextMenuForm
类,它派生自 System.Windows.Forms.Form
。该窗体可以包含任何 Control
实例,并在失去焦点时隐藏自身。要使用此窗体
// Create new instance of the form
OfficePickers.Util.ContextMenuForm contextMenuForm =
new OfficePickers.Util.ContextMenuForm();
// Adds a control to the form (Dock will set to Dock.Fill automatically)
contextMenuForm.SetContainingControl(control);
// Shows the form as a pop-up
contextMenuForm.Show(this, new Point(0, 0), 100);
您可以将此窗体用于您想要的任何控件。要关闭窗体 - 即用户选择一个选项 - 请在您的控件中使用以下代码
Form parentForm = this.FindForm();
if(parentForm != null)
{
parentForm.Hide();
}
改进
- 添加对 XP 主题的支持:为此,您应该实现 一个主题读取器,并将其与保存所有颜色的
CustomColors
类交互。 - 添加自动颜色按钮:只需绘制另一个类似于“更多颜色”按钮的按钮并为其实现选择即可。
历史
- 2005 年 8 月 12 日 - 发布
OfficeColorPicker
版本 1.0。 - 2007 年 8 月 1 日 - 发布版本 1.1,包括
- 焦点错误已修复。
- 将 Enable 更改为 false 时,按钮将以灰度显示。
- 支持
SplitButton
:单击箭头将像今天一样显示颜色选择,但单击按钮部分本身将触发SelectedColorChanged
,用于更改当前所选元素的颜色,就像在 Office 中一样。