c# 属性改变
Posted fengbaobao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c# 属性改变相关的知识,希望对你有一定的参考价值。
1 using System.ComponentModel; 2 using System.Windows.Forms; 3 using app.Main.Graph; 4 using app.KeyBoards.Events; 5 6 namespace app.Measure.Actions 7 { 8 /// <summary> 9 /// 操作接口 10 /// </summary> 11 public interface IToolAction : IActor, INotifyPropertyChanged 12 { 13 /// <summary> 14 /// 鼠标事件处理 15 /// </summary> 16 /// <param name="sender"></param> 17 /// <param name="eventArgs">鼠标事件参数</param> 18 void MouseEventHandle(object sender, MouseEventArgs eventArgs); 19 20 /// <summary> 21 /// 功能键按键事件处理 22 /// </summary> 23 /// <param name="eventArgs">功能键事件参数</param> 24 void KeyBoardEventHandle(KeyBoardEventArgs eventArgs); 25 ///// <summary> 26 ///// 按键事件处理 27 ///// </summary> 28 ///// <param name="keyEventArgs">按键事件参数</param> 29 //void KeyEventHandle(KeyEventArgs keyEventArgs); 30 } 31 }
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", 0), new MeasureResultItem("SAxis", "mm", 0), new MeasureResultItem("Cir", "mm", 0), new MeasureResultItem("Area", "mm2", 0) }; _resultIndex = 0; _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 < 3) { foreach (var p in _points) { graphics.FillEllipse(_plotBrush, new Rectangle(p.X - _widthOfPlot, p.Y - _widthOfPlot, _widthOfPlot * 2, _widthOfPlot * 2)); } } if (_points.Count != 3) return; _width = 0.5 * Math.Sqrt((_points[0].X - _points[2].X) * (_points[0].X - _points[2].X) + (_points[0].Y - _points[2].Y) * (_points[0].Y - _points[2].Y)); var p1 = new PointD((_points[2].X + _points[0].X) / 2.0, y: (_points[2].Y + _points[0].Y) / 2.0); _height = Math.Sqrt((p1.X - _points[1].X) * (p1.X - _points[1].X) + (p1.Y - _points[1].Y) * (p1.Y - _points[1].Y)); if (coordinate != null) graphics.DrawEllipse(_linePen, (int)coordinate.ViewRegionD.Min.X, (int)coordinate.ViewRegionD.Min.Y, (int)_height * 2, (int)_width * 2); } //计算结果 private void CaculateResult() { if (_coordinate == null) { return; } var p = new PointD((_points[2].X + _points[0].X) / 2.0, (_points[2].Y + _points[0].Y) / 2.0); p = _coordinate.ViewToWorld(p); var p1 = _coordinate.ViewToWorld(_points[0]); var p2 = _coordinate.ViewToWorld(_points[1]); var p3 = _coordinate.ViewToWorld(_points[2]); _width = 0.5 * _calculationFomula.Distance(p1, p3); _height = _calculationFomula.Distance(p, p2); _resultItems[0].Result = _width > _height ? 2 * _width : 2 * _height;//椭圆长轴长 _resultItems[1].Result = _width < _height ? 2 * _width : 2 * _height; //椭圆短轴长 //椭圆周长 _resultItems[2].Result = _calculationFomula.EllipseCirculation(_resultItems[0].Result, _resultItems[1].Result); //椭圆面积 _resultItems[3].Result = _calculationFomula.EllipseArea(_resultItems[0].Result, _resultItems[1].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 == 1) { MouseClick(); } } //响应按键,收集坐标,commit后结果值在结果窗口中显示出来 public void MouseClick() { if (_points.Count == 3 && _measurement.AutoNext == false) { return; } if (_points.Count >= 3) { _points.Clear(); _points.Add(_mousePoint); } else { _points.Add(_mousePoint); } if (_points.Count == 3) { //if (_measurement.AutoNext == false) //{ // return; //} CaculateResult(); for (_resultIndex = _resultItems.Count - 1; _resultIndex >= 0; _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 == 3 && measuretooltype == _measuretooltype) { _isCalc = true; CaculateResult(); for (_resultIndex = _resultItems.Count - 1; _resultIndex >= 0; _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", 0), new MeasureResultItem("TArea", "mm2", 0) }; _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, 0); _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%3) { case 0: if (_isInViewRegion) { MeasureAction.Cursortype = CursorType.CursorAreaPicture; } break; case 1: 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[0]; } } //计算结果 private void CaculateResult() { if (_coordinate == null) { return; } var pointds = new List<PointD>(); pointds.Clear(); for (var i = 0; i < _points.Count - 1; i++) { var p1 = _coordinate.ViewToWorld(_points[i]); pointds.Add(p1); } //trace的周长计算 _resultItems[0].Result = _calculationFomula.TraceCirculation(pointds); //trace的面积 _resultItems[1].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 % 3 == 1) { if (_isInViewRegion) { if (_points.Count > 3) { double angle = _calculationFomula.AngleOfPoint(_mousePoint, _points[_points.Count - 2], _points[_points.Count - 1]); double distance = _calculationFomula.Distance(_mousePoint, _points[_points.Count - 1]); if (distance > 1 && angle >= 120) { _points.Add(_mousePoint); //ToolDraw.SetCursorPositon(_mousePoint, sender); } else if (distance > 1 && angle < 120) { _points.Remove(_points[_points.Count - 1]); //ToolDraw.SetCursorPositon(_points[_points.Count - 1], sender); } } else { _points.Add(_mousePoint); } if (false == _measureData.IsRotatingButtonRePaint) { AddDataItem(_points); } } NotifyPropertyChanged(OperateState.MeasureRepaint); } if (_isInViewRegion) { switch (_keynum%3) { case 0: if (_systemStateModel.HintText != _promptString[0]) _systemStateModel.HintText = _promptString[0]; break; case 1: if (_systemStateModel.HintText != _promptString[1]) _systemStateModel.HintText = _promptString[1]; 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 == 1 && _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[0].Clone())); } else { MouseClick(); } _measureData.IsRotatingButtonRePaint = false; } } private void AddDataItem(List<Point> points) { if (_fillEllipse.Count > 0) { _fillEllipse.Remove(_fillEllipse[0]); } _fillEllipse.Add(points[0]); DrawCurvePoint point1 = new DrawCurvePoint(); foreach (Point point in points) { point1.CurvePoint.Add(point); } if (_drawCurve.Count > 0) { _drawCurve.Remove(_drawCurve[0]); } _drawCurve.Add(point1); if (_measureData.GetToolPositionDataItem.Count > _measureData.MaxMeasureTimes) { _measureData.GetToolPositionDataItem.Remove(_measureData.GetToolPositionDataItem[0]); } } //响应按键,收集坐标,commit后结果值在结果窗口中显示出来 int _keynum; //记录按键次数 public void MouseClick() { //if (keynum != 0 && keynum % 3 == 2 && _measurement.AutoNext == false) if (_keynum != 0 && _keynum % 3 == 2 && MeasureAction.AutoNextMeasure == false) { return; } _keynum++; switch (_keynum % 3) { case 1: _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 = 1, 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 2: if (_drawCurve.Count != 0) _drawCurve[0].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[0].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 = 0; NotifyPropertyChanged(OperateState.MeasureRepaint); break; case EventNames.MeasureToolCalcResult: var measuretooltype = _configService.GetMeasureToolId(_measureData.ToolId).MeasureToolType; if (_measurement.AutoCaculate == false && _isCalc == false && _keynum % 3 == 2 && measuretooltype == _measuretooltype) { _isCalc = true; CaculateResult(); for (var i = _resultItems.Count - 1; i >= 0; i--) { _eventAggregator.GetEvent<MeasureToolEvent>() .Publish(new MeasureEventArgs(MessureEventType.MessureToolCommit, _resultItems[i].Clone())); } } break; case EventNames.MeasureToolNextPaint: if (_measurement.AutoNext == false) { _keynum = 0; _isCalc = false; } NotifyPropertyChanged(OperateState.MeasureRepaint); break; } } } }
以上是关于c# 属性改变的主要内容,如果未能解决你的问题,请参考以下文章