CanvasBox.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. 
  2. using System;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Windows.Forms;
  6. namespace Dongke.IBOSS.Basics.FlowSetting
  7. {
  8. /// <summary>
  9. /// 画布容器(画板)
  10. /// </summary>
  11. internal partial class CanvasBox : UserControl
  12. {
  13. #region 成员变量
  14. /// <summary>
  15. /// 画布图片
  16. /// </summary>
  17. private Bitmap _canvasImage = null;
  18. /// <summary>
  19. /// 画布
  20. /// </summary>
  21. private Graphics _canvasGraphics = null;
  22. #endregion 成员变量
  23. #region 构造函数
  24. /// <summary>
  25. /// 构造函数
  26. /// </summary>
  27. public CanvasBox()
  28. {
  29. this.InitializeComponent();
  30. this.AutoScroll = true;
  31. this.flowCanvas.BackColor = System.Drawing.Color.White;
  32. this.txtRBFocus.ReadOnly = true;
  33. this.txtRBFocus.Size = Size.Empty;
  34. this.ClientSizeChanged += this.CanvasBox_ClientSizeChanged;
  35. this.MouseDown += this.CanvasBox_MouseDown;
  36. this.GotFocus += this.CanvasBox_GotFocus;
  37. this.flowCanvas.GotFocus += this.CanvasBox_GotFocus;
  38. this.flowCanvas.MouseDown += this.CanvasBox_MouseDown;
  39. this.flowCanvas.SizeChanged += this.flowCanvas_SizeChanged;
  40. }
  41. #endregion 构造函数
  42. #region 属性
  43. /// <summary>
  44. /// 获取处理键盘事件的控件
  45. /// </summary>
  46. public Control KeyControl
  47. {
  48. get
  49. {
  50. return this.txtRBFocus;
  51. }
  52. }
  53. /// <summary>
  54. /// 获取画布控件
  55. /// </summary>
  56. public Control CanvasControl
  57. {
  58. get
  59. {
  60. return this.flowCanvas;
  61. }
  62. }
  63. /// <summary>
  64. /// 获取画布尺寸
  65. /// </summary>
  66. public Size CanvasSize
  67. {
  68. get
  69. {
  70. return this.flowCanvas.Size;
  71. }
  72. }
  73. /// <summary>
  74. /// 获取画布图片
  75. /// </summary>
  76. public Bitmap CanvasImage
  77. {
  78. get
  79. {
  80. if (this._canvasImage == null)
  81. {
  82. this._canvasImage = new Bitmap(this.flowCanvas.Size.Width, this.flowCanvas.Size.Height);
  83. }
  84. return this._canvasImage;
  85. }
  86. }
  87. /// <summary>
  88. /// 获取画布
  89. /// </summary>
  90. public Graphics Canvas
  91. {
  92. get
  93. {
  94. if (this._canvasGraphics == null)
  95. {
  96. if (this._canvasImage == null)
  97. {
  98. this._canvasImage = new Bitmap(this.flowCanvas.Size.Width, this.flowCanvas.Size.Height);
  99. }
  100. this._canvasGraphics = this.GetGraphicsFromImage(this._canvasImage);
  101. }
  102. return this._canvasGraphics;
  103. }
  104. }
  105. /// <summary>
  106. /// 获取或设置画布背景
  107. /// </summary>
  108. public Image CanvasBackgroundImage
  109. {
  110. get
  111. {
  112. return this.flowCanvas.BackgroundImage;
  113. }
  114. set
  115. {
  116. this.flowCanvas.BackgroundImage = value;
  117. }
  118. }
  119. #endregion 属性
  120. #region 事件处理
  121. /// <summary>
  122. /// 改变画布大小
  123. /// </summary>
  124. /// <param name="sender"></param>
  125. /// <param name="e"></param>
  126. private void flowCanvas_SizeChanged(object sender, EventArgs e)
  127. {
  128. // TODO 优化 = null ,在使用时,会重新赋值。但是大小改变后要调用RefreshItems,要不是空白画布。
  129. //Bitmap bitmap = null;
  130. //if (this._canvasImage == null)
  131. //{
  132. // bitmap = new Bitmap(this.flowCanvas.Size.Width, this.flowCanvas.Size.Height);
  133. //}
  134. //else
  135. //{
  136. // bitmap = new Bitmap(this._canvasImage, this.flowCanvas.Size);
  137. //}
  138. //this._canvasImage = bitmap;
  139. //this._canvasGraphics = this.GetGraphicsFromImage(this._canvasImage);
  140. this._canvasImage = null;
  141. this._canvasGraphics = null;
  142. }
  143. /// <summary>
  144. /// 画板改变大小
  145. /// </summary>
  146. /// <param name="sender"></param>
  147. /// <param name="e"></param>
  148. private void CanvasBox_ClientSizeChanged(object sender, EventArgs e)
  149. {
  150. this.SetCanvasLocation();
  151. }
  152. /// <summary>
  153. /// 获取焦点
  154. /// </summary>
  155. /// <param name="sender"></param>
  156. /// <param name="e"></param>
  157. private void CanvasBox_MouseDown(object sender, MouseEventArgs e)
  158. {
  159. this.txtRBFocus.Focus();
  160. }
  161. /// <summary>
  162. /// 获取焦点
  163. /// </summary>
  164. /// <param name="sender"></param>
  165. /// <param name="e"></param>
  166. private void CanvasBox_GotFocus(object sender, EventArgs e)
  167. {
  168. this.txtRBFocus.Focus();
  169. }
  170. #endregion 事件处理
  171. #region 公有方法
  172. /// <summary>
  173. /// 设置画布尺寸
  174. /// </summary>
  175. /// <param name="value">画布尺寸</param>
  176. public void SetCanvasSize(Size value)
  177. {
  178. if (value.IsEmpty)
  179. {
  180. this.flowCanvas.Size = Consts.CANVAS_SIZE_MIN;
  181. }
  182. else
  183. {
  184. this.flowCanvas.Size = new Size(
  185. Math.Max(value.Width, Consts.CANVAS_SIZE_MIN.Width),
  186. Math.Max(value.Height, Consts.CANVAS_SIZE_MIN.Height));
  187. }
  188. this.SetCanvasLocation();
  189. }
  190. #endregion 公有方法
  191. #region 内部方法
  192. /// <summary>
  193. /// 设置画布呈现质量
  194. /// </summary>
  195. /// <param name="graphics"></param>
  196. internal void SetSmoothingMode(Graphics graphics)
  197. {
  198. graphics.SmoothingMode = SmoothingMode.HighQuality;
  199. }
  200. /// <summary>
  201. /// 设置Item的图片
  202. /// </summary>
  203. /// <param name="image">图片</param>
  204. internal void SetItemsImage(Image image)
  205. {
  206. this.flowCanvas.Image = image;
  207. }
  208. #endregion 内部方法
  209. #region 受保护方法
  210. /// <summary>
  211. /// 显示获得焦点的控件
  212. /// </summary>
  213. /// <param name="activeControl"></param>
  214. /// <returns></returns>
  215. protected override Point ScrollToControl(Control activeControl)
  216. {
  217. return this.DisplayRectangle.Location;
  218. }
  219. #endregion 受保护方法
  220. #region 私有方法
  221. /// <summary>
  222. /// 设置画布大小
  223. /// </summary>
  224. private void SetCanvasLocation()
  225. {
  226. int x = this.GetCanvasLeft();
  227. int y = this.GetCanvasTop();
  228. this.flowCanvas.Location = new Point(x, y);
  229. this.txtRBFocus.Location = new Point(
  230. this.flowCanvas.Right + Consts.CANVAS_MARGIN,
  231. this.flowCanvas.Bottom + Consts.CANVAS_MARGIN);
  232. //this.AutoScroll = true;
  233. }
  234. /// <summary>
  235. /// 获取画布位置Y
  236. /// </summary>
  237. /// <returns>位置Y</returns>
  238. private int GetCanvasTop()
  239. {
  240. int margin = this.ClientSize.Height
  241. - this.flowCanvas.Height
  242. - Consts.CANVAS_MARGIN
  243. - Consts.CANVAS_MARGIN;
  244. if (0 < margin)
  245. {
  246. return Consts.CANVAS_MARGIN + (margin) / 2 + this.lblLeftTop.Top;
  247. }
  248. else
  249. {
  250. return Consts.CANVAS_MARGIN + this.lblLeftTop.Top;
  251. }
  252. }
  253. /// <summary>
  254. /// 获取画布位置X
  255. /// </summary>
  256. /// <returns>位置X</returns>
  257. private int GetCanvasLeft()
  258. {
  259. int margin = this.ClientSize.Width
  260. - this.flowCanvas.Width
  261. - Consts.CANVAS_MARGIN
  262. - Consts.CANVAS_MARGIN;
  263. if (0 < margin)
  264. {
  265. return Consts.CANVAS_MARGIN + (margin) / 2 + this.lblLeftTop.Left;
  266. }
  267. else
  268. {
  269. return Consts.CANVAS_MARGIN + this.lblLeftTop.Left;
  270. }
  271. }
  272. /// <summary>
  273. /// 从图片获得Graphics
  274. /// </summary>
  275. /// <param name="image">片</param>
  276. /// <returns>Graphics</returns>
  277. private Graphics GetGraphicsFromImage(Image image)
  278. {
  279. Graphics g = Graphics.FromImage(image);
  280. this.SetSmoothingMode(g);
  281. return g;
  282. }
  283. #endregion 私有方法
  284. }
  285. }