| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
-
- using System;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Windows.Forms;
- namespace Dongke.IBOSS.Basics.FlowSetting
- {
- /// <summary>
- /// 画布容器(画板)
- /// </summary>
- internal partial class CanvasBox : UserControl
- {
- #region 成员变量
- /// <summary>
- /// 画布图片
- /// </summary>
- private Bitmap _canvasImage = null;
- /// <summary>
- /// 画布
- /// </summary>
- private Graphics _canvasGraphics = null;
- #endregion 成员变量
- #region 构造函数
- /// <summary>
- /// 构造函数
- /// </summary>
- public CanvasBox()
- {
- this.InitializeComponent();
- this.AutoScroll = true;
- this.flowCanvas.BackColor = System.Drawing.Color.White;
- this.txtRBFocus.ReadOnly = true;
- this.txtRBFocus.Size = Size.Empty;
- this.ClientSizeChanged += this.CanvasBox_ClientSizeChanged;
- this.MouseDown += this.CanvasBox_MouseDown;
- this.GotFocus += this.CanvasBox_GotFocus;
- this.flowCanvas.GotFocus += this.CanvasBox_GotFocus;
- this.flowCanvas.MouseDown += this.CanvasBox_MouseDown;
- this.flowCanvas.SizeChanged += this.flowCanvas_SizeChanged;
- }
- #endregion 构造函数
- #region 属性
- /// <summary>
- /// 获取处理键盘事件的控件
- /// </summary>
- public Control KeyControl
- {
- get
- {
- return this.txtRBFocus;
- }
- }
- /// <summary>
- /// 获取画布控件
- /// </summary>
- public Control CanvasControl
- {
- get
- {
- return this.flowCanvas;
- }
- }
- /// <summary>
- /// 获取画布尺寸
- /// </summary>
- public Size CanvasSize
- {
- get
- {
- return this.flowCanvas.Size;
- }
- }
- /// <summary>
- /// 获取画布图片
- /// </summary>
- public Bitmap CanvasImage
- {
- get
- {
- if (this._canvasImage == null)
- {
- this._canvasImage = new Bitmap(this.flowCanvas.Size.Width, this.flowCanvas.Size.Height);
- }
- return this._canvasImage;
- }
- }
- /// <summary>
- /// 获取画布
- /// </summary>
- public Graphics Canvas
- {
- get
- {
- if (this._canvasGraphics == null)
- {
- if (this._canvasImage == null)
- {
- this._canvasImage = new Bitmap(this.flowCanvas.Size.Width, this.flowCanvas.Size.Height);
- }
- this._canvasGraphics = this.GetGraphicsFromImage(this._canvasImage);
- }
- return this._canvasGraphics;
- }
- }
- /// <summary>
- /// 获取或设置画布背景
- /// </summary>
- public Image CanvasBackgroundImage
- {
- get
- {
- return this.flowCanvas.BackgroundImage;
- }
- set
- {
- this.flowCanvas.BackgroundImage = value;
- }
- }
- #endregion 属性
- #region 事件处理
- /// <summary>
- /// 改变画布大小
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void flowCanvas_SizeChanged(object sender, EventArgs e)
- {
- // TODO 优化 = null ,在使用时,会重新赋值。但是大小改变后要调用RefreshItems,要不是空白画布。
- //Bitmap bitmap = null;
- //if (this._canvasImage == null)
- //{
- // bitmap = new Bitmap(this.flowCanvas.Size.Width, this.flowCanvas.Size.Height);
- //}
- //else
- //{
- // bitmap = new Bitmap(this._canvasImage, this.flowCanvas.Size);
- //}
- //this._canvasImage = bitmap;
- //this._canvasGraphics = this.GetGraphicsFromImage(this._canvasImage);
- this._canvasImage = null;
- this._canvasGraphics = null;
- }
- /// <summary>
- /// 画板改变大小
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void CanvasBox_ClientSizeChanged(object sender, EventArgs e)
- {
- this.SetCanvasLocation();
- }
- /// <summary>
- /// 获取焦点
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void CanvasBox_MouseDown(object sender, MouseEventArgs e)
- {
- this.txtRBFocus.Focus();
- }
- /// <summary>
- /// 获取焦点
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void CanvasBox_GotFocus(object sender, EventArgs e)
- {
- this.txtRBFocus.Focus();
- }
- #endregion 事件处理
- #region 公有方法
- /// <summary>
- /// 设置画布尺寸
- /// </summary>
- /// <param name="value">画布尺寸</param>
- public void SetCanvasSize(Size value)
- {
- if (value.IsEmpty)
- {
- this.flowCanvas.Size = Consts.CANVAS_SIZE_MIN;
- }
- else
- {
- this.flowCanvas.Size = new Size(
- Math.Max(value.Width, Consts.CANVAS_SIZE_MIN.Width),
- Math.Max(value.Height, Consts.CANVAS_SIZE_MIN.Height));
- }
- this.SetCanvasLocation();
- }
- #endregion 公有方法
- #region 内部方法
- /// <summary>
- /// 设置画布呈现质量
- /// </summary>
- /// <param name="graphics"></param>
- internal void SetSmoothingMode(Graphics graphics)
- {
- graphics.SmoothingMode = SmoothingMode.HighQuality;
- }
- /// <summary>
- /// 设置Item的图片
- /// </summary>
- /// <param name="image">图片</param>
- internal void SetItemsImage(Image image)
- {
- this.flowCanvas.Image = image;
- }
- #endregion 内部方法
- #region 受保护方法
- /// <summary>
- /// 显示获得焦点的控件
- /// </summary>
- /// <param name="activeControl"></param>
- /// <returns></returns>
- protected override Point ScrollToControl(Control activeControl)
- {
- return this.DisplayRectangle.Location;
- }
- #endregion 受保护方法
- #region 私有方法
- /// <summary>
- /// 设置画布大小
- /// </summary>
- private void SetCanvasLocation()
- {
- int x = this.GetCanvasLeft();
- int y = this.GetCanvasTop();
- this.flowCanvas.Location = new Point(x, y);
- this.txtRBFocus.Location = new Point(
- this.flowCanvas.Right + Consts.CANVAS_MARGIN,
- this.flowCanvas.Bottom + Consts.CANVAS_MARGIN);
- //this.AutoScroll = true;
- }
- /// <summary>
- /// 获取画布位置Y
- /// </summary>
- /// <returns>位置Y</returns>
- private int GetCanvasTop()
- {
- int margin = this.ClientSize.Height
- - this.flowCanvas.Height
- - Consts.CANVAS_MARGIN
- - Consts.CANVAS_MARGIN;
- if (0 < margin)
- {
- return Consts.CANVAS_MARGIN + (margin) / 2 + this.lblLeftTop.Top;
- }
- else
- {
- return Consts.CANVAS_MARGIN + this.lblLeftTop.Top;
- }
- }
- /// <summary>
- /// 获取画布位置X
- /// </summary>
- /// <returns>位置X</returns>
- private int GetCanvasLeft()
- {
- int margin = this.ClientSize.Width
- - this.flowCanvas.Width
- - Consts.CANVAS_MARGIN
- - Consts.CANVAS_MARGIN;
- if (0 < margin)
- {
- return Consts.CANVAS_MARGIN + (margin) / 2 + this.lblLeftTop.Left;
- }
- else
- {
- return Consts.CANVAS_MARGIN + this.lblLeftTop.Left;
- }
- }
- /// <summary>
- /// 从图片获得Graphics
- /// </summary>
- /// <param name="image">片</param>
- /// <returns>Graphics</returns>
- private Graphics GetGraphicsFromImage(Image image)
- {
- Graphics g = Graphics.FromImage(image);
- this.SetSmoothingMode(g);
- return g;
- }
- #endregion 私有方法
- }
- }
|