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();
}
///
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的资源。
///
/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region 组件设计器生成的代码
///
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
///
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);
}
}
}