using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace WinCustControls
{
public partial class UArrowControl : UserControl
{
public UArrowControl()
{
InitializeComponent();
SetStyle(ControlStyles.AllPaintingInWmPaint, true);//忽略窗口消息,减少闪烁
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);//绘制到缓冲区,减少闪烁
SetStyle(ControlStyles.UserPaint, true);//控件由其自身而不是操作系统绘制
SetStyle(ControlStyles.ResizeRedraw, true);//控件调整其大小时重绘
SetStyle(ControlStyles.SupportsTransparentBackColor, true);//支持透明背景
this.SizeChanged += UArrowControl_SizeChanged;
this.Size = new System.Drawing.Size(100, 50);
}
private Color? borderColor = null;
[Description("箭头的边框颜色,可以为null")]
public Color? BorderColor
{
get { return borderColor; }
set
{
borderColor = value;
Invalidate();
}
}
private Color arrowColor = Color.Red;
[Description("箭头的颜色"), DefaultValue(typeof(Color), "Red")]
public Color ArrowColor
{
get { return arrowColor; }
set
{
arrowColor = value;
Invalidate();
}
}
private ArrowDirection direction = ArrowDirection.Left;
[Description("箭头的方向"), DefaultValue(typeof(ArrowDirection), "Left")]
public ArrowDirection Direction
{
get { return direction; }
set
{
direction = value;
GetPath();
Invalidate();
}
}
///
/// 箭头的路径
///
GraphicsPath path;
private void UArrowControl_SizeChanged(object sender, EventArgs e)
{
GetPath();
}
///
/// 生成箭头路径
///
private void GetPath()
{
Point[] points = null;
switch (direction)
{
case ArrowDirection.Left:
points = new Point[]
{
new Point(0,Height/2),
new Point(Width/5,0),
new Point(Width/5,Height/4),
new Point(Width-1,Height/4),
new Point(Width-1,Height- Height/4),
new Point(Width/5,Height- Height/4),
new Point(Width/5,Height),
new Point(0,Height/2)
};
break;
case ArrowDirection.Right:
points = new Point[]
{
new Point(0,Height/4),
new Point(Width-Width/5,Height/4),
new Point(Width-Width/5,0),
new Point(Width-1,Height/2),
new Point(Width-Width/5,Height),
new Point(Width-Width/5,Height- Height/4),
new Point(0,Height-Height/4),
new Point(0,Height/4)
};
break;
case ArrowDirection.Top:
points = new Point[]
{
new Point(this.Width/2,0),
new Point(this.Width,Height/5),
new Point(this.Width-this.Width/4,Height/5),
new Point(this.Width-this.Width/4,this.Height-1),
new Point(this.Width/4,this.Height-1),
new Point(this.Width/4,Height/5),
new Point(0,Height/5),
new Point(this.Width/2,0),
};
break;
case ArrowDirection.Bottom:
points = new Point[]
{
new Point(this.Width-this.Width/4,0),
new Point(this.Width-this.Width/4,this.Height-Height/5),
new Point(this.Width,this.Height-Height/5),
new Point(this.Width/2,this.Height-1),
new Point(0,this.Height-Height/5),
new Point(this.Width/4,this.Height-Height/5),
new Point(this.Width/4,0),
new Point(this.Width-this.Width/4,0),
};
break;
case ArrowDirection.Left_Right:
points = new Point[]
{
new Point(0,this.Height/2),
new Point(Width/5,0),
new Point(Width/5,this.Height/4),
new Point(this.Width-Width/5,this.Height/4),
new Point(this.Width-Width/5,0),
new Point(this.Width-1,this.Height/2),
new Point(this.Width-Width/5,this.Height),
new Point(this.Width-Width/5,this.Height-this.Height/4),
new Point(Width/5,this.Height-this.Height/4),
new Point(Width/5,this.Height),
new Point(0,this.Height/2),
};
break;
case ArrowDirection.Top_Bottom:
points = new Point[]
{
new Point(this.Width/2,0),
new Point(this.Width,Height/5),
new Point(this.Width-this.Width/4,Height/5),
new Point(this.Width-this.Width/4,this.Height-Height/5),
new Point(this.Width,this.Height-Height/5),
new Point(this.Width/2,this.Height-1),
new Point(0,this.Height-Height/5),
new Point(this.Width/4,this.Height-Height/5),
new Point(this.Width/4,Height/5),
new Point(0,Height/5),
new Point(this.Width/2,0),
};
break;
}
path = new GraphicsPath();
path.AddLines(points);
path.CloseAllFigures();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.FillPath(new SolidBrush(arrowColor), path);//填充箭头路径
//绘制边框
if (borderColor != null && borderColor != Color.Empty)
{
g.DrawPath(new Pen(new SolidBrush(borderColor.Value)), path);
}
}
}
public enum ArrowDirection
{
Left,
Right,
Top,
Bottom,
Left_Right,
Top_Bottom
}
}