C#/VS控件
发布网友
发布时间:2022-05-18 21:21
我来回答
共2个回答
热心网友
时间:2023-11-05 06:10
需要自定义菜单,继承MenuStrip
public partial class CustomContrls_MenuStrip : MenuStrip
{
private Color _themeColor = Color.Red;
public CustomContrls_MenuStrip()
{
//InitializeComponent();
this.Renderer = new CustomProfessionalRenderer(_themeColor);
}
public Color ThemeColor
{
get { return _themeColor; }
set
{
_themeColor = value;
this.Renderer = new CustomProfessionalRenderer(_themeColor);
}
}
}
public class CustomProfessionalRenderer : ToolStripProfessionalRenderer
//给这个类添加主题颜色的字段 重载其构造函数给字段赋值 以便创建不同色调的渲染对象
{
private Color _color = Color.Red;
public CustomProfessionalRenderer()
: base()
{
}
public CustomProfessionalRenderer(Color color)
: base()
{
_color = color;
}
//添加一个辅助函数 用来获取圆角矩形区域
//获取圆角矩形区域 radius=直径
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;
}
//然后重写基类的一些方法 更改外观 渲染背景
//渲染背景 包括menustrip背景 toolstripDropDown背景
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);
//屏蔽掉左边图片竖条
}
}
热心网友
时间:2023-11-05 06:10
对于位置的设置你放在哪里他就显示在哪里,背景颜色的话,你可以调用picturebox的背景颜色赋值给Menustrip的背景颜色,不就可以了啊追问为什么我把picturebox弄到最大后再拖Menustrip到界面上,Menustrip显示在picturebox后面啊
追答因为picturebox把前面的部分给填满了,所以就显示在后面了,你可以现放Menustrip
在放picturebox