Class2.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /******************************************************************
  2. 2 * 创 建 人: SamWang
  3. 3 * 创建时间: 2012-5-10 16:06
  4. 4 * 描 述:
  5. 5 * 移动控件但不改变大小
  6. 6 * 原 理:
  7. 7 * 版 本: V1.0
  8. 8 * 环 境: VS2010
  9. 9 ******************************************************************/
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Windows.Forms;
  15. using System.Drawing;
  16. using System.Drawing.Drawing2D;
  17. namespace DragControl
  18. {
  19. public class MoveControl
  20. {
  21. #region Constructors
  22. public MoveControl(Control ctrl)
  23. {
  24. currentControl = ctrl;
  25. AddEvents();
  26. }
  27. #endregion
  28. #region Fields
  29. private Control currentControl; //传入的控件
  30. private Point pPoint; //上个鼠标坐标
  31. private Point cPoint; //当前鼠标坐标
  32. FrameControl fc;//边框控件
  33. #endregion
  34. #region Properties
  35. #endregion
  36. #region Methods
  37. /// <summary>
  38. /// 挂载事件
  39. /// </summary>
  40. private void AddEvents()
  41. {
  42. currentControl.MouseClick += new MouseEventHandler(MouseClick);
  43. currentControl.MouseDown += new MouseEventHandler(MouseDown);
  44. currentControl.MouseMove += new MouseEventHandler(MouseMove);
  45. currentControl.MouseUp += new MouseEventHandler(MouseUp);
  46. }
  47. /// <summary>
  48. /// 绘制拖拉时的黑色边框
  49. /// </summary>
  50. public static void DrawDragBound(Control ctrl)
  51. {
  52. ctrl.Refresh();
  53. Graphics g = ctrl.CreateGraphics();
  54. int width = ctrl.Width;
  55. int height = ctrl.Height;
  56. Point[] ps = new Point[5]{new Point(0,0),new Point(width -1,0),
  57. new Point(width -1,height -1),new Point(0,height-1),new Point(0,0)};
  58. g.DrawLines(new Pen(Color.Black), ps);
  59. }
  60. #endregion
  61. #region Events
  62. /// <summary>
  63. /// 鼠标单击事件:用来显示边框
  64. /// </summary>
  65. /// <param name="sender"></param>
  66. /// <param name="e"></param>
  67. protected void MouseClick(object sender, MouseEventArgs e)
  68. {
  69. this.currentControl.Parent.Refresh();//刷新父容器,清除掉其他控件的边框
  70. this.currentControl.BringToFront();
  71. fc = new FrameControl(this.currentControl);
  72. this.currentControl.Parent.Controls.Add(fc);
  73. fc.Visible = true;
  74. fc.Draw();
  75. }
  76. /// <summary>
  77. /// 鼠标按下事件:记录当前鼠标相对窗体的坐标
  78. /// </summary>
  79. void MouseDown(object sender, MouseEventArgs e)
  80. {
  81. pPoint = Cursor.Position;
  82. }
  83. /// <summary>
  84. /// 鼠标移动事件:让控件跟着鼠标移动
  85. /// </summary>
  86. void MouseMove(object sender, MouseEventArgs e)
  87. {
  88. Cursor.Current = Cursors.SizeAll; //当鼠标处于控件内部时,显示光标样式为SizeAll
  89. //当鼠标左键按下时才触发
  90. if (e.Button == MouseButtons.Left)
  91. {
  92. MoveControl.DrawDragBound(this.currentControl);
  93. if(fc != null ) fc.Visible = false; //先隐藏
  94. cPoint = Cursor.Position;//获得当前鼠标位置
  95. int x = cPoint.X - pPoint.X;
  96. int y = cPoint.Y - pPoint.Y;
  97. currentControl.Location = new Point(currentControl.Location.X + x, currentControl.Location.Y + y);
  98. pPoint = cPoint;
  99. }
  100. }
  101. /// <summary>
  102. /// 鼠标弹起事件:让自定义的边框出现
  103. /// </summary>
  104. void MouseUp(object sender, MouseEventArgs e)
  105. {
  106. this.currentControl.Refresh();
  107. if (fc != null)
  108. {
  109. fc.Visible = true;
  110. fc.Draw();
  111. }
  112. }
  113. #endregion
  114. }
  115. }
  116. /******************************************************************
  117. 2 * 创 建 人: SamWang
  118. 3 * 创建时间: 2012-5-10 17:00
  119. 4 * 描 述:
  120. 5 * 在控件外部加上边框,用于拖拉,以改变内部控件的大小
  121. 6 * 原 理:
  122. 7 * 版 本: V1.0
  123. 8 * 环 境: VS2010
  124. 9 ******************************************************************/
  125. using System;
  126. using System.Collections.Generic;
  127. using System.Text;
  128. using System.Windows.Forms;
  129. using System.Drawing;
  130. using System.Drawing.Drawing2D;
  131. namespace DragControl
  132. {
  133. public class FrameControl : UserControl
  134. {
  135. #region Constructors
  136. /// <summary>
  137. /// 构造函数
  138. /// </summary>
  139. public FrameControl(Control ctrl)
  140. {
  141. baseControl = ctrl;
  142. AddEvents();
  143. CreateBounds();
  144. }
  145. #endregion
  146. #region Fields
  147. const int Band = 6; //调整大小的响应边框
  148. private int MinWidth = 20; //最小宽度
  149. private int MinHeight = 20;//最小高度
  150. Size square = new Size(Band, Band);//小矩形大小
  151. Control baseControl; //基础控件,即被包围的控件
  152. Rectangle[] smallRects = new Rectangle[8];//边框中的八个小圆圈
  153. Rectangle[] sideRects = new Rectangle[4];//四条边框,用来做响应区域
  154. Point[] linePoints = new Point[5];//四条边,用于画虚线
  155. Graphics g; //画图板
  156. Rectangle ControlRect; //控件包含边框的区域
  157. private Point pPoint; //上个鼠标坐标
  158. private Point cPoint; //当前鼠标坐标
  159. private MousePosOnCtrl mpoc;
  160. #endregion
  161. #region Properties
  162. /// <summary>
  163. /// 鼠标在控件中位置
  164. /// </summary>
  165. enum MousePosOnCtrl
  166. {
  167. NONE = 0,
  168. TOP = 1,
  169. RIGHT = 2,
  170. BOTTOM = 3,
  171. LEFT = 4,
  172. TOPLEFT = 5,
  173. TOPRIGHT = 6,
  174. BOTTOMLEFT = 7,
  175. BOTTOMRIGHT = 8,
  176. }
  177. #endregion
  178. #region Methods
  179. /// <summary>
  180. /// 加载事件
  181. /// </summary>
  182. private void AddEvents()
  183. {
  184. this.Name = "FrameControl" + baseControl.Name;
  185. this.MouseDown += new MouseEventHandler(FrameControl_MouseDown);
  186. this.MouseMove += new MouseEventHandler(FrameControl_MouseMove);
  187. this.MouseUp += new MouseEventHandler(FrameControl_MouseUp);
  188. }
  189. #region 创建边框
  190. /// <summary>
  191. /// 建立控件可视区域
  192. /// </summary>
  193. private void CreateBounds()
  194. {
  195. //创建边界
  196. int X = baseControl.Bounds.X - square.Width - 1;
  197. int Y = baseControl.Bounds.Y - square.Height - 1;
  198. int Height = baseControl.Bounds.Height + (square.Height * 2) + 2;
  199. int Width = baseControl.Bounds.Width + (square.Width * 2) + 2;
  200. this.Bounds = new Rectangle(X, Y, Width, Height);
  201. this.BringToFront();
  202. SetRectangles();
  203. //设置可视区域
  204. this.Region = new Region(BuildFrame());
  205. g = this.CreateGraphics();
  206. }
  207. /// <summary>
  208. /// 设置定义8个小矩形的范围
  209. /// </summary>
  210. void SetRectangles()
  211. {
  212. //左上
  213. smallRects[0] = new Rectangle(new Point(0, 0), square);
  214. //右上
  215. smallRects[1] = new Rectangle(new Point(this.Width - square.Width - 1, 0), square);
  216. //左下
  217. smallRects[2] = new Rectangle(new Point(0, this.Height - square.Height - 1), square);
  218. //右下
  219. smallRects[3] = new Rectangle(new Point(this.Width - square.Width - 1, this.Height - square.Height - 1), square);
  220. //上中
  221. smallRects[4] = new Rectangle(new Point(this.Width / 2 - 1, 0), square);
  222. //下中
  223. smallRects[5] = new Rectangle(new Point(this.Width / 2 - 1, this.Height - square.Height - 1), square);
  224. //左中
  225. smallRects[6] = new Rectangle(new Point(0, this.Height / 2 - 1), square);
  226. //右中
  227. smallRects[7] = new Rectangle(new Point(square.Width + baseControl.Width + 1, this.Height / 2 - 1), square);
  228. //四条边线
  229. //左上
  230. linePoints[0] = new Point(square.Width / 2, square.Height / 2);
  231. //右上
  232. linePoints[1] = new Point(this.Width - square.Width / 2 - 1, square.Height / 2);
  233. //右下
  234. linePoints[2] = new Point(this.Width - square.Width / 2 - 1, this.Height - square.Height / 2);
  235. //左下
  236. linePoints[3] = new Point(square.Width / 2, this.Height - square.Height / 2 - 1);
  237. //左上
  238. linePoints[4] = new Point(square.Width / 2, square.Height / 2);
  239. //整个包括周围边框的范围
  240. ControlRect = new Rectangle(new Point(0, 0), this.Bounds.Size);
  241. }
  242. /// <summary>
  243. /// 设置边框控件可视区域
  244. /// </summary>
  245. /// <returns></returns>
  246. private GraphicsPath BuildFrame()
  247. {
  248. GraphicsPath path = new GraphicsPath();
  249. //上边框
  250. sideRects[0] = new Rectangle(0, 0, this.Width - square.Width - 1, square.Height + 1);
  251. //左边框
  252. sideRects[1] = new Rectangle(0, square.Height + 1, square.Width + 1, this.Height - square.Height - 1);
  253. //下边框
  254. sideRects[2] = new Rectangle(square.Width + 1, this.Height - square.Height - 1, this.Width - square.Width - 1, square.Height + 1);
  255. //右边框
  256. sideRects[3] = new Rectangle(this.Width - square.Width - 1, 0, square.Width + 1, this.Height - square.Height - 1);
  257. path.AddRectangle(sideRects[0]);
  258. path.AddRectangle(sideRects[1]);
  259. path.AddRectangle(sideRects[2]);
  260. path.AddRectangle(sideRects[3]);
  261. return path;
  262. }
  263. #endregion
  264. /// <summary>
  265. /// 绘图
  266. /// </summary>
  267. public void Draw()
  268. {
  269. this.BringToFront();
  270. //g.FillRectangles(Brushes.LightGray, sideRects); //填充四条边框的内部
  271. Pen pen = new Pen(Color.Black);
  272. pen.DashStyle = DashStyle.Dot;//设置为虚线,用虚线画四边,模拟微软效果
  273. g.DrawLines(pen, linePoints);//绘制四条边线
  274. g.FillRectangles(Brushes.White, smallRects); //填充8个小矩形的内部
  275. foreach (Rectangle smallRect in smallRects)
  276. {
  277. g.DrawEllipse(Pens.Black, smallRect); //绘制8个小椭圆
  278. }
  279. //g.DrawRectangles(Pens.Black, smallRects); //绘制8个小矩形的黑色边线
  280. }
  281. /// <summary>
  282. /// 设置光标状态
  283. /// </summary>
  284. public bool SetCursorShape(int x, int y)
  285. {
  286. Point point = new Point(x, y);
  287. if (!ControlRect.Contains(point))
  288. {
  289. Cursor.Current = Cursors.Arrow;
  290. return false;
  291. }
  292. else if (smallRects[0].Contains(point))
  293. {
  294. Cursor.Current = Cursors.SizeNWSE;
  295. mpoc = MousePosOnCtrl.TOPLEFT;
  296. }
  297. else if (smallRects[1].Contains(point))
  298. {
  299. Cursor.Current = Cursors.SizeNESW;
  300. mpoc = MousePosOnCtrl.TOPRIGHT;
  301. }
  302. else if (smallRects[2].Contains(point))
  303. {
  304. Cursor.Current = Cursors.SizeNESW;
  305. mpoc = MousePosOnCtrl.BOTTOMLEFT;
  306. }
  307. else if (smallRects[3].Contains(point))
  308. {
  309. Cursor.Current = Cursors.SizeNWSE;
  310. mpoc = MousePosOnCtrl.BOTTOMRIGHT;
  311. }
  312. else if (sideRects[0].Contains(point))
  313. {
  314. Cursor.Current = Cursors.SizeNS;
  315. mpoc = MousePosOnCtrl.TOP;
  316. }
  317. else if (sideRects[1].Contains(point))
  318. {
  319. Cursor.Current = Cursors.SizeWE;
  320. mpoc = MousePosOnCtrl.LEFT;
  321. }
  322. else if (sideRects[2].Contains(point))
  323. {
  324. Cursor.Current = Cursors.SizeNS;
  325. mpoc = MousePosOnCtrl.BOTTOM;
  326. }
  327. else if (sideRects[3].Contains(point))
  328. {
  329. Cursor.Current = Cursors.SizeWE;
  330. mpoc = MousePosOnCtrl.RIGHT;
  331. }
  332. else
  333. {
  334. Cursor.Current = Cursors.Arrow;
  335. }
  336. return true;
  337. }
  338. /// <summary>
  339. /// 控件移动
  340. /// </summary>
  341. private void ControlMove()
  342. {
  343. cPoint = Cursor.Position;
  344. int x = cPoint.X - pPoint.X;
  345. int y = cPoint.Y - pPoint.Y;
  346. switch (this.mpoc)
  347. {
  348. case MousePosOnCtrl.TOP:
  349. if (baseControl.Height - y > MinHeight)
  350. {
  351. baseControl.Top += y;
  352. baseControl.Height -= y;
  353. }
  354. else
  355. {
  356. baseControl.Top -= MinHeight - baseControl.Height;
  357. baseControl.Height = MinHeight;
  358. }
  359. break;
  360. case MousePosOnCtrl.BOTTOM:
  361. if (baseControl.Height + y > MinHeight)
  362. {
  363. baseControl.Height += y;
  364. }
  365. else
  366. {
  367. baseControl.Height = MinHeight;
  368. }
  369. break;
  370. case MousePosOnCtrl.LEFT:
  371. if (baseControl.Width - x > MinWidth)
  372. {
  373. baseControl.Left += x;
  374. baseControl.Width -= x;
  375. }
  376. else
  377. {
  378. baseControl.Left -= MinWidth - baseControl.Width;
  379. baseControl.Width = MinWidth;
  380. }
  381. break;
  382. case MousePosOnCtrl.RIGHT:
  383. if (baseControl.Width + x > MinWidth)
  384. {
  385. baseControl.Width += x;
  386. }
  387. else
  388. {
  389. baseControl.Width = MinWidth;
  390. }
  391. break;
  392. case MousePosOnCtrl.TOPLEFT:
  393. if (baseControl.Height - y > MinHeight)
  394. {
  395. baseControl.Top += y;
  396. baseControl.Height -= y;
  397. }
  398. else
  399. {
  400. baseControl.Top -= MinHeight - baseControl.Height;
  401. baseControl.Height = MinHeight;
  402. }
  403. if (baseControl.Width - x > MinWidth)
  404. {
  405. baseControl.Left += x;
  406. baseControl.Width -= x;
  407. }
  408. else
  409. {
  410. baseControl.Left -= MinWidth - baseControl.Width;
  411. baseControl.Width = MinWidth;
  412. }
  413. break;
  414. case MousePosOnCtrl.TOPRIGHT:
  415. if (baseControl.Height - y > MinHeight)
  416. {
  417. baseControl.Top += y;
  418. baseControl.Height -= y;
  419. }
  420. else
  421. {
  422. baseControl.Top -= MinHeight - baseControl.Height;
  423. baseControl.Height = MinHeight;
  424. }
  425. if (baseControl.Width + x > MinWidth)
  426. {
  427. baseControl.Width += x;
  428. }
  429. else
  430. {
  431. baseControl.Width = MinWidth;
  432. }
  433. break;
  434. case MousePosOnCtrl.BOTTOMLEFT:
  435. if (baseControl.Height + y > MinHeight)
  436. {
  437. baseControl.Height += y;
  438. }
  439. else
  440. {
  441. baseControl.Height = MinHeight;
  442. }
  443. if (baseControl.Width - x > MinWidth)
  444. {
  445. baseControl.Left += x;
  446. baseControl.Width -= x;
  447. }
  448. else
  449. {
  450. baseControl.Left -= MinWidth - baseControl.Width;
  451. baseControl.Width = MinWidth;
  452. }
  453. break;
  454. case MousePosOnCtrl.BOTTOMRIGHT:
  455. if (baseControl.Height + y > MinHeight)
  456. {
  457. baseControl.Height += y;
  458. }
  459. else
  460. {
  461. baseControl.Height = MinHeight;
  462. }
  463. if (baseControl.Width + x > MinWidth)
  464. {
  465. baseControl.Width += x;
  466. }
  467. else
  468. {
  469. baseControl.Width = MinWidth;
  470. }
  471. break;
  472. }
  473. pPoint = Cursor.Position;
  474. }
  475. #endregion
  476. #region Events
  477. /// <summary>
  478. /// 鼠标按下事件:记录当前鼠标相对窗体的坐标
  479. /// </summary>
  480. void FrameControl_MouseDown(object sender, MouseEventArgs e)
  481. {
  482. pPoint = Cursor.Position;
  483. }
  484. /// <summary>
  485. /// 鼠标移动事件:让控件跟着鼠标移动
  486. /// </summary>
  487. void FrameControl_MouseMove(object sender, MouseEventArgs e)
  488. {
  489. if (e.Button == MouseButtons.Left)
  490. {
  491. this.Visible = false;
  492. MoveControl.DrawDragBound(baseControl);
  493. ControlMove();
  494. }
  495. else
  496. {
  497. this.Visible = true;
  498. SetCursorShape(e.X, e.Y); //更新鼠标指针样式
  499. }
  500. }
  501. /// <summary>
  502. /// 鼠标弹起事件:让自定义的边框出现
  503. /// </summary>
  504. void FrameControl_MouseUp(object sender, MouseEventArgs e)
  505. {
  506. this.baseControl.Refresh(); //刷掉黑色边框
  507. this.Visible = true;
  508. CreateBounds();
  509. Draw();
  510. }
  511. #endregion
  512. }
  513. }