Hoje mostro mais uma classe que estende as características do menustrip. Podendo mudar cores, formatos e outras características.

[sections] [section title=”Como mudar as cores do componente menustrip”]

internal class MenuStripExtender : MenuStrip
{
private static Color _newHoverBorderColor = Color.Black;
private static Color _newHoverBackColor = Color.LightSkyBlue;
private static Color _newBackColor = Color.LightBlue;
private static Color _newSecondColor = Color.LightSteelBlue;
private static Color _newThirdColor = Color.WhiteSmoke;

public MenuStripExtender()
{
Renderer = new MyRenderer(); // new ToolStripProfessionalRenderer(new CustomProfessionalColors());
}

[Browsable(true),
Category("New Color"),
Description("Border color in hover.")] public Color NewHoverBorderColor
{
get { return _newHoverBorderColor; }
set { _newHoverBorderColor = value; }
}

[Browsable(true),
Category("New Color"),
Description("Backcolor in hover.")] public Color NewHoverBackColor
{
get { return _newHoverBackColor; }
set { _newHoverBackColor = value; }
}

[Browsable(true),
Category("New Color"),
Description("Backcolor.")] public Color NewBackColor
{
get { return _newBackColor; }
set { _newBackColor = value; }
}

[Browsable(true),
Category("New Color"),
Description("Auxiliary color.")] public Color NewSecondColor
{
get { return _newSecondColor; }
set { _newSecondColor = value; }
}

[Browsable(true),
Category("New Color"),
Description("Auxiliary color 2.")] public Color NewThirdColor
{
get { return _newThirdColor; }
set { _newThirdColor = value; }
}

//Add this to the bottom of your form. (After your class ends)
internal class CustomProfessionalColors : ProfessionalColorTable
{
public override Color MenuBorder
{
get { return _newHoverBorderColor; }
}

public override Color MenuStripGradientBegin
{
get { return _newBackColor; }
}

public override Color MenuStripGradientEnd
{
get { return _newBackColor; }
}

public override Color MenuItemPressedGradientBegin
{
get { return _newBackColor; }
}

public override Color MenuItemPressedGradientMiddle
{
get { return _newBackColor; }
}

public override Color MenuItemPressedGradientEnd
{
get { return _newBackColor; }
}

public override Color ImageMarginGradientBegin
{
get { return _newSecondColor; }
}

public override Color ImageMarginGradientMiddle
{
get { return _newThirdColor; }
}

public override Color ImageMarginGradientEnd
{
get { return _newSecondColor; }
}

public override Color ToolStripDropDownBackground
{
get { return _newBackColor; }
}

public override Color ToolStripGradientBegin
{
get { return _newBackColor; }
}

public override Color ToolStripGradientMiddle
{
get { return _newBackColor; }
}

public override Color ToolStripGradientEnd
{
get { return _newBackColor; }
}
}

internal class MyRenderer : ToolStripProfessionalRenderer
{
public MyRenderer()
: base(new CustomProfessionalColors())
{
}

protected override void OnRenderArrow(ToolStripArrowRenderEventArgs e)
{
e.ArrowColor = e.Item.ForeColor;
base.OnRenderArrow(e);
}

/* protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
{
base.OnRenderItemText(e);
if (e.Item.GetType() == typeof(ToolStripMenuItem))
{
var tsmi = (ToolStripMenuItem)e.Item;
if (tsmi.HasDropDownItems && tsmi.OwnerItem != null)
{
Rectangle bounds = tsmi.Bounds;
bounds.X = bounds.Right – 25;
bounds.Width = 25;
bounds.Y = 0;
var tsarea = new ToolStripArrowRenderEventArgs(
e.Graphics,
tsmi,
bounds,
tsmi.ForeColor,
ArrowDirection.Right);
OnRenderArrow(tsarea);
}
}
}*/

protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
{
var rc = new Rectangle(Point.Empty, e.Item.Size);
e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

Color c = e.Item.Selected ? _newHoverBackColor : Color.Transparent;
Color cborder = e.Item.Selected ? _newHoverBorderColor : Color.Transparent;
FillRoundRect(e.Graphics, new SolidBrush(c), rc.X, rc.Y, rc.Width, rc.Height, 1);
DrawRoundRect(e.Graphics, new Pen(cborder), rc.X, rc.Y, rc.Width – 1, rc.Height – 1, 1);
}

public void DrawRoundRect(Graphics g, Pen p, float x, float y, float width, float height, float radius)
{
var gp = new GraphicsPath();
gp.AddLine(x + radius, y, x + width – (radius*2), y);
gp.AddArc(x + width – (radius*2), y, radius*2, radius*2, 270, 90);
gp.AddLine(x + width, y + radius, x + width, y + height – (radius*2));
gp.AddArc(x + width – (radius*2), y + height – (radius*2), radius*2, radius*2, 0, 90);
gp.AddLine(x + width – (radius*2), y + height, x + radius, y + height);
gp.AddArc(x, y + height – (radius*2), radius*2, radius*2, 90, 90);
gp.AddLine(x, y + height – (radius*2), x, y + radius);
gp.AddArc(x, y, radius*2, radius*2, 180, 90);
gp.CloseFigure();
g.DrawPath(p, gp);
gp.Dispose();
}

public void FillRoundRect(Graphics g, Brush p, float x, float y, float width, float height, float radius)
{
var gp = new GraphicsPath();
gp.AddLine(x + radius, y, x + width – (radius*2), y);
gp.AddArc(x + width – (radius*2), y, radius*2, radius*2, 270, 90);
gp.AddLine(x + width, y + radius, x + width, y + height – (radius*2));
gp.AddArc(x + width – (radius*2), y + height – (radius*2), radius*2, radius*2, 0, 90);
gp.AddLine(x + width – (radius*2), y + height, x + radius, y + height);
gp.AddArc(x, y + height – (radius*2), radius*2, radius*2, 90, 90);
gp.AddLine(x, y + height – (radius*2), x, y + radius);
gp.AddArc(x, y, radius*2, radius*2, 180, 90);
gp.CloseFigure();
g.FillPath(p, gp);
gp.Dispose();
}
}
}

[/section] [/sections]

 


Obrigado e até o próximo post.

DATEK Tecnologia Eletrônica
[C#] Como mudar as cores do componente menustrip
Tags:                     

Deixe uma resposta

%d blogueiros gostam disto: