123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace monitor_main_windows
- {
- public partial class Arrow : UserControl
- {
- public Arrow()
- {
- InitializeComponent();
- this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
- this.SetStyle(ControlStyles.DoubleBuffer, true);
- this.SetStyle(ControlStyles.ResizeRedraw, true);
- this.SetStyle(ControlStyles.Selectable, true);
- this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
- this.SetStyle(ControlStyles.UserPaint, true);
- this.ForeColor = Color.White;
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
- this.SizeChanged += Arrow_SizeChanged;
- this.Size = new Size(100, 50);
- }
- /// <summary>
- /// The arrow color
- /// </summary>
- private Color arrowColor = Color.FromArgb(255, 77, 59);
- /// <summary>
- /// Gets or sets the color of the arrow.
- /// </summary>
- /// <value>The color of the arrow.</value>
- [Description("箭头颜色"), Category("自定义")]
- public Color ArrowColor
- {
- get { return arrowColor; }
- set
- {
- arrowColor = value;
- Refresh();
- }
- }
- /// <summary>
- /// The border color
- /// </summary>
- private Color? borderColor = null;
- /// <summary>
- /// Gets or sets the color of the border.
- /// </summary>
- /// <value>The color of the border.</value>
- [Description("箭头边框颜色,为空则无边框"), Category("自定义")]
- public Color? BorderColor
- {
- get { return borderColor; }
- set
- {
- borderColor = value;
- Refresh();
- }
- }
- /// <summary>
- /// The direction
- /// </summary>
- private ArrowDirection direction = monitor_main_windows.ArrowDirection.Right;
- /// <summary>
- /// Gets or sets the direction.
- /// </summary>
- /// <value>The direction.</value>
- [Description("箭头方向"), Category("自定义")]
- public ArrowDirection Direction
- {
- get { return direction; }
- set
- {
- direction = value;
- ResetPath();
- Refresh();
- }
- }
- /// <summary>
- /// 获取或设置控件显示的文字的字体。
- /// </summary>
- /// <value>The font.</value>
- /// <PermissionSet>
- /// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
- /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
- /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
- /// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
- /// </PermissionSet>
- public override Font Font
- {
- get
- {
- return base.Font;
- }
- set
- {
- base.Font = value;
- Refresh();
- }
- }
- /// <summary>
- /// 获取或设置控件的前景色。
- /// </summary>
- /// <value>The color of the fore.</value>
- /// <PermissionSet>
- /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
- /// </PermissionSet>
- public override Color ForeColor
- {
- get
- {
- return base.ForeColor;
- }
- set
- {
- base.ForeColor = value;
- Refresh();
- }
- }
- /// <summary>
- /// The text
- /// </summary>
- private string text;
- /// <summary>
- /// Gets or sets the text.
- /// </summary>
- /// <value>The text.</value>
- [Bindable(true)]
- [Browsable(true)]
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
- [EditorBrowsable(EditorBrowsableState.Always)]
- [Localizable(true)]
- [Description("箭头文字"), Category("自定义")]
- public override string Text
- {
- get
- {
- return text;
- }
- set
- {
- text = value;
- Refresh();
- }
- }
- /// <summary>
- /// The m path
- /// </summary>
- GraphicsPath m_path;
- /// <summary>
- /// Initializes a new instance of the <see cref="UCArrow" /> class.
-
- /// <summary>
- /// Handles the SizeChanged event of the UCArrow control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
- void Arrow_SizeChanged(object sender, EventArgs e)
- {
- ResetPath();
- }
- /// <summary>
- /// Resets the path.
- /// </summary>
- private void ResetPath()
- {
- Point[] ps = null;
- int arrow_h = 20;
- switch (direction)
- {
- case ArrowDirection.Left:
- arrow_h = Width / 3+1;
- ps = new Point[]
- {
- new Point(0,this.Height/2),
- new Point(arrow_h,0),
- new Point(arrow_h,this.Height/4),
- new Point(this.Width-1,this.Height/4),
- new Point(this.Width-1,this.Height-this.Height/4),
- new Point(arrow_h,this.Height-this.Height/4),
- new Point(arrow_h,this.Height),
- new Point(0,this.Height/2)
- };
- break;
- case ArrowDirection.Right:
- arrow_h = Width / 3 + 1;
- ps = new Point[]
- {
- new Point(0,this.Height/4),
- new Point(this.Width-arrow_h,this.Height/4),
- new Point(this.Width-arrow_h,0),
- new Point(this.Width-1,this.Height/2),
- new Point(this.Width-arrow_h,this.Height),
- new Point(this.Width-arrow_h,this.Height-this.Height/4),
- new Point(0,this.Height-this.Height/4),
- new Point(0,this.Height/4)
- };
- break;
- case ArrowDirection.Top:
- arrow_h = Height / 3 + 1;
- ps = new Point[]
- {
- new Point(this.Width/2,0),
- new Point(this.Width,arrow_h),
- new Point(this.Width-this.Width/4,arrow_h),
- new Point(this.Width-this.Width/4,this.Height-1),
- new Point(this.Width/4,this.Height-1),
- new Point(this.Width/4,arrow_h),
- new Point(0,arrow_h),
- new Point(this.Width/2,0),
- };
- break;
- case ArrowDirection.Bottom:
- arrow_h = Height / 3 + 1;
- ps = new Point[]
- {
- new Point(this.Width-this.Width/4,0),
- new Point(this.Width-this.Width/4,this.Height-arrow_h),
- new Point(this.Width,this.Height-arrow_h),
- new Point(this.Width/2,this.Height-1),
- new Point(0,this.Height-arrow_h),
- new Point(this.Width/4,this.Height-arrow_h),
- new Point(this.Width/4,0),
- new Point(this.Width-this.Width/4,0),
- };
- break;
- case ArrowDirection.Left_Right:
- ps = new Point[]
- {
- new Point(0,this.Height/2),
- new Point(arrow_h,0),
- new Point(arrow_h,this.Height/4),
- new Point(this.Width-arrow_h,this.Height/4),
- new Point(this.Width-arrow_h,0),
- new Point(this.Width-1,this.Height/2),
- new Point(this.Width-arrow_h,this.Height),
- new Point(this.Width-arrow_h,this.Height-this.Height/4),
- new Point(arrow_h,this.Height-this.Height/4),
- new Point(arrow_h,this.Height),
- new Point(0,this.Height/2),
- };
- break;
- case ArrowDirection.Top_Bottom:
- ps = new Point[]
- {
- new Point(this.Width/2,0),
- new Point(this.Width,arrow_h),
- new Point(this.Width-this.Width/4,arrow_h),
- new Point(this.Width-this.Width/4,this.Height-arrow_h),
- new Point(this.Width,this.Height-arrow_h),
- new Point(this.Width/2,this.Height-1),
- new Point(0,this.Height-arrow_h),
- new Point(this.Width/4,this.Height-arrow_h),
- new Point(this.Width/4,arrow_h),
- new Point(0,arrow_h),
- new Point(this.Width/2,0),
- };
- break;
- case ArrowDirection.TopRight:
- arrow_h = Math.Min(Width / 5, Height / 3);
- ps = new Point[]
- {
- new Point(Width/2+5*arrow_h/2,Height/2-arrow_h/2),
- new Point(Width/2+3*arrow_h/2,Height/2-3*arrow_h/2),
- new Point(Width/2+3*arrow_h/2,Height/2-arrow_h),
- new Point(Width/2+arrow_h/2,Height/2-arrow_h),
- new Point(Width/2+arrow_h/2,Height/2),
- new Point(Width/2+3*arrow_h/2,Height/2),
- new Point(Width/2+3*arrow_h/2,Height/2+arrow_h/2),
- new Point(Width/2+5*arrow_h/2,Height/2-arrow_h/2)
- };
- break;
- case ArrowDirection.TopLeft:
- arrow_h = Math.Min(Width / 5, Height / 3);
- ps = new Point[]
- {
- new Point(Width/2-5*arrow_h/2,Height/2-arrow_h/2),
- new Point(Width/2-3*arrow_h/2,Height/2-3*arrow_h/2),
- new Point(Width/2-3*arrow_h/2,Height/2-arrow_h),
- new Point(Width/2-arrow_h/2+1,Height/2-arrow_h),
- new Point(Width/2-arrow_h/2+1,Height/2),
- new Point(Width/2-3*arrow_h/2,Height/2),
- new Point(Width/2-3*arrow_h/2,Height/2+arrow_h/2),
- new Point(Width/2-5*arrow_h/2,Height/2-arrow_h/2)
- };
- break;
- case ArrowDirection.LeftTop:
- arrow_h = Math.Min(Width /3, Height /5);
- ps = new Point[]
- {
- new Point(Width/2-arrow_h/2,Height/2-5*arrow_h/2),
- new Point(Width/2-3*arrow_h/2,Height/2-3*arrow_h/2),
- new Point(Width/2-arrow_h,Height/2-3*arrow_h/2),
- new Point(Width/2-arrow_h,Height/2-arrow_h/2+1),
- new Point(Width/2,Height/2-arrow_h/2+1),
- new Point(Width/2,Height/2-3*arrow_h/2),
- new Point(Width/2+arrow_h/2,Height/2-3*arrow_h/2),
- new Point(Width/2-arrow_h/2,Height/2-5*arrow_h/2)
- };
- break;
- case ArrowDirection.LeftDown:
- arrow_h = Math.Min(Width / 3, Height / 5);
- ps = new Point[]
- {
- new Point(Width/2-arrow_h/2,Height/2+5*arrow_h/2),
- new Point(Width/2-3*arrow_h/2,Height/2+3*arrow_h/2),
- new Point(Width/2-arrow_h,Height/2+3*arrow_h/2),
- new Point(Width/2-arrow_h,Height/2+arrow_h/2-1),
- new Point(Width/2,Height/2+arrow_h/2-1),
- new Point(Width/2,Height/2+3*arrow_h/2),
- new Point(Width/2+arrow_h/2,Height/2+3*arrow_h/2),
- new Point(Width/2-arrow_h/2,Height/2+5*arrow_h/2)
- };
- break;
- case ArrowDirection.RightTop:
- arrow_h = Math.Min(Width / 3, Height / 5);
- ps = new Point[]
- {
- new Point(Width/2+arrow_h/2,Height/2-5*arrow_h/2),
- new Point(Width/2+3*arrow_h/2,Height/2-3*arrow_h/2),
- new Point(Width/2+arrow_h,Height/2-3*arrow_h/2),
- new Point(Width/2+arrow_h,Height/2-arrow_h/2+1),
- new Point(Width/2,Height/2-arrow_h/2+1),
- new Point(Width/2,Height/2-3*arrow_h/2),
- new Point(Width/2-arrow_h/2,Height/2-3*arrow_h/2),
- new Point(Width/2+arrow_h/2,Height/2-5*arrow_h/2)
- };
- break;
- case ArrowDirection.RightDown:
- arrow_h = Math.Min(Width / 3, Height / 5);
- ps = new Point[]
- {
- new Point(Width/2+arrow_h/2,Height/2+5*arrow_h/2),
- new Point(Width/2+3*arrow_h/2,Height/2+3*arrow_h/2),
- new Point(Width/2+arrow_h,Height/2+3*arrow_h/2),
- new Point(Width/2+arrow_h,Height/2+arrow_h/2-1),
- new Point(Width/2,Height/2+arrow_h/2-1),
- new Point(Width/2,Height/2+3*arrow_h/2),
- new Point(Width/2-arrow_h/2,Height/2+3*arrow_h/2),
- new Point(Width/2+arrow_h/2,Height/2+5*arrow_h/2)
- };
- break;
- case ArrowDirection.DownLeft:
- arrow_h = Math.Min(Width / 5, Height / 3);
- ps = new Point[]
- {
- new Point(Width/2-5*arrow_h/2,Height/2+arrow_h/2),
- new Point(Width/2-3*arrow_h/2,Height/2+3*arrow_h/2),
- new Point(Width/2-3*arrow_h/2,Height/2+arrow_h),
- new Point(Width/2-arrow_h/2+1,Height/2+arrow_h),
- new Point(Width/2-arrow_h/2+1,Height/2),
- new Point(Width/2-3*arrow_h/2,Height/2),
- new Point(Width/2-3*arrow_h/2,Height/2-arrow_h/2),
- new Point(Width/2-5*arrow_h/2,Height/2+arrow_h/2)
- };
- break;
- case ArrowDirection.DownRight:
- arrow_h = Math.Min(Width / 5, Height / 3);
- ps = new Point[]
- {
- new Point(Width/2+5*arrow_h/2,Height/2+arrow_h/2),
- new Point(Width/2+3*arrow_h/2,Height/2+3*arrow_h/2),
- new Point(Width/2+3*arrow_h/2,Height/2+arrow_h),
- new Point(Width/2+arrow_h/2-1,Height/2+arrow_h),
- new Point(Width/2+arrow_h/2-1,Height/2),
- new Point(Width/2+3*arrow_h/2,Height/2),
- new Point(Width/2+3*arrow_h/2,Height/2-arrow_h/2),
- new Point(Width/2+5*arrow_h/2,Height/2+arrow_h/2)
- };
- break;
- }
- m_path = new GraphicsPath();
- m_path.AddLines(ps);
- m_path.CloseAllFigures();
- }
- /// <summary>
- /// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
- /// </summary>
- /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
- protected override void OnPaint(PaintEventArgs e)
- {
- base.OnPaint(e);
- var g = e.Graphics;
- g.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿
- g.InterpolationMode = InterpolationMode.HighQualityBicubic;
- g.CompositingQuality = CompositingQuality.HighQuality;
- g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
- //base.Region = new Region(m_path);
-
- g.FillPath(new SolidBrush(arrowColor), m_path);
- if (borderColor != null && borderColor != Color.Empty)
- g.DrawPath(new Pen(new SolidBrush(borderColor.Value)), m_path);
-
- if (direction == ArrowDirection.TopRight)
- {
- int arrow_h = Math.Min(Width / 5, Height / 3);
- g.FillPie(new SolidBrush(arrowColor),
- new Rectangle((Width - arrow_h)/ 2, Height / 2 - arrow_h, arrow_h * 2+1, arrow_h * 2+1), 180,90);
- g.FillRectangle(new SolidBrush(arrowColor),
- new RectangleF(new PointF((Width - arrow_h) / 2, Height / 2),new SizeF(arrow_h, Height / 2)));
- }
- if (direction == ArrowDirection.TopLeft)
- {
- int arrow_h = Math.Min(Width / 5, Height / 3);
- g.FillPie(new SolidBrush(arrowColor),
- new Rectangle((Width - 3*arrow_h) / 2, Height / 2 - arrow_h, arrow_h * 2, arrow_h * 2 + 1), 270, 90);
- g.FillRectangle(new SolidBrush(arrowColor),
- new RectangleF(new PointF((Width - arrow_h) / 2, Height / 2), new SizeF(arrow_h, Height / 2)));
- }
- if (direction == ArrowDirection.LeftTop)
- {
- int arrow_h = Math.Min(Width /3, Height / 5);
- g.FillPie(new SolidBrush(arrowColor),
- new Rectangle(Width / 2 - arrow_h,(Height - 3 * arrow_h) / 2, arrow_h * 2+1, arrow_h * 2), 90, 90);
- g.FillRectangle(new SolidBrush(arrowColor),
- new RectangleF(new PointF(Width / 2,(Height - arrow_h) / 2), new SizeF(Width / 2,arrow_h)));
- }
- if (direction == ArrowDirection.LeftDown)
- {
- int arrow_h = Math.Min(Width / 3, Height / 5);
- g.FillPie(new SolidBrush(arrowColor),
- new Rectangle(Width / 2 - arrow_h, (Height - arrow_h) / 2, arrow_h * 2+1 , arrow_h * 2), 180, 90);
- g.FillRectangle(new SolidBrush(arrowColor),
- new RectangleF(new PointF(Width / 2, (Height - arrow_h) / 2), new SizeF(Width / 2, arrow_h)));
- }
- if (direction == ArrowDirection.RightTop)
- {
- int arrow_h = Math.Min(Width / 3, Height / 5);
- g.FillPie(new SolidBrush(arrowColor),
- new Rectangle(Width / 2 - arrow_h, (Height - 3 * arrow_h) / 2, arrow_h * 2 , arrow_h * 2), 0, 90);
- g.FillRectangle(new SolidBrush(arrowColor),
- new RectangleF(new PointF(0, (Height - arrow_h) / 2), new SizeF(Width / 2+1, arrow_h)));
- }
- if (direction == ArrowDirection.RightDown)
- {
- int arrow_h = Math.Min(Width / 3, Height / 5);
- g.FillPie(new SolidBrush(arrowColor),
- new Rectangle(Width / 2 - arrow_h, (Height - arrow_h+1) / 2, arrow_h * 2, arrow_h * 2), 270, 90);
- g.FillRectangle(new SolidBrush(arrowColor),
- new RectangleF(new PointF(0, (Height - arrow_h) / 2), new SizeF(Width / 2 + 1, arrow_h)));
- }
- if (direction == ArrowDirection.DownLeft)
- {
- int arrow_h = Math.Min(Width / 5, Height / 3);
- g.FillPie(new SolidBrush(arrowColor),
- new Rectangle((Width - 3 * arrow_h) / 2, Height / 2 - arrow_h, arrow_h * 2, arrow_h * 2 ), 0, 90);
- g.FillRectangle(new SolidBrush(arrowColor),
- new RectangleF(new PointF((Width - arrow_h) / 2, 0), new SizeF(arrow_h, Height / 2+1)));
- }
- if (direction == ArrowDirection.DownRight)
- {
- int arrow_h = Math.Min(Width / 5, Height / 3);
- g.FillPie(new SolidBrush(arrowColor),
- new Rectangle((Width - arrow_h) / 2, Height / 2 - arrow_h, arrow_h * 2, arrow_h * 2), 90, 90);
- g.FillRectangle(new SolidBrush(arrowColor),
- new RectangleF(new PointF((Width - arrow_h) / 2, 0), new SizeF(arrow_h, Height / 2 + 1)));
- }
- if (!string.IsNullOrEmpty(text))
- {
- var size = g.MeasureString(Text, Font);
- g.DrawString(Text, Font, new SolidBrush(ForeColor), new PointF((this.Width - size.Width) / 2, (this.Height - size.Height) / 2));
- }
- }
- }
- /// <summary>
- /// Enum ArrowDirection
- /// </summary>
- public enum ArrowDirection
- {
- /// <summary>
- /// The left
- /// </summary>
- Left,
- /// <summary>
- /// The right
- /// </summary>
- Right,
- /// <summary>
- /// The top
- /// </summary>
- Top,
- /// <summary>
- /// The bottom
- /// </summary>
- Bottom,
- /// <summary>
- /// The left right
- /// </summary>
- Left_Right,
- /// <summary>
- /// The top bottom
- /// </summary>
- Top_Bottom,
- LeftTop,
- LeftDown,
- RightTop,
- RightDown,
- TopLeft,
- TopRight,
- DownLeft,
- DownRight
- }
- }
|