c# 属性改变

时间:2021-09-13 12:09:02
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
using app.Viewer.ViewModels;
using Bedrock.Winform; namespace app.Viewer.Views
{
/// <summary>
/// 帧频显示控件类
/// </summary>
public partial class FrqView : PartialView
{
#region "Field"
private FrqViewModel _frqViewModel;
private SynchronizationContext _context;
#endregion #region "Constructor"
public FrqView(FrqViewModel model)
{
_frqViewModel = model;
DataContext = model;
InitializeComponent();
Dock = DockStyle.Top; _context = SynchronizationContext.Current;
_frqViewModel.PropertyChanged += _frqViewModel_PropertyChanged;
}
#endregion #region "PropertyChanged" void _frqViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case "Frq":
_context.Post(o =>
{
FRdata.Text = _frqViewModel.Frq.ToString("f1");
Refresh();
},null);
break;
}
}
#endregion
}
}
 using System.ComponentModel;
using System.Windows.Forms;
using app.Main.Graph;
using app.KeyBoards.Events; namespace app.Measure.Actions
{
/// <summary>
/// 操作接口
/// </summary>
public interface IToolAction : IActor, INotifyPropertyChanged
{
/// <summary>
/// 鼠标事件处理
/// </summary>
/// <param name="sender"></param>
/// <param name="eventArgs">鼠标事件参数</param>
void MouseEventHandle(object sender, MouseEventArgs eventArgs); /// <summary>
/// 功能键按键事件处理
/// </summary>
/// <param name="eventArgs">功能键事件参数</param>
void KeyBoardEventHandle(KeyBoardEventArgs eventArgs);
///// <summary>
///// 按键事件处理
///// </summary>
///// <param name="keyEventArgs">按键事件参数</param>
//void KeyEventHandle(KeyEventArgs keyEventArgs);
}
}
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using app.Configuration.Models;
using app.Configuration.Services;
using app.Lib;
using app.Lib.Graph;
using app.Measure.Events;
using app.Measure.Models;
using app.Measure.ViewModels;
using app.Measure.Actions;
using Bedrock.Events;
using Microsoft.Practices.Unity;
using app.Main;
using app.Main.Graph;
using app.KeyBoards.Events; namespace app.Measure.Tools
{
//椭圆测量工具
public class _2DEllipse : PropertyChangedBase,IToolAction
{
private readonly IEventAggregator _eventAggregator;
private readonly IConfigService _configService;
private readonly MeasureData _measureData; //存放绘图工具的ID private readonly List<Point> _points; //绘制椭圆的坐标集
private readonly Measurement _measurement; //该测量环境的XML配置
private readonly Brush _plotBrush; //绘制圆点的对象
private readonly Pen _linePen; //绘制曲线的对象
private readonly int _widthOfPlot; //圆点半径
private ICoordinate _coordinate; //坐标系对象
private readonly List<MeasureResultItem> _resultItems; //存放测量值的列表
private int _resultIndex; //测量值索引
private double _width; //椭圆的长半轴长
private double _height; //椭圆的短半轴长
private readonly CalculationFomula _calculationFomula; // 计算公式
private readonly MeasureToolType _measuretooltype; //读取工具XML的配置,初始化绘图工具
public _2DEllipse(IUnityContainer unityContainer, IEventAggregator eventAggregator)
{
Logger.Log("_2DEllipse Start Initial", Level.Info);
MeasureTool tool;
var unityContainer1 = unityContainer;
_eventAggregator = eventAggregator;
_configService = unityContainer1.Resolve<IConfigService>();
_measureData = unityContainer1.Resolve<MeasureData>(); _resultItems = new List<MeasureResultItem>
{
new MeasureResultItem("LAxis", "mm", ),
new MeasureResultItem("SAxis", "mm", ),
new MeasureResultItem("Cir", "mm", ),
new MeasureResultItem("Area", "mm2", )
};
_resultIndex = ; _calculationFomula = new CalculationFomula();
_points = new List<Point>();
if (_measureData.IsGenericTool == false)
{
_measurement = _configService.GetMeasurementId(_measureData.MeasurementId);
tool = _configService.GetMeasureToolId(_measureData.ToolId);
}
else
{
_measurement = _configService.GetMeasurementId(_measureData.GenericToolItem[_measureData.GenericToolIndex].MeasurementId);
tool = _configService.GetMeasureToolId(_measureData.GenericToolItem[_measureData.GenericToolIndex].ToolId);
}
_plotBrush = new SolidBrush(Common.IntToColor(tool.ColorOfPlot));
_widthOfPlot = tool.WidthOfPlot;
_measuretooltype = tool.MeasureToolType;
_linePen = new Pen(Common.IntToColor(tool.ColorOfLine), tool.WidthOfLine) {DashStyle = tool.Dashstype};
_eventAggregator.GetEvent<PubSubEvent<string>>().Subscribe(EventHandle, true);
Logger.Log("_2DEllipse End Initial", Level.Info);
} //绘图
public void Draw(Graphics graphics, ICoordinate coordinate)
{
//ToolDraw.DrawFromToolData(graphics, coordinate, _MeasureData.GetToolPositionDataItem); if (coordinate != null)
{
_coordinate = coordinate;
if (_isInViewRegion == false)
{
return;
}
} if (_points.Count < )
{
foreach (var p in _points)
{
graphics.FillEllipse(_plotBrush, new Rectangle(p.X - _widthOfPlot, p.Y - _widthOfPlot, _widthOfPlot * , _widthOfPlot * ));
}
} if (_points.Count != ) return;
_width = 0.5 * Math.Sqrt((_points[].X - _points[].X) * (_points[].X - _points[].X) + (_points[].Y - _points[].Y) * (_points[].Y - _points[].Y));
var p1 = new PointD((_points[].X + _points[].X) / 2.0, y: (_points[].Y + _points[].Y) / 2.0);
_height = Math.Sqrt((p1.X - _points[].X) * (p1.X - _points[].X) + (p1.Y - _points[].Y) * (p1.Y - _points[].Y));
if (coordinate != null)
graphics.DrawEllipse(_linePen, (int)coordinate.ViewRegionD.Min.X, (int)coordinate.ViewRegionD.Min.Y, (int)_height * , (int)_width * );
} //计算结果
private void CaculateResult()
{
if (_coordinate == null)
{
return;
}
var p = new PointD((_points[].X + _points[].X) / 2.0, (_points[].Y + _points[].Y) / 2.0);
p = _coordinate.ViewToWorld(p);
var p1 = _coordinate.ViewToWorld(_points[]);
var p2 = _coordinate.ViewToWorld(_points[]);
var p3 = _coordinate.ViewToWorld(_points[]);
_width = 0.5 * _calculationFomula.Distance(p1, p3);
_height = _calculationFomula.Distance(p, p2);
_resultItems[].Result = _width > _height ? * _width : * _height;//椭圆长轴长
_resultItems[].Result = _width < _height ? * _width : * _height; //椭圆短轴长 //椭圆周长
_resultItems[].Result = _calculationFomula.EllipseCirculation(_resultItems[].Result, _resultItems[].Result);
//椭圆面积
_resultItems[].Result = _calculationFomula.EllipseArea(_resultItems[].Result, _resultItems[].Result);
} /// <summary>
/// 功能键、旋钮处理
/// </summary>
/// <param name="eventArgs"></param>
public void KeyBoardEventHandle(KeyBoardEventArgs eventArgs)
{ } private MousePointEvent _mousePointEvent;
private bool _isInViewRegion;
private double _distance; //获取鼠标坐标
private Point _mousePoint;
private PointD _mousePointInView;
public void MouseEventHandle(object sender, MouseEventArgs eventArgs)
{
_mousePoint = eventArgs.Location;
if (_coordinate != null)
{
_mousePointInView = _coordinate.DisplayToView(_mousePoint);
_isInViewRegion = _coordinate.ViewRegionD.Contains(_mousePointInView);
_distance = _calculationFomula.Distance(_coordinate.ViewToWorld(_mousePoint),
_coordinate.ViewToWorld(_coordinate.CenterPointD));
}
_mousePointEvent = new MousePointEvent(_distance, _isInViewRegion);
_eventAggregator.GetEvent<PubSubEvent<MousePointEvent>>().Publish(_mousePointEvent); if (eventArgs.Button == MouseButtons.Left && eventArgs.Clicks == )
{
MouseClick();
}
} //响应按键,收集坐标,commit后结果值在结果窗口中显示出来
public void MouseClick()
{
if (_points.Count == && _measurement.AutoNext == false)
{
return;
}
if (_points.Count >= )
{
_points.Clear();
_points.Add(_mousePoint);
}
else
{
_points.Add(_mousePoint);
}
if (_points.Count == )
{
//if (_measurement.AutoNext == false)
//{
// return;
//}
CaculateResult(); for (_resultIndex = _resultItems.Count - ; _resultIndex >= ; _resultIndex--)
{
_eventAggregator.GetEvent<MeasureToolEvent>()
.Publish(new MeasureEventArgs(MessureEventType.MessureToolCommit,
_resultItems[_resultIndex].Clone()));
}
}
NotifyPropertyChanged(OperateState.MeasureRepaint);
} bool _isCalc;
private void EventHandle(string e) //重绘,显示结果等事件处理
{
switch (e)
{
case EventNames.MeasureToolpaint:
NotifyPropertyChanged(OperateState.MeasureRepaint);
break;
case EventNames.MeasureToolRepaint:
_points.Clear();
NotifyPropertyChanged(OperateState.MeasureRepaint);
break;
case EventNames.MeasureToolCalcResult:
MeasureToolType measuretooltype = _configService.GetMeasureToolId(_measureData.ToolId).MeasureToolType;
if (_measurement.AutoCaculate == false && _isCalc == false && _points.Count == && measuretooltype == _measuretooltype)
{
_isCalc = true;
CaculateResult();
for (_resultIndex = _resultItems.Count - ; _resultIndex >= ; _resultIndex--)
{
_eventAggregator.GetEvent<MeasureToolEvent>()
.Publish(new MeasureEventArgs(MessureEventType.MessureToolCommit, _resultItems[_resultIndex].Clone()));
}
}
break;
case EventNames.MeasureToolNextPaint:
if (_measurement.AutoNext == false)
{
_points.Clear();
_isCalc = false;
}
NotifyPropertyChanged(OperateState.MeasureRepaint);
break;
}
}
}
}
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using app.Configuration.Models;
using app.Configuration.Services;
using app.Lib;
using app.Lib.Graph;
using app.Measure.Events;
using app.Measure.Models;
using app.Measure.ViewModels;
using app.Measure.Actions;
using Bedrock.Events;
using Microsoft.Practices.Unity;
using System.Drawing.Drawing2D;
using app.Main;
using app.Main.Graph;
using app.KeyBoards.Events;
using app.Main.ViewModels; namespace app.Measure.Tools
{
//轨迹/区域测量工具
public class _2DTrace : PropertyChangedBase,IToolAction
{
private readonly IEventAggregator _eventAggregator;
private readonly IConfigService _configService;
private readonly MeasureData _measureData; //存放绘图工具的ID private List<Point> _points; //轨迹/区域的坐标集
private List<List<Point>> _tempPoints;
private readonly MeasureTool _tool; //绘图工具的配置信息
private readonly Measurement _measurement; //该测量环境的XML配置
private readonly Font _textFont; //文本格式对象
private readonly Brush _textBrush; //绘制文本的对象
private ICoordinate _coordinate; //坐标系对象
private readonly List<MeasureResultItem> _resultItems; //存放测量值的列表
private readonly CalculationFomula _calculationFomula; // 计算公式
private readonly MeasureToolType _measuretooltype; private readonly SystemStateModel _systemStateModel;
private readonly string[] _promptString = { "Place the first point of the closed trace", "Trace the area boundary" }; //读取工具XML的配置,初始化绘图工具
public _2DTrace(IUnityContainer unityContainer, IEventAggregator eventAggregator)
{
Logger.Log("_2DTrace Start Initial", Level.Info);
var unityContainer1 = unityContainer;
_eventAggregator = eventAggregator;
_configService = unityContainer1.Resolve<IConfigService>();
_measureData = unityContainer1.Resolve<MeasureData>();
_systemStateModel = unityContainer1.Resolve<SystemStateModel>();
_coordinate = MeasureAction.GetCoordinate(); _resultItems = new List<MeasureResultItem>
{
new MeasureResultItem("TCir", "mm", ),
new MeasureResultItem("TArea", "mm2", )
}; _calculationFomula = new CalculationFomula();
_points = new List<Point>();
_tempPoints = new List<List<Point>>();
if (_measureData.IsGenericTool == false)
{
_measurement = _configService.GetMeasurementId(_measureData.MeasurementId);
_tool = _configService.GetMeasureToolId(_measureData.ToolId);
}
else
{
_measurement = _configService.GetMeasurementId(_measureData.GenericToolItem[_measureData.GenericToolIndex].MeasurementId);
_tool = _configService.GetMeasureToolId(_measureData.GenericToolItem[_measureData.GenericToolIndex].ToolId);
}
_measuretooltype = _tool.MeasureToolType;
_textFont = new Font("Consolas", 10.5F, FontStyle.Bold, GraphicsUnit.Point, );
_textBrush = new SolidBrush(Common.IntToColor(_tool.ColorOfLine));
_eventAggregator.GetEvent<PubSubEvent<string>>().Subscribe(EventHandle, true); if (_measureData.GetToolPositionDataItem == null)
{
var toolDataList = new List<ToolPositionDataItem>();
_measureData.GetToolPositionDataItem = toolDataList;
}
if (_measureData.GetMeasureResultData == null)
{
var toolDataList = new List<MeasureResultData>();
_measureData.GetMeasureResultData = toolDataList;
} _pen = new Pen(Color.Green, _tool.WidthOfLine) {DashStyle = DashStyle.Dash};
Logger.Log("_2DTrace End Initial", Level.Info);
} private ToolPositionDataItem _toolPositionDataItem;
private List<Point> _fillEllipse;
private List<DrawCurvePoint> _drawCurve; private readonly Pen _pen;//旋钮旋转重绘的颜色
private bool _isRotatebuttonRepaintFirstTime = true;//旋钮旋转重绘,该情况下是否第一次调用Draw函数 //绘图
public void Draw(Graphics graphics, ICoordinate coordinate)
{
if (coordinate != null)
{
_coordinate = coordinate;
}
else
{
return;
} if (false == _measureData.IsRotatingButtonRePaint)
{
ToolDraw.DrawFromToolData(graphics, coordinate, _measureData.GetToolPositionDataItem, _currentsender); switch (_keynum%)
{
case :
if (_isInViewRegion)
{
MeasureAction.Cursortype = CursorType.CursorAreaPicture;
}
break;
case :
if (_isInViewRegion == false)
{
UserControl temp = _measureData.MouseEventSender as UserControl;
if (null != temp)
Cursor.Clip = temp.RectangleToScreen(Rectangle.Round(_coordinate.DisplayRegion));
}
else
{
MeasureAction.Cursortype = CursorType.CursorPicture;
}
break;
default:
MeasureAction.Cursortype = MeasureAction.AutoNextMeasure == false ? CursorType.CursorEmptyPicture : CursorType.CursorAreaPicture;
break;
}
}
else
{
if (_isInViewRegion == false)
{
UserControl temp = _measureData.MouseEventSender as UserControl;
if (null != temp)
Cursor.Clip = temp.RectangleToScreen(Rectangle.Round(_coordinate.DisplayRegion));
}
MeasureAction.Cursortype = CursorType.CursorPicture; _isRotatebuttonRepaintFirstTime = _measureData.IsFirsttimeRotatePaint; _tempPoints.Clear();
ToolDraw.RotateRepaintFromToolData(graphics, coordinate, _measureData.GetToolPositionDataItem,
_measureData.RotatingButtonSelectedPointNum, _measureData.RotatingButtonSelectedIndex, ref _tempPoints,
_mousePoint, _pen, _isRotatebuttonRepaintFirstTime, _isInViewRegion, _measureData.MouseEventSender);
_measureData.IsFirsttimeRotatePaint = false;
_points = _tempPoints[];
}
} //计算结果
private void CaculateResult()
{
if (_coordinate == null)
{
return;
}
var pointds = new List<PointD>();
pointds.Clear();
for (var i = ; i < _points.Count - ; i++)
{
var p1 = _coordinate.ViewToWorld(_points[i]);
pointds.Add(p1);
}
//trace的周长计算
_resultItems[].Result = _calculationFomula.TraceCirculation(pointds);
//trace的面积
_resultItems[].Result = _calculationFomula.TraceArea(pointds);
} /// <summary>
/// 功能键、旋钮处理
/// </summary>
/// <param name="eventArgs"></param>
public void KeyBoardEventHandle(KeyBoardEventArgs eventArgs)
{ } private MousePointEvent _mousePointEvent;
private bool _isInViewRegion;
private double _distance;
private object _currentsender; //获取鼠标坐标
private Point _mousePoint;
private PointD _mousePointInView;
public void MouseEventHandle(object sender, MouseEventArgs eventArgs)
{
_currentsender = sender;
_measureData.MouseEventSender = sender;
_mousePoint = eventArgs.Location;
if (_coordinate != null)
{
_mousePointInView = _coordinate.DisplayToView(_mousePoint);
_isInViewRegion = _coordinate.ViewRegionD.Contains(_mousePointInView);
_distance = _calculationFomula.Distance(_coordinate.ViewToWorld(_mousePoint),
_coordinate.ViewToWorld(_coordinate.CenterPointD));
} if (_keynum % == )
{
if (_isInViewRegion)
{
if (_points.Count > )
{
double angle = _calculationFomula.AngleOfPoint(_mousePoint, _points[_points.Count - ],
_points[_points.Count - ]);
double distance = _calculationFomula.Distance(_mousePoint, _points[_points.Count - ]);
if (distance > && angle >= )
{
_points.Add(_mousePoint);
//ToolDraw.SetCursorPositon(_mousePoint, sender);
}
else if (distance > && angle < )
{
_points.Remove(_points[_points.Count - ]);
//ToolDraw.SetCursorPositon(_points[_points.Count - 1], sender);
}
}
else
{
_points.Add(_mousePoint);
} if (false == _measureData.IsRotatingButtonRePaint)
{
AddDataItem(_points);
}
} NotifyPropertyChanged(OperateState.MeasureRepaint);
} if (_isInViewRegion)
{
switch (_keynum%)
{
case :
if (_systemStateModel.HintText != _promptString[])
_systemStateModel.HintText = _promptString[];
break;
case :
if (_systemStateModel.HintText != _promptString[])
_systemStateModel.HintText = _promptString[];
break;
default:
if (_systemStateModel.HintText != "")
_systemStateModel.HintText = "";
break;
}
}
else
{
MeasureAction.Cursortype = CursorType.Max;
if (_systemStateModel.HintText != "")
_systemStateModel.HintText = "";
} _mousePointEvent = new MousePointEvent(_distance, _isInViewRegion) {MeasureResultItem = {Name = "Area"}};
NotifyPropertyChanged(OperateState.MeasureRepaint);
_eventAggregator.GetEvent<PubSubEvent<MousePointEvent>>().Publish(_mousePointEvent); if (eventArgs.Button == MouseButtons.Left && eventArgs.Clicks == && _isInViewRegion)
{
if (_measureData.IsRotatingButtonRePaint)
{
Cursor.Clip = Rectangle.Empty;
CaculateResult(); var temp = new MeasureResultData {Name = _measuretooltype.ToString()};
foreach (var t in _resultItems)
{
temp.MeasureResultItem.Add(t.Clone() as MeasureResultItem);
}
_measureData.GetMeasureResultData.Add(temp); _eventAggregator.GetEvent<MeasureToolEvent>()
.Publish(new MeasureEventArgs(MessureEventType.MessureToolCommit, _resultItems[].Clone()));
}
else
{
MouseClick();
}
_measureData.IsRotatingButtonRePaint = false;
}
} private void AddDataItem(List<Point> points)
{
if (_fillEllipse.Count > )
{
_fillEllipse.Remove(_fillEllipse[]);
}
_fillEllipse.Add(points[]); DrawCurvePoint point1 = new DrawCurvePoint();
foreach (Point point in points)
{
point1.CurvePoint.Add(point);
} if (_drawCurve.Count > )
{
_drawCurve.Remove(_drawCurve[]);
}
_drawCurve.Add(point1); if (_measureData.GetToolPositionDataItem.Count > _measureData.MaxMeasureTimes)
{
_measureData.GetToolPositionDataItem.Remove(_measureData.GetToolPositionDataItem[]);
}
} //响应按键,收集坐标,commit后结果值在结果窗口中显示出来
int _keynum; //记录按键次数
public void MouseClick()
{
//if (keynum != 0 && keynum % 3 == 2 && _measurement.AutoNext == false)
if (_keynum != && _keynum % == && MeasureAction.AutoNextMeasure == false)
{
return;
}
_keynum++;
switch (_keynum % )
{
case :
_points.Clear();
_fillEllipse = new List<Point>();
_drawCurve = new List<DrawCurvePoint>();
_toolPositionDataItem = new ToolPositionDataItem(_drawCurve, _fillEllipse)
{
ColorOfLine = _tool.ColorOfLine,
ColorOfPlot = _tool.ColorOfPlot,
WidthOfLine = _tool.WidthOfLine,
WidthOfPlot = _tool.WidthOfPlot,
Dashstype = _tool.Dashstype,
TextBrush = _textBrush,
TextFont = _textFont,
MeasureToolType = _measuretooltype,
ModeType = _measureData.ModelType,
PointNum = ,
ItemNum = _resultItems.Count,
ViewName = (_measureData.MouseEventSender as Control) == null ? "" : ((Control) _measureData.MouseEventSender).Name
};
_measureData.GetToolPositionDataItem.Add(_toolPositionDataItem);
if (_isInViewRegion)
{
_points.Add(_mousePoint); if (false == _measureData.IsRotatingButtonRePaint)
{
AddDataItem(_points);
}
}
break;
case :
if (_drawCurve.Count != )
_drawCurve[].IsFinished = true; if (_measurement.AutoCaculate == false)
{
return;
}
CaculateResult(); var temp = new MeasureResultData {Name = _measuretooltype.ToString()};
foreach (var t in _resultItems)
{
temp.MeasureResultItem.Add(t.Clone() as MeasureResultItem);
}
_measureData.GetMeasureResultData.Add(temp); _eventAggregator.GetEvent<MeasureToolEvent>()
.Publish(new MeasureEventArgs(MessureEventType.MessureToolCommit, _resultItems[].Clone())); _toolPositionDataItem.IsFinished = true;
break;
}
NotifyPropertyChanged(OperateState.MeasureRepaint);
} bool _isCalc;
private void EventHandle(string e) //重绘,显示结果等事件处理
{
switch (e)
{
case EventNames.MeasureToolpaint:
NotifyPropertyChanged(OperateState.MeasureRepaint);
break;
case EventNames.MeasureToolRepaint:
_points.Clear();
_keynum = ;
NotifyPropertyChanged(OperateState.MeasureRepaint);
break;
case EventNames.MeasureToolCalcResult:
var measuretooltype = _configService.GetMeasureToolId(_measureData.ToolId).MeasureToolType;
if (_measurement.AutoCaculate == false && _isCalc == false && _keynum % == && measuretooltype == _measuretooltype)
{
_isCalc = true;
CaculateResult();
for (var i = _resultItems.Count - ; i >= ; i--)
{
_eventAggregator.GetEvent<MeasureToolEvent>()
.Publish(new MeasureEventArgs(MessureEventType.MessureToolCommit, _resultItems[i].Clone()));
}
}
break;
case EventNames.MeasureToolNextPaint:
if (_measurement.AutoNext == false)
{
_keynum = ;
_isCalc = false;
}
NotifyPropertyChanged(OperateState.MeasureRepaint);
break;
}
}
}
}