TriangleButton.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Drawing.Drawing2D;
  11. namespace PaintDotNet.CustomControl
  12. {
  13. public partial class TriangleButton : UserControl
  14. {
  15. public TriangleButton()
  16. {
  17. InitializeComponent();
  18. }
  19. /// <summary>
  20. /// 必需的设计器变量。
  21. /// </summary>
  22. private System.ComponentModel.IContainer components = null;
  23. /// <summary>
  24. /// 清理所有正在使用的资源。
  25. /// </summary>
  26. /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
  27. protected override void Dispose(bool disposing)
  28. {
  29. if (disposing && (components != null))
  30. {
  31. components.Dispose();
  32. }
  33. base.Dispose(disposing);
  34. }
  35. #region 组件设计器生成的代码
  36. /// <summary>
  37. /// 设计器支持所需的方法 - 不要修改
  38. /// 使用代码编辑器修改此方法的内容。
  39. /// </summary>
  40. private void InitializeComponent()
  41. {
  42. components = new System.ComponentModel.Container();
  43. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  44. this.SuspendLayout();
  45. //
  46. // TriangleButton
  47. //
  48. this.Name = "TriangleButton";
  49. this.ResumeLayout(false);
  50. }
  51. #endregion
  52. private Point lastTrackingMouseXY = new Point(-1, -1);
  53. private int tracking = -1;
  54. private int highlight = -1;
  55. //备注:设置三角形尺寸
  56. private const int triangleSize = 7;
  57. private /*const*/ int triangleHalfLength = (triangleSize - 1) / 2;
  58. private AnchorStyles alignmentStyle = AnchorStyles.Left;
  59. public AnchorStyles AlignmentStyle
  60. {
  61. get
  62. {
  63. return this.alignmentStyle;
  64. }
  65. set
  66. {
  67. if (value != this.alignmentStyle)
  68. {
  69. this.alignmentStyle = value;
  70. Invalidate();
  71. }
  72. }
  73. }
  74. protected override void OnMouseLeave(EventArgs e)
  75. {
  76. base.OnMouseLeave(e);
  77. //int oldhighlight = highlight;
  78. highlight = -1;
  79. //this.InvalidateTriangle(oldhighlight);
  80. this.Refresh();
  81. }
  82. protected override void OnMouseMove(MouseEventArgs e)
  83. {
  84. base.OnMouseMove(e);
  85. highlight = 1;
  86. this.Refresh();
  87. }
  88. protected override void OnMouseDown(MouseEventArgs e)
  89. {
  90. base.OnMouseDown(e);
  91. if (e.Button == MouseButtons.Left)
  92. {
  93. tracking = 1;
  94. this.Refresh();
  95. }
  96. }
  97. protected override void OnMouseUp(MouseEventArgs e)
  98. {
  99. base.OnMouseUp(e);
  100. if (e.Button == MouseButtons.Left)
  101. {
  102. tracking = -1;
  103. }
  104. this.Refresh();
  105. }
  106. private void DrawGradient(Graphics g)
  107. {
  108. g.PixelOffsetMode = PixelOffsetMode.Half;
  109. // draw value triangles
  110. int pos = 0;// triangleHalfLength + 0;
  111. Brush brush;
  112. Pen pen;
  113. if (-1 != tracking)
  114. {
  115. brush = Brushes.Blue;// Blue;
  116. pen = (Pen)Pens.Transparent/*White*/.Clone();
  117. }
  118. else if(-1 != highlight)
  119. {
  120. brush = Brushes.LightGray;// Black;
  121. pen = (Pen)Pens.Blue.Clone();
  122. }
  123. else
  124. {
  125. brush = Brushes.LightGray;// Black;
  126. pen = (Pen)Pens.Gray.Clone();
  127. }
  128. g.SmoothingMode = SmoothingMode.AntiAlias;
  129. triangleHalfLength = (int)((Height - 1) / 2);
  130. Point a1 = new Point(0, 0);// new Point(pos - triangleHalfLength, 0);
  131. Point b1 = new Point(0, Height - 1);//new Point(pos, triangleSize - 1);
  132. Point c1 = new Point(Width - 1, pos + triangleHalfLength);//new Point(pos + triangleHalfLength, 0);
  133. switch (this.alignmentStyle)
  134. {
  135. case AnchorStyles.Left:
  136. // gradientAngle = 180.0f;
  137. break;
  138. case AnchorStyles.Right:
  139. a1 = new Point(Width - 1, 0);// new Point(pos - triangleHalfLength, 0);
  140. b1 = new Point(Width - 1, Height - 1);//new Point(pos, triangleSize - 1);
  141. c1 = new Point(0, pos + triangleHalfLength);//new Point(pos + triangleHalfLength, 0);
  142. break;
  143. default:
  144. throw new InvalidEnumArgumentException();
  145. }
  146. //Point a2 = new Point(a1.X, Height - 1 - a1.Y);
  147. //Point b2 = new Point(b1.X, Height - 1 - b1.Y);
  148. //Point c2 = new Point(c1.X, Height - 1 - c1.Y);
  149. //if (this.drawNearNub)
  150. //{//倒三角
  151. g.FillPolygon(brush, new Point[] { a1, b1, c1, a1 });
  152. //}
  153. ////if (this.drawFarNub)
  154. ////{//正三角
  155. // g.FillPolygon(brush, new Point[] { a2, b2, c2, a2 });
  156. ////}
  157. //if (pen != null)
  158. //{
  159. // if (this.drawNearNub)
  160. // {
  161. g.DrawPolygon(pen, new Point[] { a1, b1, c1, a1 });
  162. //}
  163. ////if (this.drawFarNub)
  164. ////{
  165. // g.DrawPolygon(pen, new Point[] { a2, b2, c2, a2 });
  166. ////}
  167. pen.Dispose();
  168. //}
  169. }
  170. protected override void OnPaint(PaintEventArgs e)
  171. {
  172. base.OnPaint(e);
  173. DrawGradient(e.Graphics);
  174. }
  175. protected override void OnPaintBackground(PaintEventArgs pevent)
  176. {
  177. base.OnPaintBackground(pevent);
  178. DrawGradient(pevent.Graphics);
  179. }
  180. }
  181. }