using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Lskj.Main { /// /// 自定义MenuStrip样式 /// public class CustomProfessionalRenderer : ToolStripProfessionalRenderer { private Color _color; public CustomProfessionalRenderer():base() { } public CustomProfessionalRenderer(Color color):base() { _color = color; } /// /// 获取圆角矩形区域 /// /// /// /// public static GraphicsPath GetRoundedRectPath(Rectangle rect, int radius) { int diameter = radius; Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter)); GraphicsPath path = new GraphicsPath(); // 左上角 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; } /// /// 渲染背景 /// /// protected override void OnRenderToolStripBackground(ToolStripRenderEventArgs e) { ToolStrip toolStrip = e.ToolStrip; Graphics g = e.Graphics; g.SmoothingMode = SmoothingMode.HighQuality;//抗锯齿 Rectangle bounds = e.AffectedBounds; LinearGradientBrush lgbrush = new LinearGradientBrush(new Point(0, 0), new Point(0, toolStrip.Height), Color.FromArgb(255, Color.White), Color.FromArgb(150, _color)); if (toolStrip is MenuStrip) { //由menuStrip的Paint方法定义 这里不做操作 } else if (toolStrip is ToolStripDropDown) { int diameter = 10;//直径 GraphicsPath path = new GraphicsPath(); Rectangle rect = new Rectangle(Point.Empty, toolStrip.Size); Rectangle arcRect = new Rectangle(rect.Location, new Size(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 = new Region(path); g.FillPath(lgbrush, path); } else { base.OnRenderToolStripBackground(e); } } /// /// 渲染边框 不绘制边框 /// /// protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e) { //不调用基类的方法 屏蔽掉该方法 去掉边框 } /// /// 渲染箭头颜色 /// /// protected override void OnRenderArrow(ToolStripArrowRenderEventArgs e) { e.ArrowColor = _color; base.OnRenderArrow(e); } /// /// 渲染菜单项 /// /// protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e) { Graphics g = e.Graphics; ToolStripItem item = e.Item; ToolStrip toolstrip = e.ToolStrip; //渲染顶级项 if (toolstrip is MenuStrip) { LinearGradientBrush lgbrush = new LinearGradientBrush(new Point(0, 0), new Point(0, item.Height), Color.FromArgb(100, Color.White), Color.FromArgb(0, Color.White)); SolidBrush brush = new SolidBrush(Color.FromArgb(255, Color.White)); if (e.Item.Selected) { GraphicsPath gp = GetRoundedRectPath(new Rectangle(new Point(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(new Point(rect.Width, rect.Height), new Point(0, rect.Height)); //path.CloseFigure(); ////填充路径 //g.FillPath(brush, path); g.FillRectangle(Brushes.White, new Rectangle(Point.Empty, item.Size)); } } //渲染下拉项 else if (toolstrip is ToolStripDropDown) { g.SmoothingMode = SmoothingMode.HighQuality; LinearGradientBrush lgbrush = new LinearGradientBrush(new Point(0, 0), new Point(item.Width, 0), Color.FromArgb(200, _color), Color.FromArgb(0, Color.White)); if (item.Selected) { GraphicsPath gp = GetRoundedRectPath(new Rectangle(0, 0, item.Width, item.Height), 10); g.FillPath(lgbrush, gp); } } else { base.OnRenderMenuItemBackground(e); } } /// /// 渲染分割线 /// /// protected override void OnRenderSeparator(ToolStripSeparatorRenderEventArgs e) { Graphics g = e.Graphics; LinearGradientBrush lgbrush = new LinearGradientBrush(new Point(0, 0), new Point(e.Item.Width, 0), _color, Color.FromArgb(0, _color)); g.FillRectangle(lgbrush, new Rectangle(3, e.Item.Height / 2, e.Item.Width, 1)); //base.OnRenderSeparator(e); } /// /// 渲染下拉菜单的左边图片区域 /// /// protected override void OnRenderImageMargin(ToolStripRenderEventArgs e) { //base.OnRenderImageMargin(e); //屏蔽掉左边图片竖条 } } }