65.9K
CodeProject 正在变化。 阅读更多。
Home

Char Ribbon

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.29/5 (5投票s)

2008年3月1日

CPOL

2分钟阅读

viewsIcon

39344

downloadIcon

722

一个简单的 Ribbon 控件库

CharRibbonSample

背景

大家好!这是我的第一篇文章,我在 C# 方面属于中级初学者。有一天没事做,我就想创建一个具有所有 3 种样式的基本 Office 2007 风格 Ribbon。希望你喜欢。我似乎无法渲染某些 Ribbon,而且当你操作它时通常会闪烁。这包含几个控件,所以我会带你了解一下其中的一些。

CharCheckBox

渲染 CharCheckBox 控制很简单。为了在鼠标悬停时创建 DarkBlue 轮廓,我们需要猜测 checkbox 本身的 X 和 Y 坐标。我们还需要猜测 checkbox 的宽度和高度。代码 1 中的示例就是这样做的。代码 2 提供了我们在悬停在控件上时获得的“橙色”效果。

// Code 1
Pen p = new Pen(Color.MidnightBlue);
Rectangle rect = new Rectangle(0, 1, 12, 12);
g.DrawRectangle(p, rect);
p.Dispose();

// Code 2
LinearGradientBrush HLin = new LinearGradientBrush(
new Point(1, 0),
new Point(0, 1),
Color.FromArgb(59, Color.Orange.R, Color.Orange.G, Color.Orange.B),
Color.FromArgb(59, Color.OrangeRed.R, Color.OrangeRed.G, Color.OrangeRed.B));
Graphics g = this.CreateGraphics();
Rectangle HRect = new Rectangle(0, 1, 12, 12);
g.FillRectangle(HLin, HRect);

这与 CharCheckBox 非常相似。只是这次,我们需要绘制我们的“radiobutton”。代码 3 中的源代码负责处理这一点。

// Code 3
Graphics g = this.CreateGraphics();
g.SmoothingMode = SmoothingMode.AntiAlias;
if (this.Checked)
{
Pen BrPen = new Pen(Color.FromArgb(74, 107, 150));
g.FillEllipse(BrPen.Brush, 2, 4, 7, 7);
BrPen.Dispose();
}

RibbonPanelTab

这里变得有趣了。我们有一个名为“Style”的 enum。这个 enum 为我们处理所有三种样式,包括:Office2007BlackOffice2007BlueOffice2007Silver。要做到这一点很简单,我们只需创建一个包含每种样式的 switch,以及 3 个 floatColor 数组用于我们的样式混合(LinearGradientBrush lg 是我们的目标混合对象)。下面的代码展示了它是如何工作的

Color[] Silvercol = new Color[] { 
Color.FromArgb(238, 241, 246), Color.FromArgb(226, 229, 234),
Color.FromArgb(238, 244, 244) };
float[] Silverpos = new float[] { 0.0f, 0.4f, 1.0f };
Color[] SBlue = new Color[] {
Color.FromArgb(205, 210, 217), Color.FromArgb(190, 196, 204),
Color.FromArgb(228, 234, 235) };
float[] Bluepos = new float[] { 0.0f, 0.4f, 1.0f };
Color[] col = new Color[] { 
Color.FromArgb(220, 231, 244), Color.FromArgb(204, 220, 238),
Color.FromArgb(216, 232, 245) };
float[] pos = new float[] { 0.0f, 0.4f, 1.0f };
ColorBlend blend = new ColorBlend();
if (Style == e_Style.Office2007Blue)
{
blend.Colors = col;
blend.Positions = pos;
}
if (Style == e_Style.Office2007Black)
{
blend.Colors = SBlue;
blend.Positions = Bluepos;
}
if (Style == e_Style.Office2007Silver)
{
blend.Colors = Silvercol;
blend.Positions = Silverpos;
}
LinearGradientBrush lg = new LinearGradientBrush(
this.ClientRectangle, Color.White, Color.White, (float)90, false);
lg.InterpolationColors = blend;
e.FillRectangle(lg, this.ClientRectangle);

为了获得我们的弧形,我们只需将我们的圆角矩形绘制为 GraphicsPath(见下文)。

// Round Rectangle
float radius2 = new float(); radius2 = (float)2;
float X2 = new float(); X2 = 0;
float Y2 = new float(); Y2 = this.Height / 2 + this.Height / 2 - 15;
float width2 = new float(); width2 = this.Width - 1;
float height2 = new float(); height2 = 15;
GraphicsPath gp3 = new GraphicsPath();
gp3.AddLine(X2 + radius2, Y2, X2 + width2 - (radius2 * 2), Y2);
gp3.AddArc(X2 + width2 - (radius2 * 2), Y2, radius2 * 2, radius2 * 2, 270, 90);
gp3.AddLine(X2 + width2, Y2 + radius2, X2 + width2, Y2 + height2 - (radius2 * 2));
gp3.AddArc(X2 + width2 - (radius2 * 2), Y2 + 
	height2 - (radius2 * 2), radius2 * 2,radius2 * 2, 0, 90);
gp3.AddLine(X2 + width2 - (radius2 * 2), Y2 + height2, X2 + radius2, Y2 + height2);
gp3.AddArc(X2, Y2 + height2 - (radius2 * 2), radius2 * 2, radius2 * 2, 90, 90);
gp3.AddLine(X2, Y2 + height2 - (radius2 * 2), X2, Y2 + radius2);
gp3.AddArc(X2, Y2, radius2 * 2, radius2 * 2, 180, 90);
gp3.CloseFigure();

然后填充它

Pen arcpen = new Pen(Color.Transparent);
switch (this.Style)
{
case e_Style.Office2007Blue:
LinearGradientBrush Styleblue = new LinearGradientBrush(
this.ClientRectangle,
Color.FromArgb(194, 216, 240),
Color.FromArgb(194, 217, 240), (float)90, false);
arcpen.Brush = Styleblue;
break;
case e_Style.Office2007Black:
LinearGradientBrush StyleBlack = new LinearGradientBrush(
this.ClientRectangle,
Color.FromArgb(184, 185, 185),
Color.FromArgb(158, 160, 160), (float)90, false);
arcpen.Brush = StyleBlack;
break;
case e_Style.Office2007Silver:
LinearGradientBrush StyleSilver = new LinearGradientBrush(
this.ClientRectangle,
Color.FromArgb(222, 226, 238),
Color.FromArgb(197, 201, 211), (float)90, false);
arcpen.Brush = StyleSilver;
break;
}
e.FillPath(arcpen.Brush, gp3);

CharStatusStrip

几乎与 ribbonPaneltab 相同,除了我们不绘制弧形,并且使用不同的混合颜色。

结束

是的,我可以讨论 Ribbon,但它基本上与 ribbonPaneltab 相同,只是带有一个 tabcontrol,并且我们正在渲染选项卡。希望你喜欢这篇文章,你可以自由编辑、贡献和向控件集合中添加控件。

历史

  • 2008 年 3 月 1 - 发布
© . All rights reserved.