ToolStripSplitButton 系统默认没有Checked效果,MS又说他们没时间改。只能自己处理一下了。
重载OnPaint(PaintEventArgs e)
if(Checked)
Rectangle rect = ButtonBounds;
using (Brush br = new LinearGradientBrush(rect, ProfessionalColors.ButtonCheckedGradientBegin,ProfessionalColors.ButtonCheckedGradientEnd,LinearGradientMode.Vertical))
{e.Graphics.FillRectangle(br, rect);}
rect.Inflate(-1, -1);
e.Graphics.DrawRectangle(new System.Drawing.Pen(ProfessionalColors.ButtonSelectedHighlightBorder), rect);
base.OnPaint(e);
ToolStripSplitButton 的button 部分没有Checked propery,下列方法简单的解决这个问题:在button 的checkd 时,额外绘制一些图形,效果如下:
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
namespace SplitButton
{
public class ToolStripSplitButtonEx : ToolStripSplitButton
{
private bool @checked = false;
public ToolStripSplitButtonEx()
{
}
public bool Checked
{
get { return @checked; }
set
{
@checked = value;
Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
if (@checked)
{
Rectangle rect = ButtonBounds;
using (Brush br = new LinearGradientBrush(rect,ProfessionalColors.ButtonCheckedGradientBegin,ProfessionalColors.ButtonCheckedGradientEnd,LinearGradientMode.Vertical))
{
e.Graphics.FillRectangle(br, rect);
}
rect.Inflate(-1, -1);
e.Graphics.DrawRectangle(Pens.Black, rect);
}
base.OnPaint(e);
}
}
}
联系客服