FlexiblePanel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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.Xml;
  11. namespace Y.Controls.Container
  12. {
  13. public partial class FlexiblePanel : Panel
  14. {
  15. Color HighlightColor = ColorTranslator.FromHtml("#78c4ec");
  16. /// <summary>
  17. /// 光标状态
  18. /// </summary>
  19. private enum EnumMousePointPosition
  20. {
  21. MouseSizeNone = 0, //'无
  22. MouseSizeRight = 1, //'拉伸右边框
  23. MouseSizeLeft = 2, //'拉伸左边框
  24. MouseSizeBottom = 3, //'拉伸下边框
  25. MouseSizeTop = 4, //'拉伸上边框
  26. MouseSizeTopLeft = 5, //'拉伸左上角
  27. MouseSizeTopRight = 6, //'拉伸右上角
  28. MouseSizeBottomLeft = 7, //'拉伸左下角
  29. MouseSizeBottomRight = 8, //'拉伸右下角
  30. MouseDrag = 9 // '鼠标拖动
  31. }
  32. #region 属性
  33. private static string xmlDocPath = "";
  34. private XmlDocument doc;
  35. private const int Band = 5;
  36. private const int MinWidth = 10;
  37. private const int MinHeight = 10;
  38. private EnumMousePointPosition m_MousePointPosition;
  39. private Point p, p1;
  40. #endregion
  41. public FlexiblePanel()
  42. {
  43. SetStyle(ControlStyles.UserPaint, true);
  44. SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
  45. SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
  46. InitializeComponent();
  47. }
  48. #region 改变控件大小和移动位置用到的方法
  49. private void MyMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  50. {
  51. p.X = e.X;
  52. p.Y = e.Y;
  53. p1.X = e.X;
  54. p1.Y = e.Y;
  55. }
  56. /// <summary>
  57. /// 鼠标离开事件需要改进
  58. /// </summary>
  59. /// <param name="sender"></param>
  60. /// <param name="e"></param>
  61. private void MyMouseLeave(object sender, EventArgs e)
  62. {
  63. Control s = (Control)sender;
  64. XmlNodeList nodes = doc.GetElementsByTagName(s.Name);
  65. XmlElement xn;
  66. if (nodes.Count != 1)
  67. {
  68. xn = doc.CreateElement(s.Name);
  69. }
  70. else
  71. {
  72. xn = (XmlElement)doc.GetElementsByTagName(s.Name)[0];
  73. }
  74. xn.SetAttribute("Top", s.Top.ToString());
  75. xn.SetAttribute("Left", s.Left.ToString());
  76. xn.SetAttribute("Width", s.Width.ToString());
  77. xn.SetAttribute("Height", s.Height.ToString());
  78. XmlNodeList xnl = doc.GetElementsByTagName(this.Name);
  79. XmlElement xnp;
  80. if (xnl.Count < 1)
  81. {
  82. xnp = doc.CreateElement(this.Name);
  83. }
  84. else
  85. {
  86. xnp = (XmlElement)xnl[0];
  87. }
  88. xnp.AppendChild((XmlNode)xn);
  89. doc.DocumentElement.AppendChild((XmlNode)xnp);
  90. doc.Save(xmlDocPath);
  91. m_MousePointPosition = EnumMousePointPosition.MouseSizeNone;
  92. this.Cursor = Cursors.Arrow;
  93. ((Control)sender).BackColor = Color.Transparent;
  94. }
  95. private EnumMousePointPosition MousePointPosition(Size size, System.Windows.Forms.MouseEventArgs e)
  96. {
  97. if ((e.X >= -1 * Band) | (e.X <= size.Width) | (e.Y >= -1 * Band) | (e.Y <= size.Height))
  98. {
  99. if (e.X < Band)
  100. {
  101. if (e.Y < Band) { return EnumMousePointPosition.MouseSizeTopLeft; }
  102. else
  103. {
  104. if (e.Y > -1 * Band + size.Height)
  105. { return EnumMousePointPosition.MouseSizeBottomLeft; }
  106. else
  107. { return EnumMousePointPosition.MouseSizeLeft; }
  108. }
  109. }
  110. else
  111. {
  112. if (e.X > -1 * Band + size.Width)
  113. {
  114. if (e.Y < Band)
  115. { return EnumMousePointPosition.MouseSizeTopRight; }
  116. else
  117. {
  118. if (e.Y > -1 * Band + size.Height)
  119. { return EnumMousePointPosition.MouseSizeBottomRight; }
  120. else
  121. { return EnumMousePointPosition.MouseSizeRight; }
  122. }
  123. }
  124. else
  125. {
  126. if (e.Y < Band)
  127. { return EnumMousePointPosition.MouseSizeTop; }
  128. else
  129. {
  130. if (e.Y > -1 * Band + size.Height)
  131. { return EnumMousePointPosition.MouseSizeBottom; }
  132. else
  133. { return EnumMousePointPosition.MouseDrag; }
  134. }
  135. }
  136. }
  137. }
  138. else
  139. { return EnumMousePointPosition.MouseSizeNone; }
  140. }
  141. private void MyMouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
  142. {
  143. Control lCtrl = (sender as Control);
  144. if (e.Button == MouseButtons.Left)
  145. {
  146. switch (m_MousePointPosition)
  147. {
  148. case EnumMousePointPosition.MouseDrag:
  149. lCtrl.Left = lCtrl.Left + e.X - p.X;
  150. lCtrl.Top = lCtrl.Top + e.Y - p.Y;
  151. break;
  152. case EnumMousePointPosition.MouseSizeBottom:
  153. lCtrl.Height = lCtrl.Height + e.Y - p1.Y;
  154. p1.X = e.X;
  155. p1.Y = e.Y; //'记录光标拖动的当前点
  156. break;
  157. case EnumMousePointPosition.MouseSizeBottomRight:
  158. lCtrl.Width = lCtrl.Width + e.X - p1.X;
  159. lCtrl.Height = lCtrl.Height + e.Y - p1.Y;
  160. p1.X = e.X;
  161. p1.Y = e.Y; //'记录光标拖动的当前点
  162. break;
  163. case EnumMousePointPosition.MouseSizeRight:
  164. lCtrl.Width = lCtrl.Width + e.X - p1.X;
  165. // lCtrl.Height = lCtrl.Height + e.Y - p1.Y;
  166. p1.X = e.X;
  167. p1.Y = e.Y; //'记录光标拖动的当前点
  168. break;
  169. case EnumMousePointPosition.MouseSizeTop:
  170. lCtrl.Top = lCtrl.Top + (e.Y - p.Y);
  171. lCtrl.Height = lCtrl.Height - (e.Y - p.Y);
  172. break;
  173. case EnumMousePointPosition.MouseSizeLeft:
  174. lCtrl.Left = lCtrl.Left + e.X - p.X;
  175. lCtrl.Width = lCtrl.Width - (e.X - p.X);
  176. break;
  177. case EnumMousePointPosition.MouseSizeBottomLeft:
  178. lCtrl.Left = lCtrl.Left + e.X - p.X;
  179. lCtrl.Width = lCtrl.Width - (e.X - p.X);
  180. lCtrl.Height = lCtrl.Height + e.Y - p1.Y;
  181. p1.X = e.X;
  182. p1.Y = e.Y; //'记录光标拖动的当前点
  183. break;
  184. case EnumMousePointPosition.MouseSizeTopRight:
  185. lCtrl.Top = lCtrl.Top + (e.Y - p.Y);
  186. lCtrl.Width = lCtrl.Width + (e.X - p1.X);
  187. lCtrl.Height = lCtrl.Height - (e.Y - p.Y);
  188. p1.X = e.X;
  189. p1.Y = e.Y; //'记录光标拖动的当前点
  190. break;
  191. case EnumMousePointPosition.MouseSizeTopLeft:
  192. lCtrl.Left = lCtrl.Left + e.X - p.X;
  193. lCtrl.Top = lCtrl.Top + (e.Y - p.Y);
  194. lCtrl.Width = lCtrl.Width - (e.X - p.X);
  195. lCtrl.Height = lCtrl.Height - (e.Y - p.Y);
  196. break;
  197. default:
  198. break;
  199. }
  200. if (lCtrl.Width < MinWidth) lCtrl.Width = MinWidth;
  201. if (lCtrl.Height < MinHeight) lCtrl.Height = MinHeight;
  202. }
  203. else
  204. {
  205. m_MousePointPosition = MousePointPosition(lCtrl.Size, e); //'判断光标的位置状态
  206. switch (m_MousePointPosition) //'改变光标
  207. {
  208. case EnumMousePointPosition.MouseSizeNone:
  209. this.Cursor = Cursors.Arrow; //'箭头
  210. break;
  211. case EnumMousePointPosition.MouseDrag:
  212. this.Cursor = Cursors.SizeAll; //'四方向
  213. ((Control)sender).BackColor = HighlightColor;
  214. break;
  215. case EnumMousePointPosition.MouseSizeBottom:
  216. this.Cursor = Cursors.SizeNS; //'南北
  217. break;
  218. case EnumMousePointPosition.MouseSizeTop:
  219. this.Cursor = Cursors.SizeNS; //'南北
  220. break;
  221. case EnumMousePointPosition.MouseSizeLeft:
  222. this.Cursor = Cursors.SizeWE; //'东西
  223. break;
  224. case EnumMousePointPosition.MouseSizeRight:
  225. this.Cursor = Cursors.SizeWE; //'东西
  226. break;
  227. case EnumMousePointPosition.MouseSizeBottomLeft:
  228. this.Cursor = Cursors.SizeNESW; //'东北到南西
  229. break;
  230. case EnumMousePointPosition.MouseSizeBottomRight:
  231. this.Cursor = Cursors.SizeNWSE; //'东南到西北
  232. break;
  233. case EnumMousePointPosition.MouseSizeTopLeft:
  234. this.Cursor = Cursors.SizeNWSE; //'东南到西北
  235. break;
  236. case EnumMousePointPosition.MouseSizeTopRight:
  237. this.Cursor = Cursors.SizeNESW; //'东北到南西
  238. break;
  239. default:
  240. break;
  241. }
  242. }
  243. }
  244. #endregion
  245. #region 初始化鼠标事件委托和控件大小和移动
  246. private void initProperty()
  247. {
  248. for (int i = 0; i < this.Controls.Count; i++)
  249. {
  250. this.Controls[i].MouseDown += new System.Windows.Forms.MouseEventHandler(MyMouseDown);
  251. this.Controls[i].MouseLeave += new System.EventHandler(MyMouseLeave);
  252. this.Controls[i].MouseMove += new System.Windows.Forms.MouseEventHandler(MyMouseMove);
  253. }
  254. }
  255. private void initStyle()
  256. {
  257. Control s;
  258. for (int i = 0; i < this.Controls.Count; i++)
  259. {
  260. s = this.Controls[i];
  261. XmlNodeList nodes = doc.GetElementsByTagName(s.Name);
  262. if (nodes.Count == 1)
  263. {
  264. XmlAttributeCollection xac = nodes[0].Attributes;
  265. foreach (XmlAttribute xa in xac)
  266. {
  267. if (xa.Value == "")
  268. continue;
  269. switch (xa.Name)
  270. {
  271. case "Top":
  272. var Top = Convert.ToInt32(xa.Value);
  273. //if (Top > 0 && Top < this.Height - s.Height)
  274. s.Top = Top;
  275. break;
  276. case "Left":
  277. var Left = Convert.ToInt32(xa.Value);
  278. //if (Left > 0 && Left < this.Width - s.Width)
  279. s.Left = Left;
  280. break;
  281. case "Width":
  282. s.Width = Convert.ToInt32(xa.Value);
  283. break;
  284. case "Height":
  285. s.Height = Convert.ToInt32(xa.Value);
  286. break;
  287. default:
  288. break;
  289. }
  290. }
  291. }
  292. }
  293. }
  294. #endregion
  295. /// <summary>
  296. /// 用于实现容器内控件移动和改变大小的方法
  297. /// </summary>
  298. /// <param name="XmlDoc">用于保存控件的属性的XML文档</param>
  299. public void InitMouseAndContolStyle(string XmlDocPath)
  300. {
  301. xmlDocPath = XmlDocPath;
  302. doc = new XmlDocument();
  303. doc.Load(XmlDocPath);
  304. initProperty();
  305. initStyle();
  306. }
  307. }
  308. }