You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

348 lines
9.8 KiB

  1. #if NETFX || NETCORE
  2. using System;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Windows.Forms;
  6. namespace Apewer.Surface
  7. {
  8. /// <summary></summary>
  9. public class BlockBox : BaseControl
  10. {
  11. #region definition
  12. private const int StateNormal = 1;
  13. private const int StateHover = 2;
  14. private const int StateLost = 3;
  15. private Color _normalborder, _normalwall, _hoverborder, _hoverwall, _lostborder, _lostwall;
  16. private int _state = StateNormal;
  17. private bool _disposed = false;
  18. private bool _haveborder = true;
  19. //private Bitmap _background = null;
  20. private void VarInit()
  21. {
  22. _normalborder = FormsUtility.GraceBorder;
  23. _normalwall = FormsUtility.White;
  24. _hoverborder = FormsUtility.GraceSilver;
  25. _hoverwall = FormsUtility.White;
  26. }
  27. #endregion
  28. #region this
  29. /// <summary></summary>
  30. public BlockBox()
  31. {
  32. VarInit();
  33. SetStyle(ControlStyles.UserPaint, true);
  34. SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  35. SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  36. // SetStyle(ControlStyles.ResizeRedraw, value);
  37. SetStyle(ControlStyles.DoubleBuffer, true);
  38. UpdateStyles();
  39. //this.BackColor = normalborder;
  40. this.Padding = new Padding(1);
  41. //this.Controls.Add(_body);
  42. EventInit();
  43. UpdateColor();
  44. }
  45. /// <summary></summary>
  46. public new void Dispose()
  47. {
  48. if (!_disposed)
  49. {
  50. _disposed = true;
  51. base.Dispose();
  52. }
  53. }
  54. #endregion
  55. #region public accessor
  56. /// <summary>添加子控件。</summary>
  57. public void Add(Control control)
  58. {
  59. if (control != null) this.Controls.Add(control);
  60. }
  61. /// <summary>移除所有子控件。</summary>
  62. public void Clean()
  63. {
  64. this.Controls.Clear();
  65. }
  66. /// <summary>移除所有子控件。</summary>
  67. public void Clear()
  68. {
  69. this.Controls.Clear();
  70. }
  71. /// <summary>子控件的数量。</summary>
  72. public int Count
  73. {
  74. get { return this.Controls.Count; }
  75. }
  76. /// <summary>获取子控件。</summary>
  77. /// <param name="index">子控件索引。</param>
  78. public Control this[int index]
  79. {
  80. get { return this.Controls[index]; }
  81. }
  82. /// <summary></summary>
  83. public bool HaveBorder
  84. {
  85. get { return _haveborder; }
  86. set { _haveborder = value; UpdateColor(); }
  87. }
  88. /// <summary></summary>
  89. public Color NormalBorder
  90. {
  91. get { return _normalborder; }
  92. set { _normalborder = value; if (_state == StateNormal) UpdateColor(); }
  93. }
  94. /// <summary></summary>
  95. public Color NormalWall
  96. {
  97. get { return _normalwall; }
  98. set { _normalwall = value; if (_state == StateNormal) UpdateColor(); }
  99. }
  100. /// <summary></summary>
  101. public Color HoverBorder
  102. {
  103. get { return _hoverborder; }
  104. set { _hoverborder = value; if (_state == StateHover) UpdateColor(); }
  105. }
  106. /// <summary></summary>
  107. public Color HoverWall
  108. {
  109. get { return _hoverwall; }
  110. set { _hoverwall = value; if (_state == StateHover) UpdateColor(); }
  111. }
  112. /// <summary></summary>
  113. public Color LostBorder
  114. {
  115. get { return _lostborder; }
  116. set { _lostborder = value; if (_state == StateLost) UpdateColor(); }
  117. }
  118. /// <summary></summary>
  119. public Color LostWall
  120. {
  121. get { return _lostwall; }
  122. set { _lostwall = value; if (_state == StateLost) UpdateColor(); }
  123. }
  124. /// <summary></summary>
  125. public void GoHover()
  126. {
  127. if (_state != StateHover)
  128. {
  129. _state = StateHover;
  130. UpdateColor();
  131. }
  132. }
  133. /// <summary></summary>
  134. public void GoNormal()
  135. {
  136. if (_state != StateNormal)
  137. {
  138. _state = StateNormal;
  139. UpdateColor();
  140. }
  141. }
  142. /// <summary></summary>
  143. public void GoLost()
  144. {
  145. if (_state != StateLost)
  146. {
  147. _state = StateLost;
  148. UpdateColor();
  149. }
  150. }
  151. #endregion
  152. #region private method
  153. private void UpdateColor()
  154. {
  155. if ((this.Width > 0) && (this.Height > 0))
  156. {
  157. Color vbordercolor;
  158. Color vwallcolor;
  159. switch (_state)
  160. {
  161. default:
  162. case StateNormal:
  163. vbordercolor = NormalBorder;
  164. vwallcolor = NormalWall;
  165. break;
  166. case StateHover:
  167. vbordercolor = HoverBorder;
  168. vwallcolor = HoverWall;
  169. break;
  170. case StateLost:
  171. vbordercolor = LostBorder;
  172. vwallcolor = LostWall;
  173. break;
  174. }
  175. var vbackground = new Bitmap(this.Width, this.Height);
  176. var vgraphics = Graphics.FromImage(vbackground);
  177. var vpen = new Pen(vbordercolor);
  178. vgraphics.SmoothingMode = SmoothingMode.None;
  179. vgraphics.CompositingMode = CompositingMode.SourceCopy;
  180. vgraphics.Clear(vwallcolor);
  181. vgraphics.DrawRectangle(vpen, 0, 0, this.Width - 1, this.Height - 1);
  182. vpen.Dispose();
  183. vgraphics.Dispose();
  184. if (this.BackgroundImage != null)
  185. {
  186. this.BackgroundImage.Dispose();
  187. this.BackgroundImage = null;
  188. }
  189. this.BackgroundImage = vbackground;
  190. }
  191. }
  192. #endregion
  193. #region private event
  194. private void EventInit()
  195. {
  196. //_body.MouseClick += event_caption_mouseclick;
  197. //_body.MouseDoubleClick += event_caption_mousedoubleclick;
  198. //_body.MouseDown += event_caption_mousedown;
  199. //_body.MouseUp += event_caption_mouseup;
  200. //_body.MouseMove += event_caption_mousemove;
  201. //_body.MouseWheel += event_caption_mousewheel;
  202. //_body.Click += event_click;
  203. //_body.DoubleClick += event_doubleclick;
  204. //_body.MouseCaptureChanged += event_caption_mousecapturechanged;
  205. //_body.MouseEnter += event_caption_mouseenter;
  206. //_body.MouseHover += event_caption_mousehover;
  207. //_body.MouseLeave += event_caption_mouseleave;
  208. //_body.PreviewKeyDown += event_previewkeydown;
  209. this.Resize += Event_This_Resize;
  210. }
  211. private void Event_This_Resize(object sender, EventArgs e)
  212. {
  213. UpdateColor();
  214. }
  215. private void Event_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
  216. {
  217. this.OnPreviewKeyDown(e);
  218. }
  219. private void Event_DoubleClick(object sender, EventArgs e)
  220. {
  221. this.OnDoubleClick(e);
  222. }
  223. private void Event_Click(object sender, EventArgs e)
  224. {
  225. this.OnClick(e);
  226. }
  227. private void Event_Caption_MouseWheel(object sender, MouseEventArgs e)
  228. {
  229. int voffset = HaveBorder ? 1 : 0;
  230. this.OnMouseWheel(new MouseEventArgs(e.Button, e.Clicks, e.X + voffset, e.Y + voffset, e.Delta));
  231. }
  232. private void Event_Caption_MouseMove(object sender, MouseEventArgs e)
  233. {
  234. int voffset = HaveBorder ? 1 : 0;
  235. this.OnMouseMove(new MouseEventArgs(e.Button, e.Clicks, e.X + voffset, e.Y + voffset, e.Delta));
  236. }
  237. private void Event_Caption_MouseLeave(object sender, EventArgs e)
  238. {
  239. this.OnMouseLeave(e);
  240. }
  241. private void Event_Caption_MouseHover(object sender, EventArgs e)
  242. {
  243. this.OnMouseHover(e);
  244. }
  245. private void Event_Caption_MouseEnter(object sender, EventArgs e)
  246. {
  247. this.OnMouseEnter(e);
  248. }
  249. private void Event_Caption_MouseDoubleClick(object sender, MouseEventArgs e)
  250. {
  251. int voffset = HaveBorder ? 1 : 0;
  252. this.OnMouseDoubleClick(new MouseEventArgs(e.Button, e.Clicks, e.X + voffset, e.Y + voffset, e.Delta));
  253. }
  254. private void Event_Caption_MouseCaptureChanged(object sender, EventArgs e)
  255. {
  256. this.OnMouseCaptureChanged(e);
  257. }
  258. private void Event_Caption_MouseUp(object sender, MouseEventArgs e)
  259. {
  260. int voffset = HaveBorder ? 1 : 0;
  261. this.OnMouseUp(new MouseEventArgs(e.Button, e.Clicks, e.X + voffset, e.Y + voffset, e.Delta));
  262. }
  263. private void Event_Caption_MouseClick(object sender, MouseEventArgs e)
  264. {
  265. int voffset = HaveBorder ? 1 : 0;
  266. this.OnMouseClick(new MouseEventArgs(e.Button, e.Clicks, e.X + voffset, e.Y + voffset, e.Delta));
  267. }
  268. private void Event_Caption_MouseDown(object sender, MouseEventArgs e)
  269. {
  270. int voffset = HaveBorder ? 1 : 0;
  271. this.OnMouseDown(new MouseEventArgs(e.Button, e.Clicks, e.X + voffset, e.Y + voffset, e.Delta));
  272. }
  273. #endregion
  274. #region common
  275. /// <summary>鼠标滚轮事件。</summary>
  276. public new void WheelMouse(MouseEventArgs e)
  277. {
  278. OnMouseWheel(e);
  279. }
  280. #endregion
  281. }
  282. }
  283. #endif