5.7 开发工作流图形定义工具
5.7.6 图形工具主界面程序
......(接前页)
//绘图面板中鼠标移动事件处理办法(用于节点移动)
private void Panel_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
Graphics g=panel1.CreateGraphics();
//如果左键选中了节点
if(pickednode != null)
{
if(nodetype==nodetypedefine.notask)
{
NodeClass tempnode=null;
//抹掉移动前画的节点
g.DrawRectangle(new Pen(new SolidBrush(this.BackColor),3),
pickednode.LeftTopX,pickednode.LeftTopY,pickednode.Width,
pickednode.Height);
g.FillRectangle(new SolidBrush(this.BackColor),pickednode.LeftTopX,
pickednode.LeftTopY,pickednode.Width,pickednode.Height);
//如果移动的是过程逻辑节点
if(pickednode.NodeType==nodetypedefine.andsplit ||
pickednode.NodeType==nodetypedefine.orsplit ||
pickednode.NodeType==nodetypedefine.andjoin ||
pickednode.NodeType==nodetypedefine.orjoin)
{
Font drawFont1 = new Font("宋体", 9);
StringFormat drawFormat1 = new StringFormat();
drawFormat1.Alignment = StringAlignment.Center;
RectangleF stringrect1=new RectangleF(pickednode.LeftTopX-15,
pickednode.LeftTopY+3,60,14);
string nodename="";
if(pickednode.NodeType==nodetypedefine.andsplit)
nodename="And Split";
if(pickednode.NodeType==nodetypedefine.andjoin)
nodename="And Join";
if(pickednode.NodeType==nodetypedefine.orsplit)
nodename="Or Split";
if(pickednode.NodeType==nodetypedefine.orjoin)
nodename="Or Join";
//抹掉原来绘制的逻辑节点名称
g.DrawString(nodename,drawFont1,new SolidBrush(this.BackColor),
stringrect1,drawFormat1);
}
//抹掉移动前画的迁移
Pen pen=new Pen(this.BackColor);
AdjustableArrowCap arrow=new AdjustableArrowCap(5,10,false);
pen.CustomEndCap=arrow;
//获取指向所选节点的所有迁移
TransitionRecords pretransitions=transitionarray.GetToNodeTransition
(pickednode.NodeName);
//如果存在指向所选节点的迁移
if(pretransitions != null)
{
for(int i=0;i<pretransitions.Count;i++)
{
tempnode=nodearray.GetNode(pretransitions
.GetTransition(i).FromNode);
g.DrawLine(pen,tempnode.LeftTopX+tempnode.Width/2,
tempnode.LeftTopY+tempnode.Height,pickednode.LeftTopX
+pickednode.Width/2,pickednode.LeftTopY);
}
}
//获取从所选节点引出的所有迁移
TransitionRecords nexttransitions=transitionarray.GetFromNodeTransition
(pickednode.NodeName);
//如果存在从所选节点引出的迁移
if(nexttransitions != null)
{
for(int i=0;i<nexttransitions.Count;i++)
{
tempnode=nodearray.GetNode(nexttransitions.GetTransition(i)
.ToNode);
g.DrawLine(pen,pickednode.LeftTopX+pickednode.Width/2,
pickednode.LeftTopY+pickednode.Height,tempnode.LeftTopX
+tempnode.Width/2,tempnode.LeftTopY);
}
}
//移动到的新位置
dragtoX=e.X;
dragtoY=e.Y;
//重画节点
g.DrawRectangle(new Pen(new SolidBrush(Color.LightPink),3),dragtoX,
dragtoY,pickednode.Width,pickednode.Height);
//重新填充不同类型的节点(填充颜色不同)
if(pickednode.NodeType==nodetypedefine.start ||
pickednode.NodeType==nodetypedefine.end)
g.FillRectangle(new SolidBrush(Color.FromArgb(128,255,255)),
dragtoX,dragtoY,pickednode.Width,pickednode.Height);
if(pickednode.NodeType==nodetypedefine.tasknode)
g.FillRectangle(new SolidBrush(Color.FromArgb(0,128,255)),
dragtoX,dragtoY,pickednode.Width,pickednode.Height);
if(pickednode.NodeType==nodetypedefine.andsplit ||
pickednode.NodeType==nodetypedefine.orsplit ||
pickednode.NodeType==nodetypedefine.andjoin ||
pickednode.NodeType==nodetypedefine.orjoin)
g.FillRectangle(new SolidBrush(Color.Yellow),
dragtoX,dragtoY,pickednode.Width,pickednode.Height);
//绘制节点名称
Font drawFont = new Font("宋体", 9);
StringFormat drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Center;
RectangleF stringrect=RectangleF.Empty;
if(pickednode.NodeType == nodetypedefine.start || pickednode.NodeType
== nodetypedefine.end)
{
stringrect=new RectangleF(e.X+7,e.Y+6,46,14);
g.DrawString(pickednode.NodeName,drawFont,new SolidBrush(Color.Red),
stringrect,drawFormat);
}
if(pickednode.NodeType == nodetypedefine.tasknode)
{
stringrect=new RectangleF(e.X+5,e.Y+7,90,16);
g.DrawString(pickednode.NodeName,drawFont,new SolidBrush
(Color.White),stringrect,drawFormat);
}
if(pickednode.NodeType==nodetypedefine.andsplit ||
pickednode.NodeType==nodetypedefine.orsplit ||
pickednode.NodeType==nodetypedefine.andjoin ||
pickednode.NodeType==nodetypedefine.orjoin)
{
stringrect=new RectangleF(e.X-15,e.Y+3,60,14);
//绘制节点名称
nodetypedefine nodetype1=pickednode.NodeType;
string nodename="";
if(nodetype1 == nodetypedefine.andsplit)
nodename="And Split";
if(nodetype1 == nodetypedefine.andjoin)
nodename="And Join";
if(nodetype1 == nodetypedefine.orsplit)
nodename="Or Split";
if(nodetype1 == nodetypedefine.orjoin)
nodename="Or Join";
g.DrawString(nodename,drawFont,new SolidBrush(Color.Red),
stringrect,drawFormat);
}
//修改pickednode坐标
pickednode.LeftTopX=dragtoX;
pickednode.LeftTopY=dragtoY;
//重画迁移
pen.Color=Color.Red;
//绘制指向所移动节点的迁移
if(pretransitions != null)
{
for(int i=0;i<pretransitions.Count;i++)
{
tempnode=nodearray.GetNode(pretransitions.GetTransition(i).
FromNode);
g.DrawLine(pen,tempnode.LeftTopX+tempnode.Width/2,
tempnode.LeftTopY+tempnode.Height,pickednode.LeftTopX
+pickednode.Width/2,pickednode.LeftTopY);
}
}
//绘制从移动节点引出的迁移
if(nexttransitions != null)
{
for(int i=0;i<nexttransitions.Count;i++)
{
tempnode=nodearray.GetNode(nexttransitions.GetTransition(i)
.ToNode);
g.DrawLine(pen,pickednode.LeftTopX+pickednode.Width/2,
pickednode.LeftTopY+pickednode.Height,tempnode.LeftTopX
+tempnode.Width/2,tempnode.LeftTopY);
}
}
}//if(nodetype==nodetypedefine.notask)
}//如果左键选中了节点
}
//绘图面板中释放鼠标
private void Panel_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
//使本方法只对移动节点起作用
if(pickednode != null & nodetype==nodetypedefine.notask &
e.Button == MouseButtons.Left)
{
//重置pickednode指向空地址
pickednode=null;
//重画被鼠标拖动擦除的原有节点
repaint();
g.Dispose();
}
}
//重绘图形
public void repaint()
{
if(nodearray.nodeList.Count > 0)
{
NodeClass tempnode=null;
Graphics g=panel1.CreateGraphics();
g.TranslateTransform(panel1.AutoScrollPosition.X,
panel1.AutoScrollPosition.Y);
//重新填充绘图区(该语句会被paint事件自动执行)
g.FillRectangle(new SolidBrush(this.BackColor),0,0,this.Width,this.Height);
for(int i=0;i<nodearray.nodeList.Count;i++)
{
tempnode=(NodeClass)nodearray.nodeList[i];
g.DrawRectangle(new Pen(new SolidBrush(Color.LightPink),3),
tempnode.LeftTopX,tempnode.LeftTopY,tempnode.Width,tempnode.Height);
if(tempnode.NodeType==nodetypedefine.start ||
tempnode.NodeType==nodetypedefine.end)
g.FillRectangle(new SolidBrush(Color.FromArgb(128,255,255)),
tempnode.LeftTopX,tempnode.LeftTopY,tempnode.Width,tempnode.Height);
if(tempnode.NodeType==nodetypedefine.tasknode)
g.FillRectangle(new SolidBrush(Color.FromArgb(0,128,255)),
tempnode.LeftTopX,tempnode.LeftTopY,tempnode.Width,tempnode.Height);
if(tempnode.NodeType == nodetypedefine.andsplit || tempnode.NodeType ==
nodetypedefine.andjoin || tempnode.NodeType
== nodetypedefine.orsplit || tempnode.NodeType
== nodetypedefine.orjoin)
g.FillRectangle(new SolidBrush(Color.Yellow),tempnode.LeftTopX,
tempnode.LeftTopY,tempnode.Width,tempnode.Height);
//绘制节点名称
Font drawFont = new Font("宋体", 9);
StringFormat drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Center;
RectangleF stringrect=RectangleF.Empty;
if(tempnode.NodeType == nodetypedefine.start || tempnode.NodeType
== nodetypedefine.end)
{
stringrect=new RectangleF(tempnode.LeftTopX+7,
tempnode.LeftTopY+6,46,14);
g.DrawString(tempnode.NodeName,drawFont,new SolidBrush(Color.Red),
stringrect,drawFormat);
}
if(tempnode.NodeType == nodetypedefine.tasknode)
{
stringrect=new RectangleF(tempnode.LeftTopX+5,
tempnode.LeftTopY+9,90,16);
g.DrawString(tempnode.NodeName,drawFont,new SolidBrush(Color.White),
stringrect,drawFormat);
}
if(tempnode.NodeType == nodetypedefine.andsplit || tempnode.NodeType
== nodetypedefine.andjoin || tempnode.NodeType
== nodetypedefine.orsplit || tempnode.NodeType
== nodetypedefine.orjoin)
{
stringrect=new RectangleF(tempnode.LeftTopX-15,
tempnode.LeftTopY+3,60,14);
nodetypedefine nodetype=tempnode.NodeType;
string nodename="";
if(nodetype == nodetypedefine.andsplit)
nodename="And Split";
if(nodetype == nodetypedefine.andjoin)
nodename="And Join";
if(nodetype == nodetypedefine.orsplit)
nodename="Or Split";
if(nodetype == nodetypedefine.orjoin)
nodename="Or Join";
g.DrawString(nodename,drawFont,new SolidBrush(Color.Red),
stringrect,drawFormat);
}
//重画节点连接线
TransitionRecords nexttransitions=transitionarray.GetFromNodeTransition
(tempnode.NodeName);
//如果存在从第i个节点引出的迁移
if(nexttransitions != null)
{
Pen pen=new Pen(Color.Red);
AdjustableArrowCap arrow=new AdjustableArrowCap(5,10,false);
pen.CustomEndCap=arrow;
for(int j=0;j<nexttransitions.Count;j++)
{
NodeClass nextnode=nodearray.GetNode(nexttransitions
.GetTransition(j).ToNode);
g.DrawLine(pen,tempnode.LeftTopX+tempnode.Width/2,
tempnode.LeftTopY+tempnode.Height,nextnode.LeftTopX
+nextnode.Width/2,nextnode.LeftTopY);
}
}
}//从节点数组循环取节点
g.Dispose();
}//如果节点数组不空
}//repaint()
......
(待续)