UCPond.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 2019-09-06
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCPond.cs">
  7. // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
  8. // </copyright>
  9. //
  10. // Blog: https://www.cnblogs.com/bfyx
  11. // GitHub:https://github.com/kwwwvagaa/NetWinformControl
  12. // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
  13. //
  14. // If you use this code, please keep this note.
  15. // ***********************************************************************
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using System.Text;
  20. using System.Windows.Forms;
  21. using System.Drawing;
  22. using System.Drawing.Drawing2D;
  23. using System.ComponentModel;
  24. namespace HZH_Controls.Controls
  25. {
  26. /// <summary>
  27. /// Class UCPond.
  28. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  29. /// </summary>
  30. /// <seealso cref="System.Windows.Forms.UserControl" />
  31. public class UCPond : UserControl
  32. {
  33. /// <summary>
  34. /// The maximum value
  35. /// </summary>
  36. private decimal maxValue = 100;
  37. /// <summary>
  38. /// Gets or sets the maximum value.
  39. /// </summary>
  40. /// <value>The maximum value.</value>
  41. [Description("最大值"), Category("自定义")]
  42. public decimal MaxValue
  43. {
  44. get { return maxValue; }
  45. set
  46. {
  47. if (value < m_value)
  48. return;
  49. maxValue = value;
  50. Refresh();
  51. }
  52. }
  53. /// <summary>
  54. /// The m value
  55. /// </summary>
  56. private decimal m_value = 0;
  57. /// <summary>
  58. /// Gets or sets the value.
  59. /// </summary>
  60. /// <value>The value.</value>
  61. [Description("值"), Category("自定义")]
  62. public decimal Value
  63. {
  64. get { return m_value; }
  65. set
  66. {
  67. if (value < 0)
  68. return;
  69. if (value > maxValue)
  70. m_value = maxValue;
  71. else
  72. m_value = value;
  73. Refresh();
  74. }
  75. }
  76. /// <summary>
  77. /// The wall color
  78. /// </summary>
  79. private Color wallColor = Color.FromArgb(255, 77, 59);
  80. /// <summary>
  81. /// Gets or sets the color of the wall.
  82. /// </summary>
  83. /// <value>The color of the wall.</value>
  84. [Description("池壁颜色"), Category("自定义")]
  85. public Color WallColor
  86. {
  87. get { return wallColor; }
  88. set
  89. {
  90. wallColor = value;
  91. Refresh();
  92. }
  93. }
  94. /// <summary>
  95. /// The wall width
  96. /// </summary>
  97. private int wallWidth = 2;
  98. /// <summary>
  99. /// Gets or sets the width of the wall.
  100. /// </summary>
  101. /// <value>The width of the wall.</value>
  102. [Description("池壁宽度"), Category("自定义")]
  103. public int WallWidth
  104. {
  105. get { return wallWidth; }
  106. set
  107. {
  108. if (value <= 0)
  109. return;
  110. wallWidth = value;
  111. Refresh();
  112. }
  113. }
  114. /// <summary>
  115. /// The liquid color
  116. /// </summary>
  117. private Color liquidColor = Color.FromArgb(3, 169, 243);
  118. /// <summary>
  119. /// Gets or sets the color of the liquid.
  120. /// </summary>
  121. /// <value>The color of the liquid.</value>
  122. [Description("液体颜色"), Category("自定义")]
  123. public Color LiquidColor
  124. {
  125. get { return liquidColor; }
  126. set { liquidColor = value; }
  127. }
  128. /// <summary>
  129. /// Initializes a new instance of the <see cref="UCPond" /> class.
  130. /// </summary>
  131. public UCPond()
  132. {
  133. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  134. this.SetStyle(ControlStyles.DoubleBuffer, true);
  135. this.SetStyle(ControlStyles.ResizeRedraw, true);
  136. this.SetStyle(ControlStyles.Selectable, true);
  137. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  138. this.SetStyle(ControlStyles.UserPaint, true);
  139. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
  140. this.Size = new Size(150, 50);
  141. }
  142. /// <summary>
  143. /// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
  144. /// </summary>
  145. /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
  146. protected override void OnPaint(PaintEventArgs e)
  147. {
  148. base.OnPaint(e);
  149. if (Height <= 0)
  150. return;
  151. var g = e.Graphics;
  152. g.SetGDIHigh();
  153. int intHeight = (int)(m_value / maxValue * this.Height);
  154. if (intHeight != 0)
  155. {
  156. g.FillRectangle(new SolidBrush(liquidColor), new Rectangle(0, this.Height - intHeight, this.Width, intHeight));
  157. }
  158. //划边
  159. g.FillRectangle(new SolidBrush(wallColor), 0, 0, wallWidth, this.Height);
  160. g.FillRectangle(new SolidBrush(wallColor), 0, this.Height - wallWidth, this.Width, wallWidth);
  161. g.FillRectangle(new SolidBrush(wallColor), this.Width - wallWidth-1, 0, wallWidth, this.Height);
  162. }
  163. }
  164. }