SkinPanel.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using LYFZ.OtherExpansion.SkinClass;
  2. using System;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6. namespace LYFZ.OtherExpansion.SkinControl
  7. {
  8. [ToolboxBitmap(typeof(Panel))]
  9. public class SkinPanel : Panel
  10. {
  11. private ControlState _controlState;
  12. private bool palace;
  13. private Rectangle backrectangle = new Rectangle(10, 10, 10, 10);
  14. private Image mouseback;
  15. private Image downback;
  16. private Image normlback;
  17. private int radius;
  18. private IContainer components;
  19. public ControlState ControlState
  20. {
  21. get
  22. {
  23. return this._controlState;
  24. }
  25. set
  26. {
  27. if (this._controlState != value)
  28. {
  29. this._controlState = value;
  30. base.Invalidate();
  31. }
  32. }
  33. }
  34. [Category("Skin"), DefaultValue(typeof(bool), "false"), Description("是否开启九宫绘图")]
  35. public bool Palace
  36. {
  37. get
  38. {
  39. return this.palace;
  40. }
  41. set
  42. {
  43. if (this.palace != value)
  44. {
  45. this.palace = value;
  46. base.Invalidate();
  47. }
  48. }
  49. }
  50. [Category("Skin"), DefaultValue(typeof(Rectangle), "10,10,10,10"), Description("九宫绘画区域")]
  51. public Rectangle BackRectangle
  52. {
  53. get
  54. {
  55. return this.backrectangle;
  56. }
  57. set
  58. {
  59. if (this.backrectangle != value)
  60. {
  61. this.backrectangle = value;
  62. }
  63. base.Invalidate();
  64. }
  65. }
  66. [Category("MouseEnter"), Description("悬浮时背景")]
  67. public Image MouseBack
  68. {
  69. get
  70. {
  71. return this.mouseback;
  72. }
  73. set
  74. {
  75. if (this.mouseback != value)
  76. {
  77. this.mouseback = value;
  78. base.Invalidate();
  79. }
  80. }
  81. }
  82. [Category("MouseDown"), Description("点击时背景")]
  83. public Image DownBack
  84. {
  85. get
  86. {
  87. return this.downback;
  88. }
  89. set
  90. {
  91. if (this.downback != value)
  92. {
  93. this.downback = value;
  94. base.Invalidate();
  95. }
  96. }
  97. }
  98. [Category("MouseNorml"), Description("初始时背景")]
  99. public Image NormlBack
  100. {
  101. get
  102. {
  103. return this.normlback;
  104. }
  105. set
  106. {
  107. if (this.normlback != value)
  108. {
  109. this.normlback = value;
  110. base.Invalidate();
  111. }
  112. }
  113. }
  114. [Category("Skin"), DefaultValue(typeof(int), "0"), Description("圆角大小")]
  115. public int Radius
  116. {
  117. get
  118. {
  119. return this.radius;
  120. }
  121. set
  122. {
  123. if (this.radius != value)
  124. {
  125. this.radius = ((value < 0) ? 0 : value);
  126. base.Invalidate();
  127. }
  128. }
  129. }
  130. public SkinPanel()
  131. {
  132. this.Init();
  133. base.ResizeRedraw = true;
  134. this.BackColor = Color.Transparent;
  135. }
  136. public void Init()
  137. {
  138. base.SetStyle(ControlStyles.ResizeRedraw, true);
  139. base.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  140. base.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  141. base.SetStyle(ControlStyles.UserPaint, true);
  142. base.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  143. base.UpdateStyles();
  144. }
  145. protected override void OnMouseEnter(EventArgs e)
  146. {
  147. this._controlState = ControlState.Hover;
  148. base.Invalidate();
  149. base.OnMouseEnter(e);
  150. }
  151. protected override void OnMouseLeave(EventArgs e)
  152. {
  153. this._controlState = ControlState.Normal;
  154. base.Invalidate();
  155. base.OnMouseLeave(e);
  156. }
  157. protected override void OnMouseDown(MouseEventArgs e)
  158. {
  159. if (e.Button == MouseButtons.Left)
  160. {
  161. this._controlState = ControlState.Pressed;
  162. base.Invalidate();
  163. }
  164. base.OnMouseDown(e);
  165. }
  166. protected override void OnMouseUp(MouseEventArgs e)
  167. {
  168. this._controlState = ControlState.Hover;
  169. base.Invalidate();
  170. base.OnMouseUp(e);
  171. }
  172. protected override void OnPaint(PaintEventArgs e)
  173. {
  174. Graphics g = e.Graphics;
  175. Bitmap btm;
  176. switch (this._controlState)
  177. {
  178. case ControlState.Hover:
  179. btm = (Bitmap)this.MouseBack;
  180. break;
  181. case ControlState.Pressed:
  182. btm = (Bitmap)this.DownBack;
  183. break;
  184. default:
  185. btm = (Bitmap)this.NormlBack;
  186. break;
  187. }
  188. if (btm != null)
  189. {
  190. if (this.Palace)
  191. {
  192. LYFZ.OtherExpansion.SkinClass.ImageDrawRect.DrawRect(g, btm, new Rectangle(base.ClientRectangle.X, base.ClientRectangle.Y, base.ClientRectangle.Width, base.ClientRectangle.Height), Rectangle.FromLTRB(this.BackRectangle.X, this.BackRectangle.Y, this.BackRectangle.Width, this.BackRectangle.Height), 1, 1);
  193. }
  194. else
  195. {
  196. this.BackgroundImage = btm;
  197. }
  198. }
  199. UpdateForm.CreateRegion(this, this.radius);
  200. base.OnPaint(e);
  201. }
  202. protected override void Dispose(bool disposing)
  203. {
  204. if (disposing && this.components != null)
  205. {
  206. this.components.Dispose();
  207. }
  208. base.Dispose(disposing);
  209. }
  210. private void InitializeComponent()
  211. {
  212. this.components = new Container();
  213. }
  214. }
  215. }