C#MeunStrip重绘
效果:
using System;
usingSystem.Collections.Generic;
using System.Linq;
using System.Text;
usingSystem.Windows.Forms;
using System.Drawing;
usingSystem.Drawing.Drawing2D;
namespace测试001
{
publicpartialclassCustomControls_MenuStrip : MenuStrip
{
privateColor _themeColor = Color.Gray;
publicCustomControls_MenuStrip()
{
//InitializeComponent();
this.Renderer = newCustomProfessionalRenderer(_themeColor);
}
publicColor ThemeColor
{
get { return _themeColor; }
set
{
_themeColor = value;
this.Renderer = newCustomProfessionalRenderer(_themeColor);
}
}
}
publicclassCustomProfessionalRenderer : ToolStripProfessionalRenderer
{
privateColor _color = Color.Red;
publicCustomProfessionalRenderer()
: base()
{
}
publicCustomProfessionalRenderer(Color color)
: base()
{
_color = color;
}
///<summary>
///获取圆角矩形区域
///</summary>
///<paramname="rect">当前控件矩形区</param>
///<paramname="radius">直径</param>
///<returns>圆角矩形区</returns>
publicstaticGraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
{
int diameter = radius;
Rectangle arcRect = newRectangle(rect.Location, newSize(diameter,diameter));
GraphicsPath path = newGraphicsPath();
// 左上角
path.AddArc(arcRect, 180, 90);
// 右上角
arcRect.X = rect.Right - diameter;
path.AddArc(arcRect, 270, 90);
// 右下角
arcRect.Y = rect.Bottom - diameter;
path.AddArc(arcRect, 0, 90);
// 左下角
arcRect.X = rect.Left;
path.AddArc(arcRect, 90, 90);
path.CloseFigure();
return path;
}
///<summary>
///为实现自定义界面 重写 System.Windows.Forms.ToolStripRenderer.OnRenderToolStripBackground
///渲染背景 包括menustrip背景 toolstripDropDown背景
///</summary>
///<paramname="e">为 System.Windows.Forms.ToolStripRenderer.OnRenderToolStripBackground 提供方法数据</param>
protectedoverridevoidOnRenderToolStripBackground(ToolStripRenderEventArgs e)
{
ToolStrip toolStrip =e.ToolStrip;
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.HighQuality;//抗锯齿
Rectangle bounds =e.AffectedBounds;
LinearGradientBrush lgbrush = newLinearGradientBrush(newPoint(0, 0), newPoint(0,toolStrip.Height), Color.FromArgb(255, Color.White), Color.FromArgb(150,_color));
if (toolStrip isMenuStrip)
{
//由menuStrip的Paint方法定义 这里不做操作
}
elseif (toolStrip isToolStripDropDown)
{
int diameter = 10;//直径
GraphicsPath path = newGraphicsPath();
Rectangle rect = newRectangle(Point.Empty,toolStrip.Size);
Rectangle arcRect = newRectangle(rect.Location, newSize(diameter,diameter));
path.AddLine(0, 0, 10, 0);
// 右上角
arcRect.X = rect.Right -diameter;
path.AddArc(arcRect, 270, 90);
// 右下角
arcRect.Y = rect.Bottom -diameter;
path.AddArc(arcRect, 0, 90);
// 左下角
arcRect.X = rect.Left;
path.AddArc(arcRect, 90, 90);
path.CloseFigure();
toolStrip.Region = newRegion(path);
g.FillPath(lgbrush, path);
}
else
{
base.OnRenderToolStripBackground(e);
}
}
///<summary>
///渲染边框
///不绘制边框
///</summary>
///<paramname="e">为 System.Windows.Forms.ToolStripRenderer.OnRenderToolStripBorder 提供方法数据</param>
protectedoverridevoidOnRenderToolStripBorder(ToolStripRenderEventArgs e)
{
///不调用该方法
//base.OnRenderToolStripBorder(e);
}
///<summary>
///渲染箭头
///</summary>
///<paramname="e">为 System.Windows.Forms.ToolStripRenderer.OnRenderArrow 提供方法数据</param>
protectedoverridevoid OnRenderArrow(ToolStripArrowRenderEventArgs e)
{
e.ArrowColor = _color;
base.OnRenderArrow(e);
}
///<summary>
///渲染菜单项
///</summary>
///<paramname="e">为 System.Windows.Forms.ToolStripRenderer.OnRenderMenuItemBackground 提供方法数据</param>
protectedoverridevoidOnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
{
Graphics g = e.Graphics;
ToolStripItem item = e.Item;
ToolStrip toolstrip =e.ToolStrip;
//渲染顶级项
if (toolstrip isMenuStrip)
{
LinearGradientBrush lgbrush = newLinearGradientBrush(newPoint(0, 0), newPoint(0, item.Height), Color.FromArgb(100, Color.White), Color.FromArgb(0, Color.White));
SolidBrush brush = newSolidBrush(Color.FromArgb(255, Color.White));
if (e.Item.Selected)
{
GraphicsPath gp =GetRoundedRectPath(newRectangle(newPoint(0, 0), item.Size),5);
g.FillPath(lgbrush, gp);
}
if (item.Pressed)
{
////创建上面左右2圆角的矩形路径
//GraphicsPath path= new GraphicsPath();
//int diameter = 8;
//Rectangle rect =new Rectangle(Point.Empty, item.Size);
//Rectangle arcRect= new Rectangle(rect.Location, new Size(diameter, diameter));
//// 左上角
//path.AddArc(arcRect,180, 90);
//// 右上角
//arcRect.X =rect.Right - diameter;
//path.AddArc(arcRect,270, 90);
//path.AddLine(newPoint(rect.Width, rect.Height), new Point(0, rect.Height));
//path.CloseFigure();
////填充路径
//g.FillPath(brush,path);
g.FillRectangle(Brushes.White, newRectangle(Point.Empty, item.Size));
}
}
//渲染下拉项
elseif (toolstrip isToolStripDropDown)
{
g.SmoothingMode = SmoothingMode.HighQuality;
LinearGradientBrush lgbrush = newLinearGradientBrush(newPoint(0, 0), newPoint(item.Width, 0), Color.FromArgb(200,_color), Color.FromArgb(0, Color.White));
if (item.Selected)
{
GraphicsPath gp =GetRoundedRectPath(newRectangle(0, 0, item.Width,item.Height), 10);
g.FillPath(lgbrush, gp);
}
}
else
{
base.OnRenderMenuItemBackground(e);
}
}
///<summary>
///渲染分界线
///</summary>
///<paramname="e"></param>
protectedoverridevoid OnRenderSeparator(ToolStripSeparatorRenderEventArgs e)
{
Graphics g = e.Graphics;
LinearGradientBrush lgbrush = newLinearGradientBrush(newPoint(0, 0), newPoint(e.Item.Width, 0),_color, Color.FromArgb(0,_color));
g.FillRectangle(lgbrush, newRectangle(3, e.Item.Height /2, e.Item.Width, 1));
//base.OnRenderSeparator(e);
}
protectedoverridevoidOnRenderImageMargin(ToolStripRenderEventArgs e)
{
//base.OnRenderImageMargin(e);
}
}
}