给大家分享一个winform分割按钮,对Button进行重写,废话不多说,直接上代码
public partial class SpliteButtonEx : System.Windows.Forms.Button
{
private Color _enabledColor = System.Drawing.Color.Gainsboro;
private Color _borderColor = System.Drawing.Color.FromArgb(235, 235, 235);
private Color _innerBorderColor = System.Drawing.Color.Transparent;
private Color _baseColor = System.Drawing.Color.FromArgb(227, 227, 227);
private Color _baseColorEnd = System.Drawing.Color.FromArgb(227, 227, 227);
private Color _arrowColor = System.Drawing.Color.FromArgb(51, 51, 51);
private int _imageWidth = 24;
private int _imageHeight = 24;
private ButtonRoundStyle _roundStyle = ButtonRoundStyle.All;
private int _radius = 8;
private int _imageTextSpace = 2;
private bool _pressOffset = false;
private bool _alwaysShowBorder = true;
private bool _showSpliteButton = false;
private int _spliteButtonWidth = 18;
private bool _spliteButtonStatus = false;
private ButtonControlState _controlState;
private ButtonMousePosition _mousePosition;
private Image _MouseHoverBackImage;
private Image _MouseDownBackImage;
private Image _MouseLeaveBackImage;
private Image _MouseHoverImage;
private Image _MouseDownImage;
private Image _MouseLeaveImage;
private Color _MouseHoverForeColor = System.Drawing.Color.FromArgb(51, 51, 51);
private Color _MouseDownForeColor = System.Drawing.Color.FromArgb(51, 51, 51);
private Color _MouseLeaveForeColor = System.Drawing.Color.FromArgb(51, 51, 51);
private bool _contextHandle;
private bool _contextOpened;
private int _contextOffset = 5;
private string itemCodeKey = string.Empty;
/// <summary>
/// 普通按钮按下事件
/// </summary>
public event EventHandler OnButtonClick;
/// <summary>
/// 分割按钮按下事件
/// </summary>
public event EventHandler OnSpliteButtonClick;
/// <summary>
/// 鼠标的当前位置
/// </summary>
public enum ButtonMousePosition
{
None,
Button,
Splitebutton,
}
public SpliteButtonEx()
: base()
{
SetStyle(
ControlStyles.SupportsTransparentBackColor |
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.ResizeRedraw |
ControlStyles.SupportsTransparentBackColor, true);
SetStyle(ControlStyles.Opaque, false);
this.BackColor = Color.Transparent;
this.Font = new System.Drawing.Font("微软雅黑", 10F);
this.ForeColor = System.Drawing.Color.FromArgb(51, 51, 51);
}
[Browsable(true)]
[Description("权限编码")]
public string ItemCodeKey
{
get { return itemCodeKey; }
set
{
itemCodeKey = value;
if (this.DesignMode)
return;
if (this.itemCodeKey == string.Empty)
return;
//if (QuestionBank.Logic.LoginInfo.ListPurviewItem.Contains<string>(this.itemCodeKey))
//{
// this.Enabled = true;
//}
//else
//{
// this.Enabled = false;
//}
}
}
[DefaultValue(typeof(Color), "51,51,51")]
[Description("鼠标经过时前景颜色")]
public Color MouseHoverForeColor
{
get { return _MouseHoverForeColor; }
set
{
if (_MouseHoverForeColor != value)
{
_MouseHoverForeColor = value;
}
}
}
[DefaultValue(typeof(Color), "51,51,51")]
[Description("鼠标按下时前景颜色")]
public Color MouseDownForeColor
{
get { return _MouseDownForeColor; }
set
{
if (_MouseDownForeColor != value)
{
_MouseDownForeColor = value;
}
}
}
[DefaultValue(typeof(Color), "51,51,51")]
[Description("鼠标离开时前景颜色")]
public Color MouseLeaveForeColor
{
get { return _MouseLeaveForeColor; }
set
{
if (_MouseLeaveForeColor != value)
{
_MouseLeaveForeColor = value;
}
}
}
[Description("鼠标经过和按下时按钮的图片")]
[DefaultValue(null)]
public Image MouseHoverImage
{
get { return _MouseHoverImage; }
set { _MouseHoverImage = (System.Drawing.Image)value; }
}
[Description("鼠标经过和按下时按钮的图片")]
[DefaultValue(null)]
public Image MouseDownImage
{
get { return _MouseDownImage; }
set { _MouseDownImage = (System.Drawing.Image)value; }
}
[Description("鼠标离开时按钮的图片")]
[DefaultValue(null)]
public Image MouseLeaveImage
{
get { return _MouseLeaveImage; }
set { _MouseLeaveImage = (System.Drawing.Image)value; }
}
[Description("鼠标经过和按下时按钮的背景图片")]
[DefaultValue(null)]
public Image MouseHoverBackImage
{
get { return _MouseHoverBackImage; }
set { _MouseHoverBackImage = (System.Drawing.Image)value; }
}
[Description("鼠标经过和按下时按钮的背景图片")]
[DefaultValue(null)]
public Image MouseDownBackImage
{
get { return _MouseDownBackImage; }
set { _MouseDownBackImage = (System.Drawing.Image)value; }
}
[Description("鼠标离开时按钮的背景图片")]
[DefaultValue(null)]
public Image MouseLeaveBackImage
{
get { return _MouseLeaveBackImage; }
set { _MouseLeaveBackImage = (System.Drawing.Image)value; }
}
[DefaultValue(5)]
[Description("下拉菜单与按钮的距离")]
public int ContextOffset
{
get { return _contextOffset; }
set
{
_contextOffset = value;
}
}
[DefaultValue(false)]
[Description("是否启用分割按钮")]
public bool ShowSpliteButton
{
get { return _showSpliteButton; }
set
{
if (_showSpliteButton != value)
{
_showSpliteButton = value;
base.Invalidate();
}
}
}
[DefaultValue(18)]
[Description("分割按钮的宽度")]
public int SpliteButtonWidth
{
get { return _spliteButtonWidth; }
set
{
if (_spliteButtonWidth != value)
{
_spliteButtonWidth = value;
base.Invalidate();
}
}
}
[DefaultValue(18)]
[Description("分割按钮的状态")]
public bool SpliteButtonStatus
{
get { return _spliteButtonStatus; }
set
{
if (_spliteButtonStatus != value)
{
_spliteButtonStatus = value;
base.Invalidate();
}
}
}
[DefaultValue(true)]
[Description("当鼠标按下时图片和文字是否产生偏移")]
public bool PressOffset
{
get { return _pressOffset; }
set
{
_pressOffset = value;
}
}
[DefaultValue(true)]
[Description("是否一直显示按钮边框\n设置为false则只在鼠标经过和按下时显示边框")]
public bool AlwaysShowBorder
{
get { return _alwaysShowBorder; }
set
{
if (_alwaysShowBorder != value)
{
_alwaysShowBorder = value;
base.Invalidate();
}
}
}
[DefaultValue(typeof(Color), "51,51,51")]
[Description("当显示分割按钮时,分割按钮的箭头颜色")]
public Color ArrowColor
{
get { return _arrowColor; }
set
{
if (_arrowColor != value)
{
_arrowColor = value;
base.Invalidate();
}
}
}
[DefaultValue(typeof(Color), "235, 235, 235")]
[Description("按钮的边框颜色")]
public Color BorderColor
{
get { return _borderColor; }
set
{
if (_borderColor != value)
{
_borderColor = value;
base.Invalidate();
}
}
}
[DefaultValue(typeof(Color), "213, 213, 213")]
[Description("按钮内边框颜色")]
public Color InnerBorderColor
{
get { return _innerBorderColor; }
set
{
if (_innerBorderColor != value)
{
_innerBorderColor = value;
base.Invalidate();
}
}
}
[DefaultValue(typeof(Color), "227 ,227 ,227")]
[Description("鼠标经过和按下时按钮的渐变背景颜色")]
public Color BaseColor
{
get { return _baseColor; }
set
{
if (_baseColor != value)
{
_baseColor = value;
}
}
}
[DefaultValue(typeof(Color), "227 ,227, 227")]
[Description("鼠标经过和按下时按钮的渐变背景颜色")]
public Color BaseColorEnd
{
get { return _baseColorEnd; }
set
{
if (_baseColorEnd != value)
{
_baseColorEnd = value;
}
}
}
[DefaultValue(24)]
[Description("图片宽度")]
public int ImageWidth
{
get { return _imageWidth; }
set
{
if (value != _imageWidth)
{
_imageWidth = value < 11 ? 11 : value;
base.Invalidate();
}
}
}
[DefaultValue(24)]
[Description("图片高度")]
public int ImageHeight
{
get { return _imageHeight; }
set
{
if (value != _imageHeight)
{
_imageHeight = value < 11 ? 11 : value;
base.Invalidate();
}
}
}
[DefaultValue(typeof(ButtonRoundStyle), "1")]
[Description("按钮圆角样式")]
public ButtonRoundStyle RoundStyle
{
get { return _roundStyle; }
set
{
if (_roundStyle != value)
{
_roundStyle = value;
base.Invalidate();
}
}
}
[DefaultValue(2)]
[Description("按钮圆角弧度")]
public int Radius
{
get { return _radius; }
set
{
if (_radius != value)
{
_radius = value < 2 ? 2 : value;
base.Invalidate();
}
}
}
[DefaultValue(2)]
[Description("图片与文字之间的间距")]
public int ImageTextSpace
{
get { return _imageTextSpace; }
set
{
if (_imageTextSpace != value)
{
_imageTextSpace = value < 0 ? 0 : value;
base.Invalidate();
}
}
}
/// <summary>
/// 按钮当前状态
/// </summary>
internal ButtonControlState ControlState
{
get { return _controlState; }
set
{
if (_controlState != value)
{
_controlState = value;
base.Invalidate();
}
}
}
/// <summary>
/// 鼠标当前所在位置
/// </summary>
internal ButtonMousePosition CurrentMousePosition
{
get { return _mousePosition; }
set
{
if (_mousePosition != value)
{
_mousePosition = value;
base.Invalidate();
}
}
}
/// <summary>
/// 普通按钮矩形位置
/// </summary>
internal Rectangle ButtonRect
{
get
{
if (ShowSpliteButton)
{
return new Rectangle(0, 0, ClientRectangle.Width - SpliteButtonWidth, ClientRectangle.Height);
}
else
{
return ClientRectangle;
}
}
}
/// <summary>
/// 分割按钮矩形位置
/// </summary>
internal Rectangle SpliteButtonRect
{
get
{
if (ShowSpliteButton)
{
return new Rectangle(ClientRectangle.Width - SpliteButtonWidth, 0, SpliteButtonWidth, ClientRectangle.Height);
}
else
{
return Rectangle.Empty;
}
}
}
protected override void OnEnabledChanged(EventArgs e)
{
base.OnEnabledChanged(e);
this.ForeColor = this.Enabled ? _MouseLeaveForeColor : _enabledColor;
}
protected override void OnMouseLeave(EventArgs e)
{
if (!_contextOpened)
{
ControlState = ButtonControlState.Normal;
CurrentMousePosition = ButtonMousePosition.None;
}
if (_MouseLeaveBackImage != null)
{
this.BackgroundImage = _MouseLeaveBackImage;
}
if (_MouseLeaveImage != null)
{
this.Image = _MouseLeaveImage;
}
//this.ForeColor = _MouseLeaveForeColor;
this.ForeColor = this.Enabled ? _MouseLeaveForeColor : _enabledColor;
base.OnMouseLeave(e);
}
protected override void OnMouseMove(MouseEventArgs mevent)
{
base.OnMouseMove(mevent);
if (ClientRectangle.Contains(mevent.Location))
{
ControlState = ButtonControlState.Hover;
if (ShowSpliteButton)
{
CurrentMousePosition = ButtonRect.Contains(mevent.Location) ? ButtonMousePosition.Button : ButtonMousePosition.Splitebutton;
}
else
{
CurrentMousePosition = ButtonMousePosition.Button;
}
}
else
{
ControlState = ButtonControlState.Normal;
CurrentMousePosition = ButtonMousePosition.None;
}
}
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
if (_MouseHoverBackImage != null)
{
this.BackgroundImage = _MouseHoverBackImage;
}
if (_MouseHoverImage != null)
{
this.Image = _MouseHoverImage;
}
this.ForeColor = _MouseHoverForeColor;
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == MouseButtons.Left && e.Clicks == 1)
{
ControlState = ButtonControlState.Pressed;
if (ShowSpliteButton)
{
CurrentMousePosition = ButtonRect.Contains(e.Location) ? ButtonMousePosition.Button : ButtonMousePosition.Splitebutton;
}
else
{
CurrentMousePosition = ButtonMousePosition.Button;
}
}
if (_MouseDownBackImage != null)
{
this.BackgroundImage = _MouseDownBackImage;
}
if (_MouseDownImage != null)
{
this.Image = _MouseDownImage;
}
this.ForeColor = _MouseDownForeColor;
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
if (e.Button == MouseButtons.Left && e.Clicks == 1)
{
if (ClientRectangle.Contains(e.Location))
{
ControlState = ButtonControlState.Normal;
if (ShowSpliteButton)
{
CurrentMousePosition = ButtonRect.Contains(e.Location) ? ButtonMousePosition.Button : ButtonMousePosition.Splitebutton;
if (CurrentMousePosition == ButtonMousePosition.Splitebutton)
{
SpliteButtonStatus = !SpliteButtonStatus;
base.Invalidate();
if (OnSpliteButtonClick != null)
{
OnSpliteButtonClick(this, EventArgs.Empty);
}
//if (this.ContextMenuStrip != null)
//{
// if (!_contextHandle)
// {
// _contextHandle = true;
// this.ContextMenuStrip.Opening += new CancelEventHandler(ContextMenuStrip_Opening);
// this.ContextMenuStrip.Closed += new ToolStripDropDownClosedEventHandler(ContextMenuStrip_Closed);
// }
// this.ContextMenuStrip.Opacity = 1.0;
// this.ContextMenuStrip.Show(this, 0, this.Height + ContextOffset);
//}
}
else
{
if (OnButtonClick != null)
{
OnButtonClick(this, EventArgs.Empty);
}
}
}
else
{
CurrentMousePosition = ButtonMousePosition.Button;
}
}
else
{
ControlState = ButtonControlState.Normal;
CurrentMousePosition = ButtonMousePosition.None;
}
}
}
private void ContextMenuStrip_Closed(object sender, ToolStripDropDownClosedEventArgs e)
{
_contextOpened = false;
ControlState = ButtonControlState.Normal;
CurrentMousePosition = ButtonMousePosition.None;
base.Invalidate();
}
private void ContextMenuStrip_Opening(object sender, CancelEventArgs e)
{
_contextOpened = true;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
base.OnPaintBackground(e);
Graphics g = e.Graphics;
Rectangle imageRect;
Rectangle textRect;
CalculateRect(out imageRect, out textRect, g);
g.SmoothingMode = SmoothingMode.AntiAlias;
//画边框与背景
RenderBackGroundInternal(
g,
ClientRectangle,
RoundStyle,
Radius
);
//画图像
if (Image != null)
{
g.InterpolationMode = InterpolationMode.HighQualityBilinear;
g.DrawImage(
Image,
imageRect,
0,
0,
Image.Width,
Image.Height,
GraphicsUnit.Pixel);
}
//画文字
if (Text != "")
{
TextRenderer.DrawText(
g,
Text,
Font,
textRect,
ForeColor,
GetTextFormatFlags(TextAlign, RightToLeft == RightToLeft.Yes));
}
//画分割按钮
if (ShowSpliteButton)
{
RenderSpliteButton(g, ClientRectangle);
}
}
/// <summary>
/// 获取图像以及文字的位置
/// </summary>
/// <param name="imageRect"></param>
/// <param name="textRect"></param>
private void CalculateRect(out Rectangle imageRect, out Rectangle textRect, Graphics g)
{
imageRect = Rectangle.Empty;
textRect = Rectangle.Empty;
SizeF textSize = g.MeasureString(Text, Font);
if (Image == null)
{
switch (TextAlign)
{
case ContentAlignment.BottomCenter:
textRect = new Rectangle((ButtonRect.Width - (int)textSize.Width) / 2, Height - (int)textSize.Height - 3, (int)textSize.Width, (int)textSize.Height);
break;
case ContentAlignment.BottomLeft:
textRect = new Rectangle(2, Height - (int)textSize.Height - 3, (int)textSize.Width, (int)textSize.Height);
break;
case ContentAlignment.BottomRight:
textRect = new Rectangle(ButtonRect.Width - (int)textSize.Width - 3, Height - (int)textSize.Height - 3, (int)textSize.Width, (int)textSize.Height);
break;
case ContentAlignment.MiddleCenter:
textRect = new Rectangle((ButtonRect.Width - (int)textSize.Width) / 2, (Height - (int)textSize.Height) / 2, (int)textSize.Width, (int)textSize.Height);
break;
case ContentAlignment.MiddleLeft:
textRect = new Rectangle(2, (Height - (int)textSize.Height) / 2, (int)textSize.Width, (int)textSize.Height);
break;
case ContentAlignment.MiddleRight:
textRect = new Rectangle(ButtonRect.Width - (int)textSize.Width - 3, (Height - (int)textSize.Height) / 2, (int)textSize.Width, (int)textSize.Height);
break;
case ContentAlignment.TopCenter:
textRect = new Rectangle((ButtonRect.Width - (int)textSize.Width) / 2, 2, (int)textSize.Width, (int)textSize.Height);
break;
case ContentAlignment.TopLeft:
textRect = new Rectangle(2, 2, (int)textSize.Width, (int)textSize.Height);
break;
case ContentAlignment.TopRight:
textRect = new Rectangle(ButtonRect.Width - (int)textSize.Width - 3, 2, (int)textSize.Width, (int)textSize.Height);
break;
}
if (PressOffset && ControlState == ButtonControlState.Pressed && CurrentMousePosition == ButtonMousePosition.Button)
{
textRect.X += 1;
textRect.Y += 1;
}
if (RightToLeft == RightToLeft.Yes)
{
textRect.X = ButtonRect.Width - textRect.Right;
}
return;
}
if (this.Text == "")
{
switch (ImageAlign)
{
case ContentAlignment.BottomCenter:
imageRect = new Rectangle((ButtonRect.Width - ImageWidth) / 2, Height - ImageHeight - 3, ImageWidth, ImageHeight);
break;
case ContentAlignment.BottomLeft:
imageRect = new Rectangle(2, Height - ImageHeight - 3, ImageWidth, ImageHeight);
break;
case ContentAlignment.BottomRight:
imageRect = new Rectangle(ButtonRect.Width - ImageWidth - 3, Height - ImageHeight - 3, ImageWidth, ImageHeight);
break;
case ContentAlignment.MiddleCenter:
imageRect = new Rectangle((ButtonRect.Width - ImageWidth) / 2, (Height - ImageHeight) / 2, ImageWidth, ImageHeight);
break;
case ContentAlignment.MiddleLeft:
imageRect = new Rectangle(2, (Height - ImageHeight) / 2, ImageWidth, ImageHeight);
break;
case ContentAlignment.MiddleRight:
imageRect = new Rectangle(ButtonRect.Width - ImageWidth - 3, (Height - ImageHeight) / 2, ImageWidth, ImageHeight);
break;
case ContentAlignment.TopCenter:
imageRect = new Rectangle((ButtonRect.Width - ImageWidth) / 2, 2, ImageWidth, ImageHeight);
break;
case ContentAlignment.TopLeft:
imageRect = new Rectangle(2, 2, ImageWidth, ImageHeight);
break;
case ContentAlignment.TopRight:
imageRect = new Rectangle(ButtonRect.Width - ImageWidth - 3, 2, ImageWidth, ImageHeight);
break;
}
if (PressOffset && ControlState == ButtonControlState.Pressed && CurrentMousePosition == ButtonMousePosition.Button)
{
imageRect.X += 1;
imageRect.Y += 1;
}
if (RightToLeft == RightToLeft.Yes)
{
imageRect.X = ButtonRect.Width - imageRect.Right;
}
return;
}
switch (TextImageRelation)
{
case TextImageRelation.Overlay:
imageRect = new Rectangle(
(ButtonRect.Width - ImageWidth - (int)textSize.Width - ImageTextSpace) / 2,
(Height - ImageHeight) / 2,
ImageWidth,
ImageHeight);
textRect = new Rectangle(
imageRect.Right + ImageTextSpace,
(Height - (int)textSize.Height) / 2,
(int)textSize.Width,
(int)textSize.Height);
break;
case TextImageRelation.ImageAboveText:
imageRect = new Rectangle(
(ButtonRect.Width - ImageWidth) / 2,
(Height - ImageHeight - (int)textSize.Height - ImageTextSpace) / 2,
ImageWidth,
ImageHeight);
textRect = new Rectangle(
(ButtonRect.Width - (int)textSize.Width) / 2,
imageRect.Bottom + ImageTextSpace,
(int)textSize.Width,
(int)textSize.Height);
break;
case TextImageRelation.ImageBeforeText:
imageRect = new Rectangle(
(ButtonRect.Width - ImageWidth - (int)textSize.Width - ImageTextSpace) / 2,
(Height - ImageHeight) / 2,
ImageWidth,
ImageHeight);
textRect = new Rectangle(
imageRect.Right + ImageTextSpace,
(Height - (int)textSize.Height) / 2,
(int)textSize.Width,
(int)textSize.Height);
break;
case TextImageRelation.TextAboveImage:
textRect = new Rectangle(
(ButtonRect.Width - (int)textSize.Width) / 2,
(Height - (int)textSize.Height - ImageHeight - ImageTextSpace) / 2,
(int)textSize.Width,
(int)textSize.Height);
imageRect = new Rectangle(
(ButtonRect.Width - ImageWidth) / 2,
textRect.Bottom + ImageTextSpace,
ImageWidth,
ImageHeight);
break;
case TextImageRelation.TextBeforeImage:
textRect = new Rectangle(
(ButtonRect.Width - ImageWidth - (int)textSize.Width - ImageTextSpace) / 2,
(Height - (int)textSize.Height) / 2,
(int)textSize.Width,
(int)textSize.Height);
imageRect = new Rectangle(
textRect.Right + ImageTextSpace,
(Height - ImageHeight) / 2,
ImageWidth,
ImageHeight);
break;
}
if (PressOffset && ControlState == ButtonControlState.Pressed && CurrentMousePosition == ButtonMousePosition.Button)
{
imageRect.X += 1;
imageRect.Y += 1;
textRect.X += 1;
textRect.Y += 1;
}
if (RightToLeft == RightToLeft.Yes)
{
imageRect.X = ButtonRect.Width - imageRect.Right;
textRect.X = ButtonRect.Width - textRect.Right;
}
}
/// <summary>
/// 画边框与背景
/// </summary>
internal void RenderBackGroundInternal(Graphics g, Rectangle rect, ButtonRoundStyle style, int roundWidth)
{
if (ControlState != ButtonControlState.Normal || AlwaysShowBorder)
{
rect.Width--;
rect.Height--;
if (style != ButtonRoundStyle.None)
{
using (GraphicsPath path = ButtonGraphicsPathHelper.CreatePath(rect, roundWidth, style, false))
{
if (ControlState != ButtonControlState.Normal)
{
using (LinearGradientBrush brush = (ControlState == ButtonControlState.Pressed) ? new LinearGradientBrush(rect, BaseColorEnd, BaseColor, LinearGradientMode.ForwardDiagonal) : new LinearGradientBrush(rect, BaseColor, BaseColorEnd, LinearGradientMode.Vertical))
{
if (!ShowSpliteButton)
{
g.FillPath(brush, path);
}
else
{
if (CurrentMousePosition == ButtonMousePosition.Button)
{
using (GraphicsPath buttonpath = ButtonGraphicsPathHelper.CreatePath(ButtonRect, roundWidth, ButtonRoundStyle.Left, true))
{
g.FillPath(brush, buttonpath);
}
}
else
{
using (GraphicsPath splitepath = ButtonGraphicsPathHelper.CreatePath(SpliteButtonRect, roundWidth, ButtonRoundStyle.Right, true))
{
g.FillPath(brush, splitepath);
}
}
}
}
}
using (Pen pen = new Pen(_borderColor))
{
g.DrawPath(pen, path);
}
}
rect.Inflate(-1, -1);
using (GraphicsPath path = ButtonGraphicsPathHelper.CreatePath(rect, roundWidth, style, false))
{
using (Pen pen = new Pen(InnerBorderColor))
{
g.DrawPath(pen, path);
}
}
}
else
{
if (ControlState != ButtonControlState.Normal)
{
using (LinearGradientBrush brush = (ControlState == ButtonControlState.Pressed) ? new LinearGradientBrush(rect, BaseColorEnd, BaseColor, LinearGradientMode.ForwardDiagonal) : new LinearGradientBrush(rect, BaseColor, BaseColorEnd, LinearGradientMode.Vertical))
{
if (!ShowSpliteButton)
{
g.FillRectangle(brush, rect);
}
else
{
if (CurrentMousePosition == ButtonMousePosition.Button)
{
g.FillRectangle(brush, ButtonRect);
}
else
{
g.FillRectangle(brush, SpliteButtonRect);
}
}
}
}
using (Pen pen = new Pen(_borderColor))
{
g.DrawRectangle(pen, rect);
}
rect.Inflate(-1, -1);
using (Pen pen = new Pen(InnerBorderColor))
{
g.DrawRectangle(pen, rect);
}
}
}
}
/// <summary>
/// 画分割按钮
/// </summary>
/// <param name="g"></param>
/// <param name="rect"></param>
internal void RenderSpliteButton(Graphics g, Rectangle rect)
{
Point[] points = new Point[3];
if (SpliteButtonStatus)//分割按钮状态,角形指向的方向向上(点击隐藏打开的内容,点击之后箭头向下)
{
points[0] = new Point((rect.Width - SpliteButtonWidth) + SpliteButtonWidth / 2, rect.Height / 2 - 3);
points[1] = new Point((rect.Width - SpliteButtonWidth) + (SpliteButtonWidth / 2 - 5), rect.Height / 2 + 3);
points[2] = new Point((rect.Width - SpliteButtonWidth) + (SpliteButtonWidth / 2 + 5), rect.Height / 2 + 3);
}
else //三角形指向的方向向下(点击打开隐藏的内容,点击之后箭头向上)
{
points[0] = new Point((rect.Width - SpliteButtonWidth) + (SpliteButtonWidth / 2 - 5), rect.Height / 2 - 3);
points[1] = new Point((rect.Width - SpliteButtonWidth) + (SpliteButtonWidth / 2 + 5), rect.Height / 2 - 3);
points[2] = new Point((rect.Width - SpliteButtonWidth) + SpliteButtonWidth / 2, rect.Height / 2 + 3);
}
if (PressOffset && ControlState == ButtonControlState.Pressed && CurrentMousePosition == ButtonMousePosition.Splitebutton)
{
points[0].X += 1;
points[0].Y += 1;
points[1].X += 1;
points[1].Y += 1;
points[2].X += 1;
points[2].Y += 1;
}
using (SolidBrush brush = new SolidBrush(ArrowColor))
{
g.FillPolygon(brush, points);
}
}
internal static TextFormatFlags GetTextFormatFlags(ContentAlignment alignment, bool rightToleft)
{
TextFormatFlags flags = TextFormatFlags.WordBreak | TextFormatFlags.SingleLine;
if (rightToleft)
{
flags |= TextFormatFlags.RightToLeft | TextFormatFlags.Right;
}
switch (alignment)
{
case ContentAlignment.BottomCenter:
flags |= TextFormatFlags.Bottom | TextFormatFlags.HorizontalCenter;
break;
case ContentAlignment.BottomLeft:
flags |= TextFormatFlags.Bottom | TextFormatFlags.Left;
break;
case ContentAlignment.BottomRight:
flags |= TextFormatFlags.Bottom | TextFormatFlags.Right;
break;
case ContentAlignment.MiddleCenter:
flags |= TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter;
break;
case ContentAlignment.MiddleLeft:
flags |= TextFormatFlags.VerticalCenter | TextFormatFlags.Left;
break;
case ContentAlignment.MiddleRight:
flags |= TextFormatFlags.VerticalCenter | TextFormatFlags.Right;
break;
case ContentAlignment.TopCenter:
flags |= TextFormatFlags.Top | TextFormatFlags.HorizontalCenter;
break;
case ContentAlignment.TopLeft:
flags |= TextFormatFlags.Top | TextFormatFlags.Left;
break;
case ContentAlignment.TopRight:
flags |= TextFormatFlags.Top | TextFormatFlags.Right;
break;
}
return flags;
}
}