精彩控件源码(4)--Office 2003 Color Picker

本文介绍了一个模仿Office 2003界面的颜色选择器控件,包括OfficeColorPicker、ComboBoxColorPicker和ToolStripColorPicker三种形式。这些控件具有颜色选择事件和属性,可以方便地在Windows应用程序中使用,提供弹出式颜色选择体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 http://www.codeproject.com/KB/selection/office2003colorpicker.aspx

The OfficeColorPicker in action

Introduction

While working on a project with an Office 2003 look, I needed a color picker with the exact look and feel of a Windows 2003 application. So, I've created one.

Using the code

The project has three controls that are available for use: OfficeColorPicker, ComboBoxColorPicker and ToolStripColorPicker. The first two controls are ToolBoxItem controls and therefore using them is as simple as dragging the control from the toolbox to your form. The third control is derived from ToolStripDropDownButton and will be discussed later in this article. All of these controls have a Color property to get or set the selected color of the control. Also, they have an event called SelectedColorChanged that occurs when the selected color of the control changes.

OfficeColorPicker

The OfficeColorPicker placed in a form

This control holds all of the colors and functionality for color picking. It can be used modeless in the application by this code, where this is a Form or any other container:

Collapse Copy Code
// 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);

Of course, the better way to open the color picker is as a pop-up, using the other two controls provided.

ComboBoxColorPicker

This control derives from System.Windows.Forms.ComboBox to show the selected color in the ComboBox. When clicking on the drop-down arrow, it will open the OfficeColorPicker control in a pop-up with context menu behavior, i.e. it will close on selection or on loss of focus.

The ComboBoxColorPicker placed in a form

To use this combo box control:

Collapse Copy Code
// 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

The ComboBoxColorPicker placed in a form

This control derives from System.Windows.Forms.ToolStripDropDownButton in order to allow using the button inside any xxxStrip control. Examples include ToolStrip, ContextMenuStrip, MenuStrip and any other controls that "know" to hold ToolStripItem. To add ToolStripColorPicker to ToolStrip, use the following code:

Collapse Copy Code
// 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);

An easier way of using ToolStripColorPicker is to employ the design-time support of VS 2005. Just add ToolStrip or any other xxxStrip control to the form and use the drop-down list of controls to add ToolStripColorPicker:

Desing-Time support for the ToolStripTColorPicker

VS 2005 uses some sort of reflection to find the ToolStripItem available to add, but this subject is beyond the scope of this article. ToolStripColorPicker has the following properties:

Collapse Copy Code
/// <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;

The ButtonDisplayStyle property, unlike the DisplayStyle one, is used in order to specify whether to paint the image, the text and the underline for the button instead of just the image and the text, as in DisplayStyle.

Events

As mentioned above, all of these controls have the SelectedColorChanged event. This event occurs when the selected color of the control changes. To use this event:

Collapse Copy Code
...

// 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
       ...
}

Points of interest

To allow context menu behavior, I've created a ContextMenuForm class that derives from System.Windows.Forms.Form. This form may hold any Control instance and will hide itself when it loses focus. To use this form:

Collapse Copy Code
// 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);

You can use this form for any control you wish. To close the form -- i.e. the user selects an option -- use the following code in your control:

Collapse Copy Code
Form parentForm = this.FindForm();
if(parentForm != null)
{
    parentForm.Hide();
}

Improvements

  • Add support for XP themes: to do this, you should implement a Theme reader and interact it with the CustomColors class that holds all the colors.
  • Add the automatic color button: just paint another button like the More Colors button and implement the selection for it.

History

  • 8.12.2005 - Version 1.0 of OfficeColorPicker posted.
  • 1.8.2007 - Version 1.1 released, including:
    • Focus bug fixed.
    • When changing Enable to false, the button is painted in grayscale.
    • Support for SplitButton: clicking on the arrow will display the color selection as today, but clicking on the button portion itself will fire SelectedColorChanged to use for changing the color of the current selected element, like in Office.
1. 用户与身体信息管理模块 用户信息管理: 注册登录:支持手机号 / 邮箱注册,密码加密存储,提供第三方快捷登录(模拟) 个人资料:记录基本信息(姓名、年龄、性别、身高、体重、职业) 健康目标:用户设置目标(如 “减重 5kg”“增肌”“维持健康”)及期望周期 身体状态跟踪: 体重记录:定期录入体重数据,生成体重变化曲线(折线图) 身体指标:记录 BMI(自动计算)、体脂率(可选)、基础代谢率(根据身高体重估算) 健康状况:用户可填写特殊情况(如糖尿病、过敏食物、素食偏好),系统据此调整推荐 2. 膳食记录与食物数据库模块 食物数据库: 基础信息:包含常见食物(如米饭、鸡蛋、牛肉)的名称、类别(主食 / 肉类 / 蔬菜等)、每份重量 营养成分:记录每 100g 食物的热量(kcal)、蛋白质、脂肪、碳水化合物、维生素、矿物质含量 数据库维护:管理员可添加新食物、更新营养数据,支持按名称 / 类别检索 膳食记录功能: 快速记录:用户选择食物、输入食用量(克 / 份),系统自动计算摄入的营养成分 餐次分类:按早餐 / 午餐 / 晚餐 / 加餐分类记录,支持上传餐食照片(可选) 批量操作:提供常见套餐模板(如 “三明治 + 牛奶”),一键添加到记录 历史记录:按日期查看过往膳食记录,支持编辑 / 删除错误记录 3. 营养分析模块 每日营养摄入分析: 核心指标计算:统计当日摄入的总热量、蛋白质 / 脂肪 / 碳水化合物占比(按每日推荐量对比) 微量营养素分析:检查维生素(如维生素 C、钙、铁)的摄入是否达标 平衡评估:生成 “营养平衡度” 评分(0-100 分),指出摄入过剩或不足的营养素 趋势分析: 周 / 月营养趋势:用折线图展示近 7 天 / 30 天的热量、三大营养素摄入变化 对比分析:将实际摄入与推荐量对比(如 “蛋白质摄入仅达到推荐量的 70%”) 目标达成率:针对健
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值