123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Drawing.Drawing2D;
- namespace PaintDotNet.CustomControl
- {
- public partial class TriangleButton : UserControl
- {
- public TriangleButton()
- {
- InitializeComponent();
- }
- /// <summary>
- /// 必需的设计器变量。
- /// </summary>
- private System.ComponentModel.IContainer components = null;
- /// <summary>
- /// 清理所有正在使用的资源。
- /// </summary>
- /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- #region 组件设计器生成的代码
- /// <summary>
- /// 设计器支持所需的方法 - 不要修改
- /// 使用代码编辑器修改此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- components = new System.ComponentModel.Container();
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.SuspendLayout();
- //
- // TriangleButton
- //
- this.Name = "TriangleButton";
- this.ResumeLayout(false);
- }
- #endregion
- private Point lastTrackingMouseXY = new Point(-1, -1);
- private int tracking = -1;
- private int highlight = -1;
- //备注:设置三角形尺寸
- private const int triangleSize = 7;
- private /*const*/ int triangleHalfLength = (triangleSize - 1) / 2;
- private AnchorStyles alignmentStyle = AnchorStyles.Left;
- public AnchorStyles AlignmentStyle
- {
- get
- {
- return this.alignmentStyle;
- }
- set
- {
- if (value != this.alignmentStyle)
- {
- this.alignmentStyle = value;
- Invalidate();
- }
- }
- }
- protected override void OnMouseLeave(EventArgs e)
- {
- base.OnMouseLeave(e);
- //int oldhighlight = highlight;
- highlight = -1;
- //this.InvalidateTriangle(oldhighlight);
- this.Refresh();
- }
- protected override void OnMouseMove(MouseEventArgs e)
- {
- base.OnMouseMove(e);
- highlight = 1;
- this.Refresh();
- }
- protected override void OnMouseDown(MouseEventArgs e)
- {
- base.OnMouseDown(e);
- if (e.Button == MouseButtons.Left)
- {
- tracking = 1;
- this.Refresh();
- }
- }
- protected override void OnMouseUp(MouseEventArgs e)
- {
- base.OnMouseUp(e);
- if (e.Button == MouseButtons.Left)
- {
- tracking = -1;
- }
- this.Refresh();
- }
- private void DrawGradient(Graphics g)
- {
- g.PixelOffsetMode = PixelOffsetMode.Half;
- // draw value triangles
- int pos = 0;// triangleHalfLength + 0;
- Brush brush;
- Pen pen;
- if (-1 != tracking)
- {
- brush = Brushes.Blue;// Blue;
- pen = (Pen)Pens.Transparent/*White*/.Clone();
- }
- else if(-1 != highlight)
- {
- brush = Brushes.LightGray;// Black;
- pen = (Pen)Pens.Blue.Clone();
- }
- else
- {
- brush = Brushes.LightGray;// Black;
- pen = (Pen)Pens.Gray.Clone();
- }
- g.SmoothingMode = SmoothingMode.AntiAlias;
- triangleHalfLength = (int)((Height - 1) / 2);
- Point a1 = new Point(0, 0);// new Point(pos - triangleHalfLength, 0);
- Point b1 = new Point(0, Height - 1);//new Point(pos, triangleSize - 1);
- Point c1 = new Point(Width - 1, pos + triangleHalfLength);//new Point(pos + triangleHalfLength, 0);
- switch (this.alignmentStyle)
- {
- case AnchorStyles.Left:
- // gradientAngle = 180.0f;
- break;
- case AnchorStyles.Right:
- a1 = new Point(Width - 1, 0);// new Point(pos - triangleHalfLength, 0);
- b1 = new Point(Width - 1, Height - 1);//new Point(pos, triangleSize - 1);
- c1 = new Point(0, pos + triangleHalfLength);//new Point(pos + triangleHalfLength, 0);
- break;
- default:
- throw new InvalidEnumArgumentException();
- }
- //Point a2 = new Point(a1.X, Height - 1 - a1.Y);
- //Point b2 = new Point(b1.X, Height - 1 - b1.Y);
- //Point c2 = new Point(c1.X, Height - 1 - c1.Y);
- //if (this.drawNearNub)
- //{//倒三角
- g.FillPolygon(brush, new Point[] { a1, b1, c1, a1 });
- //}
- ////if (this.drawFarNub)
- ////{//正三角
- // g.FillPolygon(brush, new Point[] { a2, b2, c2, a2 });
- ////}
- //if (pen != null)
- //{
- // if (this.drawNearNub)
- // {
- g.DrawPolygon(pen, new Point[] { a1, b1, c1, a1 });
- //}
- ////if (this.drawFarNub)
- ////{
- // g.DrawPolygon(pen, new Point[] { a2, b2, c2, a2 });
- ////}
- pen.Dispose();
- //}
- }
- protected override void OnPaint(PaintEventArgs e)
- {
- base.OnPaint(e);
- DrawGradient(e.Graphics);
- }
- protected override void OnPaintBackground(PaintEventArgs pevent)
- {
- base.OnPaintBackground(pevent);
- DrawGradient(pevent.Graphics);
- }
- }
- }
|