DesignerControl.xaml.cs
上传用户:lvyingde
上传日期:2018-04-24
资源大小:610k
文件大小:11k
源码类别:

SilverLight

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Documents;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Animation;
  11. using System.Windows.Shapes;
  12. namespace wxwinter.WFDesignerSl
  13. {
  14.     public partial class DesignerControl : UserControl,IDesigner
  15.     {
  16.         public DesignerControl()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.         Random random = new Random();
  21.         int link_n = 0;
  22.         int activity_n = 0;
  23.         List<ActivityControl> ActivityControlList = new List<ActivityControl>();
  24.         List<ActivityPath> ActivityPathList = new List<ActivityPath>();
  25.         void pt_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
  26.         {
  27.             Path p = sender as Path;
  28.             var v = System.Windows.MessageBox.Show("删除所选连线?", "", MessageBoxButton.OKCancel);
  29.             if (v == MessageBoxResult.OK)
  30.             {
  31.                 RemoveActivityPath(p.Name);
  32.             }
  33.         }
  34.         void pt_MouseLeave(object sender, MouseEventArgs e)
  35.         {
  36.             Path p = sender as Path;
  37.             p.Stroke = new SolidColorBrush(Colors.Black);
  38.             p.StrokeThickness = 1;
  39.           
  40.         }
  41.         void pt_MouseEnter(object sender, MouseEventArgs e)
  42.         {
  43.             Path p = sender as Path;
  44.             p.Stroke = new SolidColorBrush(Colors.Red);
  45.             p.StrokeThickness =2;
  46.         }
  47.         void activity_连接事件(object sender, EventArgs e)
  48.         {
  49.             ActivityControl act = sender as ActivityControl;
  50.             LinkEventArgs c = e as LinkEventArgs;
  51.             AddActivityPath("", c.起点, act.Name, c.路由条件, "");
  52.         }
  53.         void activity_刷新事件(object sender, EventArgs e)
  54.         {
  55.             RefurbishActivityPath();
  56.         }
  57.         void DrawActivityPath(ActivityControl startingActivity, ActivityControl targetingActivity, Path pathObject, ActivityPath activityPath)
  58.         {
  59.             pathObject.Stroke = new SolidColorBrush(Colors.Black);
  60.             pathObject.StrokeThickness = 1;
  61.             
  62.             int i=0 ;//= startingActivity.分支集合.FindIndex(p => p == activityPath.路由);
  63.             for (int w = 0; w < startingActivity.分支集合.Count; w++)
  64.             {
  65.                 if (startingActivity.分支集合[w] == activityPath.路由)
  66.                 {
  67.                     i = w;
  68.                     break;
  69.                 }
  70.             }
  71.             int l = 0;
  72.             for (int n = 0; n <= i; n++)
  73.             {
  74.                 l = startingActivity.分支集合[n].Length + l;
  75.             }
  76.            
  77.             double x1;
  78.             double y1;
  79.             switch (activityPath.路由)
  80.             {
  81.                 case "开始状态":
  82.                     x1 = Canvas.GetLeft(startingActivity) + startingActivity.Width / 2;
  83.                     y1 = Canvas.GetTop(startingActivity) + startingActivity.Height;
  84.                     break;
  85.                 default:
  86.                     x1 = Canvas.GetLeft(startingActivity) + 5 + (l * 10) + i * 5;
  87.                     y1 = Canvas.GetTop(startingActivity) + startingActivity.Height - 10;
  88.                     break;
  89.             }
  90.             double x4 = Canvas.GetLeft(targetingActivity) + 20;
  91.             double y4 = Canvas.GetTop(targetingActivity);
  92.             double x2 = x1;
  93.             double y2 = y1 + 100;
  94.             double x3 = x4;
  95.             double y3 = y4 - 100;
  96.             PathFigure 形状子部分 = new PathFigure();
  97.             PathSegmentCollection 串线 = new PathSegmentCollection();
  98.             PathFigureCollection 形状集合 = new PathFigureCollection();
  99.             PathGeometry 形状 = new PathGeometry();
  100.             形状子部分.StartPoint = new Point(x1, y1);
  101.             形状子部分.Segments = 串线;
  102.             形状集合.Add(形状子部分);
  103.             形状.Figures = 形状集合;
  104.             pathObject.Data = 形状;
  105.             //pathObject.Stroke = System.Windows.Media.Brushes.Black;
  106.             pathObject.StrokeThickness = 1;
  107.             BezierSegment b = new BezierSegment();
  108.             b.Point1 = new Point(x2, y2);
  109.             b.Point2 = new Point(x3, y3);
  110.             b.Point3 = new Point(x4, y4);
  111.             串线.Add(b);
  112.             串线.Add(new LineSegment() { Point = new Point() { Y = y4 - 10, X = x4 + 5 } });
  113.             串线.Add(new LineSegment() { Point = new Point() { Y = y4 - 10, X = x4 + 4 } });
  114.             串线.Add(new LineSegment() { Point = new Point() { Y = y4, X = x4 } });
  115.             串线.Add(new LineSegment() { Point = new Point() { Y = y4 - 10, X = x4 - 5 } });
  116.             串线.Add(new LineSegment() { Point = new Point() { Y = y4 - 10, X = x4 - 6 } });
  117.             Canvas.SetLeft(activityPath.标签, x1 + (x4 - x1) / 2);
  118.             Canvas.SetTop(activityPath.标签, y4 - (y4 - y1) / 2);
  119.         }
  120.         void RefurbishActivityPath()
  121.         {
  122.             foreach (var v in ActivityPathList)
  123.             {
  124.                 if (ActivityControlList.Count(p => p.Name == v.起点) == 1 && ActivityControlList.Count(p => p.Name == v.目标) == 1)
  125.                 {
  126.                     var s = ActivityControlList.Single(p => p.Name == v.起点);
  127.                     var t = ActivityControlList.Single(p => p.Name == v.目标);
  128.                     DrawActivityPath(s, t, v.连线, v);
  129.                 }
  130.             }
  131.         }
  132.         public double PageSize
  133.         {
  134.             set
  135.             {
  136.                 scaleTransform.ScaleX = value;
  137.                 scaleTransform.ScaleY = value;
  138.             }
  139.             get
  140.             {
  141.                 return scaleTransform.ScaleX;
  142.             }
  143.         }
  144.         public double PageHeight
  145.         {
  146.             set
  147.             {
  148.                 this.myDesigner.Height = value;
  149.             }
  150.             get
  151.             {
  152.                 return this.myDesigner.Height;
  153.             }
  154.         }
  155.         public double PageWidth
  156.         {
  157.             set
  158.             {
  159.                 this.myDesigner.Width = value;
  160.             }
  161.             get
  162.             {
  163.                 return this.myDesigner.Width;
  164.             }
  165.         }
  166.         public List<string> NameList
  167.         {
  168.             get
  169.             {
  170.                 var v = myDesigner.Children.Cast<FrameworkElement>().Select(p => p.Name).ToList();
  171.                 return v;
  172.             }
  173.         }
  174.         public void AddActivity(ActivityControl activity)
  175.         {
  176.             if (string.IsNullOrEmpty(activity.Name))
  177.             {
  178.                 activity_n = activity_n + 1;
  179.                 activity.Name = "activity" + activity_n.ToString();
  180.             }
  181.  
  182.             if (activity.分支集合.Count == 0)
  183.             {
  184.                 activity.分支集合.Add("wxwinter");
  185.             }
  186.             this.myDesigner.Children.Add(activity);
  187.             ActivityControlList.Add(activity);
  188.             activity.刷新事件 += new EventHandler(activity_刷新事件);
  189.             activity.连接事件 += new EventHandler(activity_连接事件);
  190.             activity.设计器 = this;
  191.             RefurbishActivityPath();
  192.         }
  193.         public void RemoveActivity(string activityName)
  194.         {
  195.             var v = ActivityControlList.Single(p => p.Name == activityName);
  196.             this.myDesigner.Children.Remove(v);
  197.             ActivityControlList.Remove(v);
  198.             var pt = ActivityPathList.Where(p => p.起点 == activityName || p.目标 == activityName).Select(p => p.Name).ToList();
  199.             foreach (var tp in pt)
  200.             {
  201.                 RemoveActivityPath(tp);
  202.             }
  203.             RefurbishActivityPath();
  204.         }
  205.         public void AddActivityPath(string pathName, string startingPoint, string targetingPoint, string condition, string describe)
  206.         {
  207.             var v = ActivityPathList.Count(p => p.起点 == startingPoint && p.路由 == condition);
  208.             if (v != 0)
  209.             {
  210.                 MessageBox.Show("每个[起点]的[路由条件]只能指向一个[目标],如果要重新指定,请删除原指向");
  211.                 return;
  212.             }
  213.             if (startingPoint == targetingPoint)
  214.             {
  215.                 return;
  216.             }
  217.             if (string.IsNullOrEmpty(pathName))
  218.             {
  219.                 link_n = link_n + 1;
  220.                 pathName = "link" + link_n.ToString();
  221.             }
  222.             //if (NameList.Exists(p => p == pathName))
  223.             //{
  224.             //    return;
  225.             //}
  226.             Path pt = new Path();
  227.             pt.MouseEnter += new MouseEventHandler(pt_MouseEnter);
  228.             pt.MouseLeave += new MouseEventHandler(pt_MouseLeave);
  229.             pt.MouseLeftButtonDown += new MouseButtonEventHandler(pt_MouseRightButtonUp);
  230.             pt.Name = pathName;
  231.             ActivityPath ptr = new ActivityPath();
  232.             ptr.连线 = pt;
  233.             ptr.说明 = describe;
  234.             ptr.目标 = targetingPoint;
  235.             ptr.起点 = startingPoint;
  236.             ptr.路由 = condition;
  237.             ptr.标签 = new TextBlock() { Text = condition, Name = "tb_" + pathName };
  238.             myDesigner.Children.Add(ptr.标签);
  239.             this.myDesigner.Children.Add(pt);
  240.             ActivityPathList.Add(ptr);
  241.             RefurbishActivityPath();
  242.         }
  243.         public void RemoveActivityPath(string pathName)
  244.         {
  245.             var prt = ActivityPathList.Single(p => p.连线.Name == pathName);
  246.             this.myDesigner.Children.Remove(prt.连线);
  247.             this.myDesigner.Children.Remove(prt.标签);
  248.             ActivityPathList.Remove(prt);
  249.             RefurbishActivityPath();
  250.         }
  251.         public void SetCurrentActivity(string activityName)
  252.         {
  253.             ActivityControlList.Single(p => p.Name == activityName).设为活动();
  254.         }
  255.         public List<ActivityPath> GetActivityPathList()
  256.         {
  257.             return ActivityPathList;
  258.         }
  259.         public List<ActivityControl> GetActivityControlList()
  260.         {
  261.             return ActivityControlList;
  262.         }
  263.         private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  264.         {
  265.            
  266.         }
  267.         private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  268.         {
  269.             myContext.sourceName = "";
  270.             myContext.mouseMove = false;
  271.         }
  272.         public void Clear(bool isCreate)
  273.         {
  274.             ActivityControlList.Clear();
  275.             ActivityPathList.Clear();
  276.             this.myDesigner.Children.Clear();
  277.             if (isCreate)
  278.             {
  279.                 AddActivity(new BeginControl() { Name = "开始状态", 类型 = "头" });
  280.                 AddActivity(new EndControl() { Name = "归档状态", 类型 = "尾", X坐标 = 500, Y坐标 = 500 });
  281.             }
  282.         }
  283.     }
  284. }