logo资料库

VisualC__NET中应用GDI_动态创建可移动_拖拽等修改其属性的图形.pdf

第1页 / 共4页
第2页 / 共4页
第3页 / 共4页
第4页 / 共4页
资料共4页,全文预览结束
Visual C# .NET 中应用 GD I+ 动态创建可移 动 、 拖拽等修改其属性的图形 朱卫新 摘 要: 在 Visual C#.NET 中使用 GDI+动态创建可以用鼠标拖拽方式实现移动、 缩放等图形属性 修改, 实现直线、 矩形、 文本的创建、 移动、 缩放功能, 绘制的图形可以保存成 XML 文件, 并可 以加载已保存 XML 文件。 关键词: C#; GDI+; XML; 图形 1 引言 让信息系统的终端用户可以自己绘制图形 , 而且可以以鼠 标拖 拽 的 方 式 方 便 地 实 现 已 绘 制 图 形 的 移 动 、 缩 放 等 修 改 功 能 , 不 仅 极 大 地 方 便 用 户 的 使 用 , 同 时 可 以 满 足 很 多 情 况 下 用户自己现场绘图的 场 景 , 正 是 在 VC# 编 程 环 境 中 使 用 GDI+ 实 现 上 述 功 能 , 使 用 户 不 仅 可 以 自 己 绘 图 , 还 可 以 选 择 修 改 绘制的图形, 并把图形保存成 XML 文件, 供以后加载使用。 2 演示程序 为更加清晰地描述软件功能以及验证方法的可行性 , 制作 了一个演示程序, 演示用户自定义绘图的效果 , 运行界面及其 介绍如图 1 所示。 图 1 工具 条 用 来 设 置 当 前 是 要 绘 制 图 形 还 是 要 选 择 图 形 进 行 修 改 , 选 择 “直 线 ”、 择 “选择” 按钮可以进行图形选择并修改。 “矩 形 ” 和 “文 本 ” 按 钮 时 绘 制 图 形 , 选 当鼠 标 移 动 到 图 形 附 近 时 , 图 形 高 亮 显 示 , 此 时 左 键 单 击 即选中, 鼠标选中图形时显示上 、 下、 左、 右等方向, 可以直 接拖拽修改。 3 实现方法 public class Graph {public Graph(){} public class Line//定义直线的属性和方法 {public Point P1;public Point P2;private int m_Diff=2;//容差 public int Diff {set{if(value>0&&value<100) m_Diff=value;} get{return m_Diff;}} private int XToY(int x) {float k=((float)(P2.Y-P1.Y))/((float)(P2.X-P1.X)); int y=(int)(k*(x-P1.X)+P1.Y); return y;} private int YToX(int y) {float k=((float)(P2.X-P1.X))/((float)(P2.Y-P1.Y)); int x=(int)(k*(y-P1.Y)+P1.X); return x;} public bool IsSelect(Point P) {//鼠标点到直线的水平或垂直距离小于等于指定值时为选中 if(Math.Abs(P1.X-P2.X)>=Math.Abs(P1.Y-P2.Y)) {if(P.X>Math.Max(P1.X,P2.X)||P.XMath.Max(P1.Y,P2.Y)||P.Y
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&&idxSelected
lastX -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&&idxSelected
GRAPHICS 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"; docMsg.LoadXml(xmlData); System.Xml.XmlNode nodeMsg = docMsg.SelectSingleNode ("/graph/object"); for(int i=0;i
分享到:
收藏