Winform MenuStrip 更改選擇背景色
阿新 • • 發佈:2018-12-21
一、自定義MenuStrip render
專案時,覺得自帶選單項選中顏色與專案不匹配,想要更改,多方查詢總結出方法。
初始化時 將this.menustrip1.render=new myselfrender();設定為自己重寫的render類。
public class CustomProfessionalRenderer:ToolStripProfessionalRenderer { Font textFont = new System.Drawing.Font("微軟雅黑", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); private Color menuItemSelectedColor = Color.Gray; private Color menuItemBorderColor = Color.Black; /// <summary> /// Initialize a new instance of the Visual Studio MenuBarRenderer class. /// </summary> public CustomProfessionalRenderer() : base(new MenuBarColor()) { this.menuItemSelectedColor = Color.LightSteelBlue; this.menuItemBorderColor = Color.LightSteelBlue; } protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e) { e.TextFont = textFont; base.OnRenderItemText(e); } #region Backgrounds protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e) { if (e.Item.IsOnDropDown) { if (e.Item.Selected == true && e.Item.Enabled) { DrawMenuDropDownItemHighlight(e); } } else { base.OnRenderMenuItemBackground(e); } } 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(200, Color.FromArgb(48,61,69)), Color.FromArgb(200, Color.FromArgb(48, 61, 69))); 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 OnRenderImageMargin(ToolStripRenderEventArgs e) { // base.OnRenderImageMargin(e); } #endregion #region DrawMenuDropDownItemHighlight private void DrawMenuDropDownItemHighlight(ToolStripItemRenderEventArgs e) { Rectangle rect = new Rectangle(); rect = new Rectangle(2, 0, (int)e.Graphics.VisibleClipBounds.Width - 4, (int)e.Graphics.VisibleClipBounds.Height - 1); using (SolidBrush brush = new SolidBrush(menuItemSelectedColor)) { e.Graphics.FillRectangle(brush, rect); } using (Pen pen = new Pen(menuItemBorderColor)) { e.Graphics.DrawRectangle(pen, rect); } } #endregion } public class MenuBarColor : ProfessionalColorTable { Color ManuBarCommonColor = Color.LightSteelBlue; Color SelectedHighlightColor = Color.FromArgb(253, 244, 191); Color MenuBorderColor = Color.White; Color MenuItemBorderColor = Color.FromArgb(48,61,69); Color MenuStripbg = Color.Transparent; /// <summary> /// Initialize a new instance of the Visual Studio MenuBarColor class. /// </summary> public MenuBarColor() { } #region ///// <summary> ///// 獲取選中除頂級 System.Windows.Forms.ToolStripMenuItem 之外的 System.Windows.Forms.ToolStripMenuItem 時,要使用的純色。 ///// </summary> //public override Color MenuItemSelected //{ // get // { // return SelectedHighlightColor; // } //} /// <summary> /// 被按下時使用的漸變的開始顏色 /// </summary> public override Color MenuItemPressedGradientBegin { get { return MenuStripbg; } } /// <summary> /// 被按下時使用的漸變的結束顏色 /// </summary> public override Color MenuItemPressedGradientEnd { get { return MenuStripbg; } } /// <summary> /// 被按下時使用的漸變的中間顏色 /// </summary> public override Color MenuItemPressedGradientMiddle { get { return MenuStripbg; } } /// <summary> /// 獲取按鈕被選定時使用的純色 /// </summary> public override Color ButtonSelectedHighlight { get { return ManuBarCommonColor; } } /// <summary> /// 被選定時使用的漸變的開始顏色 /// </summary> public override Color MenuItemSelectedGradientBegin { get { return ManuBarCommonColor; } } /// <summary> /// 被選定時使用的漸變的結束顏色 /// </summary> public override Color MenuItemSelectedGradientEnd { get { return ManuBarCommonColor; } } /// <summary> /// MenuStrip 上使用的邊框顏色 /// </summary> public override Color MenuBorder { get { return MenuBorderColor; } } /// <summary> /// ToolStripMenuItem 的邊框顏色 /// </summary> public override Color MenuItemBorder { get { return MenuItemBorderColor; } } #endregion }