using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace Dongke.IBOSS.Basics.FlowSetting
{
///
/// 画布容器(画板)
///
internal partial class CanvasBox : UserControl
{
#region 成员变量
///
/// 画布图片
///
private Bitmap _canvasImage = null;
///
/// 画布
///
private Graphics _canvasGraphics = null;
#endregion 成员变量
#region 构造函数
///
/// 构造函数
///
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 属性
///
/// 获取处理键盘事件的控件
///
public Control KeyControl
{
get
{
return this.txtRBFocus;
}
}
///
/// 获取画布控件
///
public Control CanvasControl
{
get
{
return this.flowCanvas;
}
}
///
/// 获取画布尺寸
///
public Size CanvasSize
{
get
{
return this.flowCanvas.Size;
}
}
///
/// 获取画布图片
///
public Bitmap CanvasImage
{
get
{
if (this._canvasImage == null)
{
this._canvasImage = new Bitmap(this.flowCanvas.Size.Width, this.flowCanvas.Size.Height);
}
return this._canvasImage;
}
}
///
/// 获取画布
///
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;
}
}
///
/// 获取或设置画布背景
///
public Image CanvasBackgroundImage
{
get
{
return this.flowCanvas.BackgroundImage;
}
set
{
this.flowCanvas.BackgroundImage = value;
}
}
#endregion 属性
#region 事件处理
///
/// 改变画布大小
///
///
///
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;
}
///
/// 画板改变大小
///
///
///
private void CanvasBox_ClientSizeChanged(object sender, EventArgs e)
{
this.SetCanvasLocation();
}
///
/// 获取焦点
///
///
///
private void CanvasBox_MouseDown(object sender, MouseEventArgs e)
{
this.txtRBFocus.Focus();
}
///
/// 获取焦点
///
///
///
private void CanvasBox_GotFocus(object sender, EventArgs e)
{
this.txtRBFocus.Focus();
}
#endregion 事件处理
#region 公有方法
///
/// 设置画布尺寸
///
/// 画布尺寸
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 内部方法
///
/// 设置画布呈现质量
///
///
internal void SetSmoothingMode(Graphics graphics)
{
graphics.SmoothingMode = SmoothingMode.HighQuality;
}
///
/// 设置Item的图片
///
/// 图片
internal void SetItemsImage(Image image)
{
this.flowCanvas.Image = image;
}
#endregion 内部方法
#region 受保护方法
///
/// 显示获得焦点的控件
///
///
///
protected override Point ScrollToControl(Control activeControl)
{
return this.DisplayRectangle.Location;
}
#endregion 受保护方法
#region 私有方法
///
/// 设置画布大小
///
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;
}
///
/// 获取画布位置Y
///
/// 位置Y
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;
}
}
///
/// 获取画布位置X
///
/// 位置X
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;
}
}
///
/// 从图片获得Graphics
///
/// 片
/// Graphics
private Graphics GetGraphicsFromImage(Image image)
{
Graphics g = Graphics.FromImage(image);
this.SetSmoothingMode(g);
return g;
}
#endregion 私有方法
}
}