GRAPHICS AND IMAGE PROCESSING
{LINE=0,RECT=1,LABEL=2}
private struct objGraph
{public objType gType; public object Graph;}
private ArrayList m_Graph=new ArrayList();//用于存放绘制的
//图形
private int curPosX,curPosY, lastX,lastY;
private bool blDrawing=false;
private bool blMoving=false;
private bool blSave=true;
private void ReDraw()//根据 ArrayList 里存放的信息重新绘制
//所有已绘制的图形
{Graphics g=this.CreateGraphics();
Pen penunsel=new Pen(Color.Black,1);
Pen pensel=new Pen(Color.Brown,1);
Pen pen;
for(int i=m_Graph.Count-1;i>=0;i--)//把 ArrayList 里存放的
//信息逐一调出来
{objGraph objGh=(objGraph)(m_Graph[i]);
if(i==idxSelected)
pen=pensel;
else
pen=penunsel;
switch(objGh.gType)
{case objType.LINE:
Graph.Line ln=(Graph.Line)(objGh.Graph);
g.DrawLine(pen,ln.P1,ln.P2);//把原来的直线用新的颜色画出来
break;}}}
private int GetSelectIndex(int x,int y) //给 一 个 坐 标 ,判 断 有 没
//有已绘制的图元被选中,并用不同颜色标识选中的图元
{Graphics g=this.CreateGraphics();
if(idxSelect>=0&&idxSelect
=0&&idxSelected=0&&idxSelectedlastX -curPosX,ln.P1.Y +lastY -curPosY),new Point (ln.P2.X +
lastX-curPosX,ln.P2.Y+lastY-curPosY));
ReDraw();//为防止清除原图元的操作同时也擦掉其他图形上的
//部分像素,重新画一次以前的所有图形画最新的图形
g.DrawLine (pen,new Point (ln.P1.X +e.X -curPosX,ln.P1.Y +e.
Y -curPosY),new Point (ln.P2.X +e.X -curPosX,ln.P2.Y +e.Y -
curPosY));
break;}
lastX=e.X;
lastY=e.Y;
}}}
(5) 在 窗 体 的 MouseUp 事 件 中 添 加 新绘 制 的 图 元 或 保 存
修改过的图元属性到 arraylist 中, 代码片段如下:
private void fr mGraph_MouseUp (object sender, System.
Windows.Forms.MouseEventArgs e)
{if(blDrawing) //保存新绘制的图元
{blDrawing=false;
objGraph objGh;
if(e.X! =curPosX||e.Y! =curPosY)
{switch(pushBtName)
{case "line"://保存直线的代码,其他图元代码略
Graph.Line ln=new Graph_demo.Graph.Line();
ln.P1=new Point(curPosX,curPosY);
ln.P2=new Point(e.X,e.Y);
objGh=new objGraph();
objGh.gType=objType.LINE;
objGh.Graph=ln;
m_Graph.Add(objGh);//把新画的图形加到 arraylist 中
break; }
ReDraw();}}
if(blMoving) //保存修改的图元属性
{blMoving=false;
if(idxSelected>=0&&idxSelected
=0&&idxSelectedGRAPHICS AND IMAGE PROCESSING
this.pic_LT.Visible=true;
this.pic_RB.Visible=true;
this.pic_LT.Cursor=Cursors.Hand;
this.pic_RB.Cursor=Cursors.Hand;
for(int i=2;i