您的位置:首页 > 编程语言 > C#

【转】C# 实现完整功能的截图控件(3)-实现漂亮的快捷菜单

2010-09-27 08:52 621 查看
前面的两篇文章已经实现了两个截图控件所需要的控件:绘图工具栏控件颜色、字体选择控件,这篇文章将介绍截图控件所需的最后一个控件——快捷菜单,这个控件不需要做太多的工作,用ContextMenuStrip菜单控件就行了,需要做的只是对它进行美化,使它跟整个截图控件的控件风格保持一致,来看看最终的效果:

介绍实现绘图工具栏控件的那篇文章中,介绍了ToolStrip的美化,因为ContextMenuStrip也是通过继承ToolStrip来实现的控件,所以ContextMenuStrip的美化跟它一样,只需重写ToolStripRenderer的几个绘制方法就行了。还是利用ToolStrip的美化的时候实现的类ToolStripRendererEx,重写相应的方法,来看看代码:

protected override void OnRenderToolStripBackground(
ToolStripRenderEventArgs e)
...{
Color baseColor = ColorTable.BackColorNormal;
ToolStrip toolStrip = e.ToolStrip;
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;

if (toolStrip is ToolStripDropDown)
...{
RegionHelper.CreateRegion(e.ToolStrip, e.AffectedBounds);

Rectangle rect = e.AffectedBounds;

using (GraphicsPath path = GraphicsPathHelper.CreatePath(
rect, 8, RoundStyle.All, false))
...{
using (SolidBrush brush = new SolidBrush(
ColorTable.BackColorNormal))
...{
g.FillPath(brush, path);
}
using (Pen pen = new Pen(ColorTable.BorderColor))
...{
g.DrawPath(pen, path);

using (GraphicsPath innerPath =
GraphicsPathHelper.CreatePath(
rect, 8, RoundStyle.All, true))
...{
g.DrawPath(pen, innerPath);
}
}
}
}
else
...{
LinearGradientMode mode =
e.ToolStrip.Orientation == Orientation.Horizontal ?
LinearGradientMode.Vertical : LinearGradientMode.Horizontal;
RenderBackgroundInternal(
g,
e.AffectedBounds,
ColorTable.BackColorHover,
ColorTable.BorderColor,
ColorTable.BackColorNormal,
RoundStyle.All,
false,
true,
mode);
}
}

protected override void OnRenderSeparator(
ToolStripSeparatorRenderEventArgs e)
...{
Rectangle rect = e.Item.ContentRectangle;
if (e.ToolStrip is ToolStripDropDown)
...{
if (e.Item.RightToLeft == RightToLeft.Yes)
...{
//rect.X -= OffsetMargin + 4;
}
else
...{
rect.X += OffsetMargin + 4;
}
rect.Width -= OffsetMargin + 8;
}
RenderSeparatorLine(
e.Graphics,
rect,
ColorTable.BackColorPressed,
ColorTable.BackColorNormal,
SystemColors.ControlLightLight,
e.Vertical);
}

protected override void OnRenderMenuItemBackground(
ToolStripItemRenderEventArgs e)
...{
if (!e.Item.Enabled)
...{
return;
}

Graphics g = e.Graphics;
Rectangle rect = new Rectangle(Point.Empty, e.Item.Size);
g.SmoothingMode = SmoothingMode.AntiAlias;

if (e.Item.RightToLeft == RightToLeft.Yes)
...{
rect.X += 4;
}
else
...{
rect.X += OffsetMargin + 4;
}
rect.Width -= OffsetMargin + 8;
rect.Height--;

if (e.Item.Selected)
...{
RenderBackgroundInternal(
g,
rect,
ColorTable.BackColorHover,
ColorTable.BorderColor,
ColorTable.BackColorNormal,
RoundStyle.All,
true,
true,
LinearGradientMode.Vertical);
}
else
...{
base.OnRenderMenuItemBackground(e);
}
}

protected override void OnRenderImageMargin(
ToolStripRenderEventArgs e)
...{
if (e.ToolStrip is ToolStripDropDownMenu)
...{
Rectangle rect = e.AffectedBounds;
Graphics g = e.Graphics;
rect.Width = OffsetMargin;
if (e.ToolStrip.RightToLeft == RightToLeft.Yes)
...{
rect.X -= 2;
}
else
...{
rect.X += 2;
}
rect.Y += 1;
rect.Height -= 2;
g.SmoothingMode = SmoothingMode.AntiAlias;
using (LinearGradientBrush brush = new LinearGradientBrush(
rect,
ColorTable.BackColorHover,
Color.White,
90f))
...{
Blend blend = new Blend();
blend.Positions = new float[] ...{ 0f, .2f, 1f };
blend.Factors = new float[] ...{ 0f, 0.1f, .9f };
brush.Blend = blend;
rect.Y += 1;
rect.Height -= 2;
using (GraphicsPath path =
GraphicsPathHelper.CreatePath(
rect, 8, RoundStyle.All, false))
...{
g.FillPath(brush, path);
}
}

g.TextRenderingHint = TextRenderingHint.AntiAlias;
StringFormat sf = new StringFormat(StringFormatFlags.NoWrap);
Font font = new Font(
e.ToolStrip.Font.FontFamily, 11, FontStyle.Bold);
sf.Alignment = StringAlignment.Near;
sf.LineAlignment = StringAlignment.Center;
sf.Trimming = StringTrimming.EllipsisCharacter;

g.TranslateTransform(rect.X, rect.Bottom);
g.RotateTransform(270f);

if (!string.IsNullOrEmpty(MenuLogoString))
...{
Rectangle newRect = new Rectangle(
rect.X, rect.Y, rect.Height, rect.Width);

using (Brush brush = new SolidBrush(ColorTable.ForeColor))
...{
g.DrawString(
MenuLogoString,
font,
brush,
newRect,
sf);
}
}

g.ResetTransform();
return;
}

base.OnRenderImageMargin(e);
}

protected override void OnRenderItemImage(
ToolStripItemImageRenderEventArgs e)
...{
Graphics g = e.Graphics;
g.InterpolationMode = InterpolationMode.HighQualityBilinear;

if (e.Item is ToolStripMenuItem)
...{
ToolStripMenuItem item = (ToolStripMenuItem)e.Item;
if (item.Checked)
...{
return;
}
Rectangle rect = e.ImageRectangle;
if (e.Item.RightToLeft == RightToLeft.Yes)
...{
rect.X -= OffsetMargin + 2;
}
else
...{
rect.X += OffsetMargin + 2;
}
ToolStripItemImageRenderEventArgs ne =
new ToolStripItemImageRenderEventArgs(
e.Graphics, e.Item, e.Image, rect);
base.OnRenderItemImage(ne);
return;
}

base.OnRenderItemImage(e);
}

protected override void OnRenderItemText(
ToolStripItemTextRenderEventArgs e)
...{
e.TextColor = ColorTable.ForeColor;

if (!(e.ToolStrip is MenuStrip) && (e.Item is ToolStripMenuItem))
...{
Rectangle rect = e.TextRectangle;
if (e.Item.RightToLeft == RightToLeft.Yes)
...{
rect.X -= 16;
}
else
...{
rect.X += 16;
}
e.TextRectangle = rect;
}

base.OnRenderItemText(e);
}

最后,把ContextMenuStrip的Renderer设为ToolStripRendererEx就行了。下一篇文章将介绍实现完整的截图控件,并提供完整的源代码下载。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐