前面对整个页面做了一个简单的说明
现在开始进页面进行拆分
在整个页面里可以拆分以下几个基本的控件:Activity控件、ArrowLine控件,Canvas画布
先说第一个控件吧,先看一下图如下所示:
图面上的这些按钮都是Activity控件:我分别给他取的名字是:BeginActivity,EndActivity,JudgeActivity,LableActivity、HandlingProcessActivity、DBActivity它们都继承IActivity接口;由于每个Activity之间都有好多相同的操作,于是他们都继承一个BaseActivity控件;
BaseActivity控件实现IBaseActivity接口
接口图如下所示
/// <summary>
/// Base控件接口
/// </summary>
public interface IBaseActivity : IImportExport
{
/// <summary>
/// 所有箭头的坐标
/// </summary>
Dictionary<ArrowLine, ArrowLine> DictArrowCapPoint { get; set; }
/// <summary>
/// 所有箭尾的坐标
/// </summary>
Dictionary<ArrowLine, ArrowLine> DictArrowFootPoint { get; set; }
/// <summary>
/// 当前左键单的坐标
/// </summary>
Point CurrentLeftButtonDownPoint { get; set; }
/// <summary>
/// 节点的标题
/// </summary>
String LabelContent { get; set; }
/// <summary>
/// 节点的GUID
/// </summary>
String ActivityGUID { get; set; }
/// <summary>
/// 删除线
/// </summary>
void RemoveLine(ArrowLine line);
/// <summary>
/// 更新所有箭头坐标
/// </summary>
/// <param name="point"></param>
void UpdateArrowCapPoint(Point point);
/// <summary>
/// 更新所有箭尾坐标
/// </summary>
/// <param name="point"></param>
void UpdateArrowFootPoint(Point point);
/// <summary>
/// 判断线是否存在了。
/// </summary>
/// <param name="iact"></param>
/// <returns></returns>
bool CheckedArrowIsExists(IActivity iact);
/// <summary>
/// 控件的与线之间的关系
/// </summary>
/// <returns></returns>
string ExportControlRelationship();
}
BaseActivity控件界面如下所示:
界面xaml描述代码如下:
<Grid x:Class="PlatformClient.EventDesign.Activitys.BaseActivity"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Height="40" Width="100"
Canvas.ZIndex="10" Background="#5280B784">
<TextBlock Name="txtCommon" Visibility="Visible" Canvas.ZIndex="5" Text="demo activity" TextWrapping="Wrap" VerticalAlignment="Center" HorizontalAlignment="Stretch" />
<Ellipse Name="eDot" Height="40" Stroke="#FFE2CE14" StrokeThickness="13" Opacity="0.5"
Width="40" Canvas.ZIndex="100" />
</Grid>
BaseActivity控件里的代码实现如下:
public partial class BaseActivity : Grid, IBaseActivity
{
string _ActivityGUID;
Dictionary<ArrowLine, ArrowLine> _DictArrowCapPoint = new Dictionary<ArrowLine, ArrowLine>();
Dictionary<ArrowLine, ArrowLine> _DictArrowFootPoint = new Dictionary<ArrowLine, ArrowLine>();
public Point CurrentEnterPoint = new Point();
public event MouseEventHandler DotMouseEnter;
public event MouseEventHandler DotMouseMove;
public event MouseEventHandler DotMouseLeave;
public event MouseButtonEventHandler DotMouseLeftButtonUp;
public event MouseButtonEventHandler DotMouseLeftButtonDown;
public Dictionary<ArrowLine, ArrowLine> DictArrowCapPoint { get { return _DictArrowCapPoint; } set { _DictArrowCapPoint = value; } }
public Dictionary<ArrowLine, ArrowLine> DictArrowFootPoint { get { return _DictArrowFootPoint; } set { _DictArrowFootPoint = value; } }
public Point CurrentLeftButtonDownPoint { get; set; }
public String LabelContent
{
get { return this.txtCommon.Text; }
set
{
this.txtCommon.Text = value;
// ToolTipService.SetToolTip(this, value);
}
}
public string ActivityGUID
{
get
{
if (String.IsNullOrEmpty(_ActivityGUID))
{
_ActivityGUID = Wrapper.GuidValue;
}
return _ActivityGUID;
}
set { _ActivityGUID = value; }
}
protected List<ContextMenuInfo> _ContextMenus
{
get
{
return new List<ContextMenuInfo>{
new ContextMenuInfo{ Header="删除", Type = ContextMenuType.Delete, Source=this },
new ContextMenuInfo{ Header="修改内容", Type = ContextMenuType.ModifyContent,Source=this}
};
}
}
public BaseActivity()
{
InitializeComponent();
this.eDot.MouseEnter += new MouseEventHandler(eDot_MouseEnter);
this.eDot.MouseLeave += new MouseEventHandler(eDot_MouseLeave);
this.eDot.MouseMove += new MouseEventHandler(eDot_MouseMove);
this.eDot.MouseLeftButtonDown += new MouseButtonEventHandler(eDot_MouseLeftButtonDown);
this.eDot.MouseLeftButtonUp += new MouseButtonEventHandler(eDot_MouseLeftButtonUp);
}
void eDot_MouseLeave(object sender, MouseEventArgs e)
{
if (null != DotMouseLeave)
{
DotMouseLeave(this, e);
}
}
void eDot_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (null != DotMouseLeftButtonUp)
{
DotMouseLeftButtonUp(this, e);
}
}
void eDot_MouseMove(object sender, MouseEventArgs e)
{
if (null != DotMouseMove)
{
DotMouseMove(this, e);
}
}
void eDot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (null != DotMouseLeftButtonDown)
{
DotMouseLeftButtonDown(this, e);
}
}
void eDot_MouseEnter(object sender, MouseEventArgs e)
{
if (null != DotMouseEnter)
{
DotMouseEnter(this, e);
}
}
/// <summary>
/// 更新所有箭头的坐标
/// </summary>
/// <param name="point"></param>
public void UpdateArrowCapPoint(Point point)
{
if (CurrentEnterPoint.X.Equals(point.X) && CurrentEnterPoint.Y.Equals(point.Y))
{
return;
}
double _x = 0;
double _y = 0;
if (0 != CurrentEnterPoint.X && 0 != CurrentEnterPoint.Y)
{
_x = point.X - CurrentEnterPoint.X;
_y = point.Y - CurrentEnterPoint.Y;
}
foreach (var v in DictArrowFootPoint)
{
var ep = v.Value.StartPoint;
v.Value.StartPoint = new Point(ep.X + _x, ep.Y + _y);
}
CurrentEnterPoint = point;
}
/// <summary>
/// 更新所有箭尾的坐标
/// </summary>
/// <param name="point"></param>
public void UpdateArrowFootPoint(Point point)
{
if (CurrentLeftButtonDownPoint.X.Equals(point.X) && CurrentLeftButtonDownPoint.Y.Equals(point.Y))
{
return;
}
double _x = 0;
double _y = 0;
if (0 != CurrentLeftButtonDownPoint.X && 0 != CurrentLeftButtonDownPoint.Y)
{
_x = point.X - CurrentLeftButtonDownPoint.X;
_y = point.Y - CurrentLeftButtonDownPoint.Y;
}
foreach (var v in DictArrowCapPoint)
{
var sp = v.Value.EndPoint;
v.Value.EndPoint = new Point(sp.X + _x, sp.Y + _y);
}
CurrentLeftButtonDownPoint = point;
}
/// <summary>
///
/// </summary>
/// <param name="line"></param>
public void RemoveLine(ArrowLine line)
{
if (DictArrowCapPoint.ContainsKey(line))
{
DictArrowCapPoint.Remove(line);
}
if (DictArrowFootPoint.ContainsKey(line))
{
DictArrowFootPoint.Remove(line);
}
}
/// <summary>
/// 判断线是否已经存在了
/// </summary>
/// <param name="iact"></param>
/// <returns></returns>
public bool CheckedArrowIsExists(IActivity iact)
{
//-->所有字典里的箭头
foreach (var v in DictArrowCapPoint)
{
if (null == v.Value.ArrowFootControl || !(v.Value.ArrowFootControl is IActivity))
{
continue;
}
if ((v.Value.ArrowFootControl as IActivity).Equals(iact))
{
return true;
}
}
//-->所有字典里的箭尾
foreach (var v in DictArrowFootPoint)
{
if (null == v.Value.ArrowCapControl || !(v.Value.ArrowCapControl is IActivity))
{
continue;
}
if ((v.Value.ArrowCapControl as IActivity).Equals(iact))
{
return true;
}
}
return false;
}
public string ExportLocation()
{
ActivityInfo ai = new ActivityInfo();
ai.Type = this.GetType().Name;
ai.Name = this.Name;
ai.Guid = this.ActivityGUID;
ai.Width = this.Width;
ai.Height = this.Height;
ai.Left = Canvas.GetLeft(this);
ai.Top = Canvas.GetTop(this);
ai.Content = this.txtCommon.Text.Trim();
ai.EnterX = CurrentEnterPoint.X;
ai.EnterY = CurrentEnterPoint.Y;
var aa = ai.ToXElement("Activity");
return aa.ToString();
}
public string ExportControlRelationship()
{ //-->
List<string> item = new List<string>();
string _arrowCap = string.Empty;
List<String> capItem = new List<string>();
foreach (var v in DictArrowCapPoint)
{
capItem.Add(v.Key.Name);
}
List<String> footItem = new List<string>();
foreach (var v in DictArrowFootPoint)
{
footItem.Add(v.Key.Name);
}
string result = string.Empty;
result += string.Format("<Control Name=\"{0}\" Cap=\"{1}\" Foot=\"{2}\" />",
this.Name, string.Join("|", capItem), string.Join("|", footItem));
return result;
}
public void Dispose()
{
try
{
this.eDot.MouseEnter -= eDot_MouseEnter;
this.eDot.MouseLeave -= eDot_MouseLeave;
this.eDot.MouseMove -= eDot_MouseMove;
this.eDot.MouseLeftButtonDown -= eDot_MouseLeftButtonDown;
this.eDot.MouseLeftButtonUp -= eDot_MouseLeftButtonUp;
DictArrowFootPoint.Clear();
DictArrowCapPoint.Clear();
}
catch { }
}
internal bool IsStart()
{
//-->如果已经存在一根开始线了,就不能再有了。
if (0 < DictArrowFootPoint.Count)
{
return false;
}
return true;
}
}
BeginActivity,EndActivity,JudgeActivity,LableActivity、HandlingProcessActivity、DBActivity...这些控件都继承BaseActivity;继承后它们也就完成了,鼠标的拖拽,右、左键的单击事件都可以用了,又由于每个Activity都有自己的特性,所以Activity在继承BaseActivity控件的时候,还得实现IActivity接口。