Office 2010 按钮






3.60/5 (5投票s)
本文档描述了如何扩展系统按钮,以实现 Office 2010 按钮的外观和感觉。
引言
本文演示了如何扩展系统按钮控件,以实现 Office 2010 按钮的外观和感觉。
基础
创建用户界面的第一步是选择合适的基组件,该组件提供完整的功能和可扩展性。对于实现按钮控件,ButtonBase
类将是正确的选择;为了简单起见,此示例使用 Button
作为基类。
要点
- 继承自基类。
- 重写必要的的方法和属性以进行自定义实现。
本示例继承自 Button
类,并重写 Paint 方法以实现 Office 2010 按钮的外观。
使用代码
OfficeButton
类中的关键部分是重写的 Paint 方法,该方法完全忽略基类的绘制实现,并提供自己的实现。
protected override void OnPaint(PaintEventArgs pevent)
{
PaintButtonBackground(pevent);
PaintImageAndText(pevent);
PaintButtonBorder(pevent);
}
图像和文本在按钮的所有三种状态下都可用,并且始终以相同的方式使用 PaintEventArgs
进行绘制,而背景绘制在所有三种状态下都不同。GetBackgroundGradientBrush
方法获取适当的画笔并填充背景。
private void PaintButtonBackground(PaintEventArgs e)
{
ButtonState state = GetButtonState();
using (LinearGradientBrush brush = GetBackgroundGradientBrush(state))
{
e.Graphics.FillRectangle(brush, this.ClientRectangle);
}
}
GetBackgroundGradientBrush
根据按钮状态返回适当的混合画笔。
private LinearGradientBrush GetBackgroundGradientBrush(ButtonState buttonState)
{
Color gradientBegin = Color.Empty;
Color gradientEnd = Color.Empty;
switch (buttonState)
{
...
...
...
case ButtonState.Selected:
LinearGradientBrush brush = new LinearGradientBrush(
this.ClientRectangle, gradientEnd, gradientBegin,
LinearGradientMode.Vertical);
this.SelectionBlend.Colors = this.InterpolationColors;
brush.InterpolationColors = this.SelectionBlend;
return brush;
}
return new LinearGradientBrush(this.ClientRectangle, gradientEnd,
gradientBegin, LinearGradientMode.Vertical);
}
希望这能让你更好地了解如何扩展系统控件以满足你的需求。