using System;
|
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.Drawing;
|
using System.Data;
|
using System.Linq;
|
using System.Text;
|
using System.Windows.Forms;
|
using Eventech.DynPicture.Model;
|
using System.Threading;
|
|
namespace Eventech.DynPicture.WinFrmUI
|
{
|
/// <summary>
|
/// 拓展绘制文本,选中文本
|
/// </summary>
|
public partial class PictureEditInfo : PictureEditBasic
|
{
|
/// <summary>
|
/// 构造函数
|
/// </summary>
|
public PictureEditInfo()
|
{
|
InitializeComponent();
|
}
|
|
#region 事件
|
|
/// <summary>
|
/// 图片信息上按下鼠标时触发
|
/// </summary>
|
public event Action<IPictureBaseInfo, MouseEventArgs> ImageInfoMouseDownEvent;
|
|
/// <summary>
|
/// 图片信息点击时触发
|
/// </summary>
|
public event Action<IPictureBaseInfo, MouseEventArgs> ImageInfoMouseClickEvent;
|
|
/// <summary>
|
/// 图片信息双击时触发
|
/// </summary>
|
public event Action<IPictureBaseInfo, MouseEventArgs> ImageInfoMouseDoubleClickEvent;
|
|
/// <summary>
|
/// 更新图片信息事件
|
/// </summary>
|
public event Action<IPictureBaseInfo> UpdateImageInfoEvent;
|
|
#endregion
|
|
/// <summary>
|
/// 当前选中的图片信息
|
/// </summary>
|
public IPictureBaseInfo SelectedImageInfo
|
{
|
get { return _selectedImageInfo; }
|
private set{_selectedImageInfo = value;}
|
}
|
private IPictureBaseInfo _selectedImageInfo;
|
|
/// <summary>
|
/// 是否高亮显示图片信息对象
|
/// </summary>
|
public bool IsHighlightImageInfo
|
{
|
get { return _isHighlightImageInfo; }
|
set { _isHighlightImageInfo = value; }
|
}
|
private bool _isHighlightImageInfo = false;
|
|
/// <summary>
|
/// 选中图片信息时的鼠标信息
|
/// </summary>
|
protected MouseEventArgs _imageSelectedMouse;
|
|
/// <summary>
|
/// 是否显示图片信息选中外观
|
/// </summary>
|
public bool ShowSelectedAppearance
|
{
|
get { return _showSelectedAppearance; }
|
set { _showSelectedAppearance = value; }
|
}
|
private bool _showSelectedAppearance = true;
|
|
/// <summary>
|
/// 是否允许鼠标按下移动图片信息
|
/// </summary>
|
public bool AllowMouseMoveImageInfo
|
{
|
get { return _allowMouseMoveImageInfo; }
|
set { _allowMouseMoveImageInfo = value; }
|
}
|
private bool _allowMouseMoveImageInfo = false;
|
|
/// <summary>
|
/// 是否允许键盘方向键移动
|
/// </summary>
|
public bool AllowKeyDirectionMove
|
{
|
get { return _allowKeyDirectionMove; }
|
set { _allowKeyDirectionMove = value; }
|
}
|
private bool _allowKeyDirectionMove = false;
|
|
/// <summary>
|
/// 初始化显示图片
|
/// </summary>
|
/// <param name="img"></param>
|
/// <returns></returns>
|
protected override Image CreateOriginDisplayImage(Image img)
|
{
|
var cmg = base.CreateOriginDisplayImage(img);
|
if (cmg == null)
|
return null;
|
_selectedImageInfo = null;
|
DrawImageInfos(cmg, _imageInfos);
|
return cmg;
|
}
|
|
/// <summary>
|
/// 图片信息
|
/// </summary>
|
public virtual List<IPictureBaseInfo> ImageInfos
|
{
|
get { return _imageInfos; }
|
set
|
{
|
_imageInfos = value;
|
this.DisplayImage = _img;
|
}
|
}
|
private List<IPictureBaseInfo> _imageInfos = null;
|
|
|
/// <summary>
|
/// 图片信息与坐标对应关系
|
/// </summary>
|
protected Dictionary<IPictureBaseInfo, ImageInfoCoordinate> _imageInfoDics = new Dictionary<IPictureBaseInfo,ImageInfoCoordinate>();
|
|
/// <summary>
|
/// 绘制图片信息信息(用Text绘制)
|
/// </summary>
|
/// <param name="img"></param>
|
/// <param name="imageInfos"></param>
|
protected void DrawImageInfos(Image img, List<IPictureBaseInfo> imageInfos)
|
{
|
if (_imageInfoDics != null && _imageInfoDics.Count > 0)
|
_imageInfoDics.Clear();
|
if (img == null)
|
return;
|
if (imageInfos == null || imageInfos.Count < 1)
|
return;
|
using (var g = Graphics.FromImage(img))
|
{
|
var levels = imageInfos.OrderBy(x => x.Level).Select(x => x.Level).Distinct().ToList();
|
levels.ForEach(x => {
|
var levInfos = imageInfos.Where(t => t.Level == x).ToList();
|
levInfos.ForEach(y => {
|
var coor = y.GetCoordinate(g);
|
if (coor != null)
|
{
|
_imageInfoDics.Add(y,coor);
|
y.DrawInfo2(g,coor);
|
}
|
});
|
});
|
}
|
}
|
|
#region 移动图片信息
|
|
//鼠标左键是否按下图片信息
|
private bool _mouseLeftInfoDown = false;
|
//鼠标按下对应图片上的位置
|
private PointF _mouseLeftDownImagePoint;
|
//鼠标左键移动点位置
|
private Point _mouseLeftMovePoint;
|
//是否移动了图片信息
|
private bool _isImageInfoMove = false;
|
/// <summary>
|
/// 鼠标按下选中图片信息并触发ImageTextMouseDownEvent
|
/// </summary>
|
/// <param name="e"></param>
|
protected override void OnMouseDown(MouseEventArgs e)
|
{
|
SelImageInfo(e);
|
var allowMoveImage = this.AllowMouseDownMoveImage;
|
if (_selectedImageInfo != null)
|
{
|
if (this.AllowMouseMoveImageInfo)
|
{
|
this.AllowMouseDownMoveImage = false;
|
_mouseLeftInfoDown = true;
|
_mouseLeftDownImagePoint = ViewPointToImage(e.Location).Value;
|
_mouseLeftMovePoint = e.Location;
|
}
|
}
|
base.OnMouseDown(e);
|
this.AllowMouseDownMoveImage = allowMoveImage;
|
}
|
|
/// <summary>
|
/// 鼠标移动
|
/// </summary>
|
/// <param name="e"></param>
|
protected override void OnMouseMove(MouseEventArgs e)
|
{
|
base.OnMouseMove(e);
|
if (_mouseLeftInfoDown)
|
{
|
if (_desRectf.Contains(e.Location))
|
{
|
_mouseLeftMovePoint = e.Location;
|
_isImageInfoMove = true;
|
this.Invalidate();
|
}
|
}
|
}
|
|
/// <summary>
|
/// 绘制移动图片信息移动边框
|
/// </summary>
|
/// <param name="g"></param>
|
protected virtual void DrawInfoMoveBounds(Graphics g)
|
{
|
if (_selectedImageInfo == null)
|
return;
|
if (!_mouseLeftInfoDown)
|
return;
|
if (!_isImageInfoMove)
|
return;
|
var mp = ViewPointToImage(_mouseLeftMovePoint);
|
var x_add = mp.Value.X - _mouseLeftDownImagePoint.X;
|
var y_add = mp.Value.Y - _mouseLeftDownImagePoint.Y;
|
|
var coordinate = _imageInfoDics[_selectedImageInfo];
|
var points = new PointF[5];
|
points[0] = GetMoveZoomPointF(coordinate.AfterLeftTop, x_add, y_add);
|
points[1] = GetMoveZoomPointF(coordinate.AfterRightTop, x_add, y_add);
|
points[2] = GetMoveZoomPointF(coordinate.AfterRightBottom, x_add, y_add);
|
points[3] = GetMoveZoomPointF(coordinate.AfterLeftBottom, x_add, y_add);
|
points[4] = points[0];
|
using (var pen = new Pen(Color.Blue, 2f))
|
{
|
g.DrawLines(pen, points);
|
}
|
}
|
|
private PointF GetMoveZoomPointF(PointF P, float x, float y)
|
{
|
var sp = new PointF(P.X + x, P.Y + y);
|
return SourcePointToZoom(sp);
|
}
|
|
/// <summary>
|
/// 鼠标弹起
|
/// </summary>
|
/// <param name="e"></param>
|
protected override void OnMouseUp(MouseEventArgs e)
|
{
|
base.OnMouseUp(e);
|
_mouseLeftInfoDown = false;
|
if (_isImageInfoMove)
|
{
|
if (_selectedImageInfo != null)
|
{
|
var loc = _desRectf.Contains(e.Location) ? e.Location : _mouseLeftMovePoint;
|
var mp = ViewPointToImage(loc);
|
var x_add = mp.Value.X - _mouseLeftDownImagePoint.X;
|
var y_add = mp.Value.Y - _mouseLeftDownImagePoint.Y;
|
|
if (_selectedImageInfo is PictureTextInfo)
|
{
|
var textInfo = _selectedImageInfo as PictureTextInfo;
|
textInfo.Point = new PointF(textInfo.Point.X + x_add, textInfo.Point.Y + y_add);
|
}
|
else if (_selectedImageInfo is PictureImageInfo)
|
{
|
var imageInfo = _selectedImageInfo as PictureImageInfo;
|
imageInfo.Point = new PointF(imageInfo.Point.X + x_add, imageInfo.Point.Y + y_add);
|
}
|
else if (_selectedImageInfo is PictureLineInfo)
|
{
|
var lineInfo = _selectedImageInfo as PictureLineInfo;
|
lineInfo.StartPointF = new PointF(lineInfo.StartPointF.X + x_add, lineInfo.StartPointF.Y + y_add);
|
lineInfo.EndPointF = new PointF(lineInfo.EndPointF.X + x_add, lineInfo.EndPointF.Y + y_add);
|
}
|
else if (_selectedImageInfo is PictureArrowInfo)
|
{
|
var arrowInfo = _selectedImageInfo as PictureArrowInfo;
|
arrowInfo.StartPointF = new PointF(arrowInfo.StartPointF.X + x_add, arrowInfo.StartPointF.Y + y_add);
|
arrowInfo.EndPointF = new PointF(arrowInfo.EndPointF.X + x_add, arrowInfo.EndPointF.Y + y_add);
|
}
|
else if (_selectedImageInfo is PictureMarkInfo)
|
{
|
var markInfo = _selectedImageInfo as PictureMarkInfo;
|
markInfo.StartPointF = new PointF(markInfo.StartPointF.X + x_add, markInfo.StartPointF.Y + y_add);
|
markInfo.EndPointF = new PointF(markInfo.EndPointF.X + x_add, markInfo.EndPointF.Y + y_add);
|
markInfo.ExtendPointF = new PointF(markInfo.ExtendPointF.X + x_add, markInfo.ExtendPointF.Y + y_add);
|
}
|
this.RedrawImageTextInfos();
|
if (this.UpdateImageInfoEvent != null)
|
this.UpdateImageInfoEvent(_selectedImageInfo);
|
}
|
}
|
_isImageInfoMove = false;
|
}
|
|
#endregion
|
|
/// <summary>
|
/// 选择图片信息
|
/// </summary>
|
/// <param name="e"></param>
|
protected virtual void SelImageInfo(MouseEventArgs e)
|
{
|
var sel = _selectedImageInfo;
|
_selectedImageInfo = null;
|
if (_displayImage != null)
|
{
|
if (_imageInfoDics != null && _imageInfoDics.Count > 0)
|
{
|
if (_desRectf.Contains(e.Location))
|
{
|
var dp = ViewPointToDisplay(e.Location);
|
var sp = DisplayPointToSource(dp);
|
foreach (var x in _imageInfoDics)
|
{
|
var np = x.Value.Origin.RotatePointF(sp, x.Value.Angle, false);
|
if (x.Value.BeforeRectf.Contains(np))
|
{
|
_selectedImageInfo = x.Key;
|
_imageSelectedMouse = e;
|
if (this.ImageInfoMouseDownEvent != null)
|
this.ImageInfoMouseDownEvent(x.Key, e);
|
break;
|
}
|
}
|
}
|
}
|
}
|
this.Invalidate();
|
}
|
|
/// <summary>
|
/// 触发ImageInfoMouseClickEvent
|
/// </summary>
|
/// <param name="e"></param>
|
protected override void OnMouseClick(MouseEventArgs e)
|
{
|
base.OnMouseClick(e);
|
if (_selectedImageInfo != null)
|
{
|
if (this.ImageInfoMouseClickEvent != null)
|
this.ImageInfoMouseClickEvent(_selectedImageInfo, _imageSelectedMouse);
|
}
|
}
|
|
/// <summary>
|
/// 触发ImageInfoMouseDoubleClickEvent
|
/// </summary>
|
/// <param name="e"></param>
|
protected override void OnMouseDoubleClick(MouseEventArgs e)
|
{
|
base.OnMouseDoubleClick(e);
|
if (_selectedImageInfo != null)
|
{
|
if (this.ImageInfoMouseDoubleClickEvent != null)
|
this.ImageInfoMouseDoubleClickEvent(_selectedImageInfo, _imageSelectedMouse);
|
}
|
}
|
|
/// <summary>
|
/// 绘制选中的图片信息的边框
|
/// </summary>
|
/// <param name="e"></param>
|
protected override void OnPaint(PaintEventArgs e)
|
{
|
base.OnPaint(e);
|
DrawSelTextBounds(e.Graphics);
|
DrawImageTextBounds(e.Graphics);
|
DrawInfoMoveBounds(e.Graphics);
|
}
|
|
/// <summary>
|
/// 绘制选中文本边框
|
/// </summary>
|
/// <param name="g"></param>
|
protected virtual void DrawSelTextBounds(Graphics g)
|
{
|
if (_showSelectedAppearance)
|
{
|
if (_selectedImageInfo != null)
|
{
|
var coordinate = _imageInfoDics[_selectedImageInfo];
|
var points = new PointF[5];
|
points[0] = SourcePointToZoom(coordinate.AfterLeftTop);
|
points[1] = SourcePointToZoom(coordinate.AfterRightTop);
|
points[2] = SourcePointToZoom(coordinate.AfterRightBottom);
|
points[3] = SourcePointToZoom(coordinate.AfterLeftBottom);
|
points[4] = points[0];
|
using (var pen = new Pen(Color.Red, 1f))
|
{
|
g.DrawLines(pen, points);
|
}
|
}
|
}
|
}
|
|
/// <summary>
|
/// 绘制文本边框
|
/// </summary>
|
/// <param name="g"></param>
|
protected virtual void DrawImageTextBounds(Graphics g)
|
{
|
if (this.IsHighlightImageInfo)
|
{
|
if (this.ImageInfos != null && this.ImageInfos.Count > 0)
|
{
|
if (_imageInfoDics != null && _imageInfoDics.Count > 0)
|
{
|
using (var pen = new Pen(Color.Blue, 2f))
|
{
|
_imageInfoDics.ToList().ForEach(x =>
|
{
|
if (x.Key != _selectedImageInfo)
|
{
|
var coordinate = _imageInfoDics[x.Key];
|
var points = new PointF[5];
|
points[0] = SourcePointToZoom(coordinate.AfterLeftTop);
|
points[1] = SourcePointToZoom(coordinate.AfterRightTop);
|
points[2] = SourcePointToZoom(coordinate.AfterRightBottom);
|
points[3] = SourcePointToZoom(coordinate.AfterLeftBottom);
|
points[4] = points[0];
|
g.DrawLines(pen, points);
|
}
|
});
|
}
|
}
|
}
|
}
|
}
|
|
/// <summary>
|
/// 替换图片(如果尺寸相同保持状态,否则重置)
|
/// </summary>
|
/// <param name="img"></param>
|
public override void ReplaceImage(Image img)
|
{
|
if (_img != null)
|
{
|
if (_img.Size == img.Size)
|
{
|
_img = img;
|
var cimg = img.CloneC();
|
DrawImageInfos(cimg, _imageInfos);
|
RotateImage(cimg);
|
if (_displayImage != null)
|
_displayImage.Dispose();
|
_displayImage = cimg;
|
this.Invalidate();
|
return;
|
}
|
}
|
this.Image = img;
|
}
|
|
/// <summary>
|
/// 重绘图片信息信息
|
/// </summary>
|
/// <param name="imageTextInfos"></param>
|
public virtual void RedrawImageTextInfos(List<IPictureBaseInfo> imageTextInfos)
|
{
|
_imageInfos = imageTextInfos;
|
RedrawImageTextInfos();
|
this.Invalidate();
|
}
|
|
/// <summary>
|
/// 重绘显示文本
|
/// </summary>
|
/// <param name="id"></param>
|
/// <param name="text"></param>
|
public virtual void RedrawImageText(long id, string text)
|
{
|
if (_imageInfos == null)
|
return;
|
var imageInfo = _imageInfos.Find(x=>x.ID==id&&x.InfoType==ePictureInfoType.Text);
|
if (imageInfo == null)
|
return;
|
var imageText = imageInfo as PictureTextInfo;
|
imageText.Text = text;
|
RedrawImageTextInfos();
|
this.Invalidate();
|
}
|
|
|
|
/// <summary>
|
/// 重绘图片信息信息
|
/// </summary>
|
public virtual void RedrawImageTextInfos()
|
{
|
if (_img == null)
|
return;
|
//SpinWait.SpinUntil(() => !_isAsyncLoading);
|
var cimg = _img.CloneC();
|
DrawImageInfos(cimg, _imageInfos);
|
RotateImage(cimg);
|
if (_displayImage != null)
|
_displayImage.Dispose();
|
_displayImage = cimg;
|
if (_selectedImageInfo != null)
|
{
|
if (_imageInfos == null || _imageInfos.Count < 1)
|
_selectedImageInfo = null;
|
else
|
_selectedImageInfo = _imageInfos.Find(x => x.ID == _selectedImageInfo.ID);
|
}
|
this.Invalidate();
|
}
|
|
|
|
|
public void SetSelImageInfo(long id)
|
{
|
if (this.ImageInfos == null)
|
return;
|
var imageInfo = this.ImageInfos.Find(x => x.ID == id);
|
if (imageInfo == null)
|
return;
|
_selectedImageInfo = imageInfo;
|
this.Invalidate();
|
}
|
|
/// <summary>
|
/// 响应键盘事件(上下左右)
|
/// </summary>
|
/// <param name="msg"></param>
|
/// <param name="keyData"></param>
|
/// <returns></returns>
|
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
|
{
|
if (this.AllowKeyDirectionMove)
|
{
|
if (this.SelectedImageInfo != null)
|
{
|
var rotateKey = TransferRotateKey(keyData);
|
switch (rotateKey)
|
{
|
case Keys.Up:
|
this.SelectedImageInfo.IncreaseAxisValue(eAxis.Y,-1);
|
this.RedrawImageTextInfos();
|
if (this.UpdateImageInfoEvent != null)
|
this.UpdateImageInfoEvent(_selectedImageInfo);
|
return true;
|
case Keys.Down:
|
this.SelectedImageInfo.IncreaseAxisValue(eAxis.Y, 1);
|
this.RedrawImageTextInfos();
|
if (this.UpdateImageInfoEvent != null)
|
this.UpdateImageInfoEvent(_selectedImageInfo);
|
return true;
|
case Keys.Left:
|
this.SelectedImageInfo.IncreaseAxisValue(eAxis.X, -1);
|
this.RedrawImageTextInfos();
|
if (this.UpdateImageInfoEvent != null)
|
this.UpdateImageInfoEvent(_selectedImageInfo);
|
return true;
|
case Keys.Right:
|
this.SelectedImageInfo.IncreaseAxisValue(eAxis.X, 1);
|
this.RedrawImageTextInfos();
|
if (this.UpdateImageInfoEvent != null)
|
this.UpdateImageInfoEvent(_selectedImageInfo);
|
return true;
|
default: break;
|
}
|
}
|
}
|
return base.ProcessCmdKey(ref msg, keyData);
|
}
|
|
/// <summary>
|
/// 根据图片Rotate角度转化上下左右键盘
|
/// </summary>
|
/// <param name="directionKey">方向键</param>
|
/// <returns>Rotate后的方向键</returns>
|
protected virtual Keys TransferRotateKey(Keys directionKey)
|
{
|
var direcKeys = new List<Keys>() { Keys.Left, Keys.Up, Keys.Right, Keys.Down };
|
if (!direcKeys.Contains(directionKey))
|
return directionKey;
|
var angle = _displayAngle % 360;
|
var keyNumber = ((int)directionKey - (int)Keys.Left) % 4;
|
switch (angle)
|
{
|
case 270:
|
keyNumber += 1;
|
break;
|
case 180:
|
keyNumber += 2;
|
break;
|
case 90:
|
keyNumber += 3;
|
break;
|
default: break;
|
}
|
keyNumber = keyNumber % 4;
|
return (Keys)((int)Keys.Left + keyNumber);
|
}
|
|
|
}
|
}
|