/r&d/UserRectDemo/UserRect.cs

http://freepresenter.codeplex.com · C# · 295 lines · 243 code · 46 blank · 6 comment · 20 complexity · cbacaf687dbbc11ede31a5a81d65c969 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. using System.Windows.Forms;
  7. namespace UserRectDemo
  8. {
  9. public class UserRect
  10. {
  11. private PictureBox mPictureBox;
  12. public Rectangle rect;
  13. public bool allowDeformingDuringMovement=false ;
  14. private bool mIsClick=false;
  15. private bool mMove=false;
  16. private int oldX;
  17. private int oldY;
  18. private int sizeNodeRect= 5;
  19. private Bitmap mBmp=null;
  20. private PosSizableRect nodeSelected = PosSizableRect.None;
  21. private int angle = 30;
  22. private enum PosSizableRect
  23. {
  24. UpMiddle,
  25. LeftMiddle,
  26. LeftBottom,
  27. LeftUp,
  28. RightUp,
  29. RightMiddle,
  30. RightBottom,
  31. BottomMiddle,
  32. None
  33. };
  34. public UserRect(Rectangle r)
  35. {
  36. rect = r;
  37. mIsClick = false;
  38. }
  39. public void Draw(Graphics g)
  40. {
  41. g.DrawRectangle(new Pen(Color.Red),rect);
  42. foreach (PosSizableRect pos in Enum.GetValues(typeof(PosSizableRect)))
  43. {
  44. g.DrawRectangle(new Pen(Color.Red),GetRect(pos));
  45. }
  46. }
  47. public void SetBitmapFile(string filename)
  48. {
  49. this.mBmp = new Bitmap(filename);
  50. }
  51. public void SetBitmap(Bitmap bmp)
  52. {
  53. this.mBmp = bmp;
  54. }
  55. public void SetPictureBox(PictureBox p)
  56. {
  57. this.mPictureBox = p;
  58. mPictureBox.MouseDown +=new MouseEventHandler(mPictureBox_MouseDown);
  59. mPictureBox.MouseUp += new MouseEventHandler(mPictureBox_MouseUp);
  60. mPictureBox.MouseMove += new MouseEventHandler(mPictureBox_MouseMove);
  61. mPictureBox.Paint += new PaintEventHandler(mPictureBox_Paint);
  62. }
  63. private void mPictureBox_Paint(object sender, PaintEventArgs e)
  64. {
  65. try
  66. {
  67. Draw(e.Graphics);
  68. }
  69. catch (Exception exp)
  70. {
  71. System.Console.WriteLine(exp.Message);
  72. }
  73. }
  74. private void mPictureBox_MouseDown(object sender, MouseEventArgs e)
  75. {
  76. mIsClick = true;
  77. nodeSelected = PosSizableRect.None;
  78. nodeSelected = GetNodeSelectable(e.Location);
  79. if (rect.Contains(new Point(e.X,e.Y)))
  80. {
  81. mMove = true;
  82. }
  83. oldX = e.X;
  84. oldY = e.Y;
  85. }
  86. private void mPictureBox_MouseUp(object sender, MouseEventArgs e)
  87. {
  88. mIsClick = false;
  89. mMove = false;
  90. }
  91. private void mPictureBox_MouseMove(object sender, MouseEventArgs e)
  92. {
  93. ChangeCursor(e.Location);
  94. if (mIsClick == false)
  95. {
  96. return;
  97. }
  98. Rectangle backupRect = rect;
  99. switch (nodeSelected)
  100. {
  101. case PosSizableRect.LeftUp:
  102. rect.X += e.X - oldX;
  103. rect.Width -= e.X - oldX;
  104. rect.Y += e.Y - oldY;
  105. rect.Height -= e.Y - oldY;
  106. break;
  107. case PosSizableRect.LeftMiddle:
  108. rect.X += e.X - oldX;
  109. rect.Width -= e.X - oldX;
  110. break;
  111. case PosSizableRect.LeftBottom:
  112. rect.Width -= e.X - oldX;
  113. rect.X += e.X - oldX;
  114. rect.Height += e.Y - oldY;
  115. break;
  116. case PosSizableRect.BottomMiddle:
  117. rect.Height += e.Y - oldY;
  118. break;
  119. case PosSizableRect.RightUp:
  120. rect.Width += e.X - oldX;
  121. rect.Y += e.Y - oldY;
  122. rect.Height -= e.Y - oldY;
  123. break;
  124. case PosSizableRect.RightBottom:
  125. rect.Width += e.X - oldX;
  126. rect.Height += e.Y - oldY;
  127. break;
  128. case PosSizableRect.RightMiddle:
  129. rect.Width += e.X - oldX;
  130. break;
  131. case PosSizableRect.UpMiddle:
  132. rect.Y += e.Y - oldY;
  133. rect.Height -= e.Y - oldY;
  134. break;
  135. default:
  136. if (mMove)
  137. {
  138. rect.X = rect.X + e.X - oldX;
  139. rect.Y = rect.Y + e.Y - oldY;
  140. }
  141. break;
  142. }
  143. oldX = e.X;
  144. oldY = e.Y;
  145. if (rect.Width < 5 || rect.Height < 5)
  146. {
  147. rect = backupRect;
  148. }
  149. TestIfRectInsideArea();
  150. mPictureBox.Invalidate();
  151. }
  152. private void TestIfRectInsideArea()
  153. {
  154. // Test if rectangle still inside the area.
  155. if (rect.X < 0) rect.X = 0;
  156. if (rect.Y < 0) rect.Y = 0;
  157. if (rect.Width <= 0) rect.Width = 1;
  158. if (rect.Height <= 0) rect.Height = 1;
  159. if (rect.X + rect.Width > mPictureBox.Width)
  160. {
  161. rect.Width = mPictureBox.Width - rect.X - 1; // -1 to be still show
  162. if (allowDeformingDuringMovement == false)
  163. {
  164. mIsClick = false;
  165. }
  166. }
  167. if (rect.Y + rect.Height > mPictureBox.Height)
  168. {
  169. rect.Height = mPictureBox.Height - rect.Y - 1;// -1 to be still show
  170. if (allowDeformingDuringMovement == false)
  171. {
  172. mIsClick = false;
  173. }
  174. }
  175. }
  176. private Rectangle CreateRectSizableNode(int x, int y)
  177. {
  178. return new Rectangle(x - sizeNodeRect / 2, y - sizeNodeRect / 2, sizeNodeRect, sizeNodeRect);
  179. }
  180. private Rectangle GetRect(PosSizableRect p)
  181. {
  182. switch (p)
  183. {
  184. case PosSizableRect.LeftUp:
  185. return CreateRectSizableNode(rect.X, rect.Y);
  186. case PosSizableRect.LeftMiddle:
  187. return CreateRectSizableNode(rect.X, rect.Y + +rect.Height / 2);
  188. case PosSizableRect.LeftBottom:
  189. return CreateRectSizableNode(rect.X, rect.Y +rect.Height);
  190. case PosSizableRect.BottomMiddle:
  191. return CreateRectSizableNode(rect.X + rect.Width / 2,rect.Y + rect.Height);
  192. case PosSizableRect.RightUp:
  193. return CreateRectSizableNode(rect.X + rect.Width,rect.Y );
  194. case PosSizableRect.RightBottom:
  195. return CreateRectSizableNode(rect.X + rect.Width,rect.Y + rect.Height);
  196. case PosSizableRect.RightMiddle:
  197. return CreateRectSizableNode(rect.X + rect.Width, rect.Y + rect.Height / 2);
  198. case PosSizableRect.UpMiddle:
  199. return CreateRectSizableNode(rect.X + rect.Width/2, rect.Y);
  200. default :
  201. return new Rectangle();
  202. }
  203. }
  204. private PosSizableRect GetNodeSelectable(Point p)
  205. {
  206. foreach (PosSizableRect r in Enum.GetValues(typeof(PosSizableRect)))
  207. {
  208. if (GetRect(r).Contains(p))
  209. {
  210. return r;
  211. }
  212. }
  213. return PosSizableRect.None;
  214. }
  215. private void ChangeCursor(Point p)
  216. {
  217. mPictureBox.Cursor = GetCursor(GetNodeSelectable(p));
  218. }
  219. /// <summary>
  220. /// Get cursor for the handle
  221. /// </summary>
  222. /// <param name="p"></param>
  223. /// <returns></returns>
  224. private Cursor GetCursor(PosSizableRect p)
  225. {
  226. switch (p)
  227. {
  228. case PosSizableRect.LeftUp:
  229. return Cursors.SizeNWSE;
  230. case PosSizableRect.LeftMiddle:
  231. return Cursors.SizeWE;
  232. case PosSizableRect.LeftBottom:
  233. return Cursors.SizeNESW;
  234. case PosSizableRect.BottomMiddle:
  235. return Cursors.SizeNS;
  236. case PosSizableRect.RightUp:
  237. return Cursors.SizeNESW;
  238. case PosSizableRect.RightBottom:
  239. return Cursors.SizeNWSE;
  240. case PosSizableRect.RightMiddle:
  241. return Cursors.SizeWE;
  242. case PosSizableRect.UpMiddle:
  243. return Cursors.SizeNS;
  244. default:
  245. return Cursors.Default;
  246. }
  247. }
  248. }
  249. }