实例介绍
在Visualc#.NET中使用GDI+动态创建可以用鼠标拖拽方式实现移动、缩放等图形属性修改,实现直线、矩形、文本的创建、移动、缩放功能,绘制的图形可以保存成XML文件,并可以加栽已保存XML文件。
出国s活“出B图,里当! 实用第一氵智慧密集 lastx-curPosX, In P1.Y+lastY-curPosY), new Point(In P2.X+ curPos X=e lastX-cur PosX, In P2.Y+lastY-curPosY) prOsy=e ReDraw:// blOwing=true; ∥ private void pic_ MouseUp (object sender, System. indows g DrawLine(pen, new Point(In P1. X+e.x-curPosX, In P1.Y+e. Forms. MouseEventArgs e) Y-curPosY), new Point (In P2. X+e.X-curPosX, In P2.Y+eY- llastX=e X curPos Y) lastY=eY ouseLeave(sender e): l lastxeX private void pic_ Mouse Move object sender, System. Win lastY=eY dows. Forms. Mouse EventArgs e) } lif(bOving lf(idx Selected<O)return realist objGraph objGh=(obj Graph)(m Graphlidx selected]) Graphics g=this Create Graphics() private void tr m Graph_MouseUp object sender, System. Pen pen=new Pen(Color. Black, 1) Windows Forms. Mouse EventArgs e) Pen pencls =new Pen(this Back Color, 1 iif(blDrawing)∥ tblDrawing=false string picTag=((Picture BOx)sender). Tag ToString() t dx=e. x-cur PosX bigraph objgh t dy=e. Y-curPosY if(e X!=curPosXle Y!=curPosY t ddx=lastX-cur PosX (switch(push BtName case"line"∥/ int ddy=lastY-cur POsY Graph Line In=new Graph_demo Graph Line switch(objGh gType In Pl=new Point(curPosX, curPosY) case objType.LNE:∥ In. P2=new Point(eXeY) Graph Line In=(Graph Line(ob Gh Graph) objgh=new objgraph0 switchipic Tagt objGh. gType=objType LINE, Icase " LT objGh. Graph=In fdx!=0|dy!=0) m_graph. Add(objgh):∥ arraylist g Draw Linelpencls, new PointiIn P1X+ddx, In P1. Y +ddy ), In P2) break; 1 ReDraw( ReDraw 0: 3] g DrawLine(pen, new Point(In P1.X+dx, In P1.Y+dy), In P2) if(bIMoving)∥ break tblMoving=false casc"RB if(idx Selected>=0&&idx Selected<m_Graph. Count if(ddx! =oldy! =0) (Graphics g=this Create Graphics( g Draw Line(pencIs, In P1, new Point(n P2. X+ddx, In P2.Y+ddy)): objGraph objgh=(obj Graph!(m Graph[idx Selected]) ReDraw switch(obj Gh gType g DrawLine(pen, In P1, new Pcint(In P2. X+dx. In P2. Y+dy)): case objType LINE:∥ Graph Line In=-(Graph Line)(obi gh Graph) break g DrawLine(new Pen(this Back Color), In P1, In P2): / last=ex. In P1 -new Point (In P1. X +e.X-curPosX, In P1.Y+e. Y-cur lastY=eY: PcsY);∥ orivate void pic_sel( In P2 -new Point (In P2. x +e. x-curPosX, In P2. Y +e Y -cur- (f(this textInput Visible)return: Box[ aryPi o×目{ Ipic_LT, pic_RB,pic_CT break: 1 pic_rT, pic_ LC, pic_RC, pic_LB, pic_CBh ReDraw0;∥/ if(idx Selected>=0&&idx Selected<m_ Graph. Count lobj Graph obj gh=(obj Graph)(m_ graphlidx Selected]) 6 8 Picturebox switch(objGh g lype) (case objType LINE: / Graph. Line In=(Graph Line)ob Gh Graph) private void pic_Mouse Down (object sender, System. Win- this,pic _LT Location=new Point(In P1. X-3 n P1Y-3 dows. Forms. Mouse EventArgs e) this pic_ RB Location=new Point(In P2 X-3, In P2 Y-3 62 2011.19 C1994-2012ChinaAcademicJournalElectronicPublishingHouse.Allrightsreservedhtlp://www.cnki.net GRAPHICS AND IMAGE PROCESSING this pic_LT Visible=true nodeMsg. AppendChild(eleMsg): y thispic_RB Visible=true; docMsg. Save(dlg. FileName) this pic_ LT Cursor=Cursors. Hand bls this pic_RB Cursor=Cursors. Hand private void multemOpen_Click(cbject sender, System. Even for(int i=2; sary Pic. Length: rgse)∥ ary]. Visible=talse [System. Windows Forms. Open File Dialog dlg =new OpenFile break: ,L Dialog() dIg Filter= (*.graph)* graph for(int 1=0; sary Pic Length; ++ if (dIg Show Dialog(==Dialog Result. OK) ary Pic印].sbe= false} ry private void rt Sizing (Rectangle rt, int dx, int dy, int ddx, int ddy (System. XmL.Xml Document docMsg =new System. XmL. Xml string pic I ag, Pen pen, Pen pencls)t Graphics g=this Create Graphics() docMsg Load(d g FileName) chlpic T ag) System. Xml. XmlNode nodeMsg= dccMsg Select SingloNodo Icase LT (graph/object" g Draw Rectangle(pencls,rt Location X+ddx, rt Location Y +ddy, n_Graph. Clear(;∥ rtSize. Width-ddx, rt Size. Height-ddy obj graph objgh ReDraw( foreach (System. XmL. XmINode nodeltem in node Msg g Draw Rectangle (pen, rt Location X +dx, rt Location Y +dy, rt ChildNodes Size. Width-dx, It Size. Height-dy): (switch(nodeltem Attributes type] Value break: 75 case"LNE":∥ XMI XMI Graph. Line In=new Graph demo Graph Line() In P1=new Point( Convert. T 32 (nodeltem Attributes [ x1 Value), Convert. Iolnt32(nodeltem. Attributesly1"Value! private void multem Save Click(object sender, System. Even- In. P2-new Point(Convert. Tolnt32 (nodeltem Attributes[x2" tArgs e)∥ Value), Convert ToInt 32(nodeltem Attributesl y2" Value) System WindowsForms. Save Dialog dlg =new Save File- objGh=new obiGraph( Dialogo obj gh. g l ype=obj l ype. LINE if(File Name! =)dla FileName=File Name objGh. Graph=In dlg Filter= (* graph)* graph m_ Graph. Add(ob gh dlg. Default Ext="graph break; 11 if (dlg Show Dialog()==DialogResult. OK this Create Graphics().Clear(this Back Color); // (System. Xml. XmlDocument docMsg =new System. Xml. Xml ReDraw: / Document( Icatch(EXception ex string xmlData ="<graph><sum name=+dg FileName+ [Message Box. Show(ex ToString O Message Box But- date="+Date Time. Now ToString (+></sum><object></ob- tons. OK Ject></graph> docMsg. LoadXml(xmlData System. Xm. XmINode nodeMsg= docMsg SelectSingleNode (/graph/object" for(int i=0; i<m_Graph. Count; i ++) Ⅴ isual#NET lobj Graph objGh=(objGraph)im_Graph[l GDI SystemXmLXmlElement eleMsg=docMsg CreateElement'item) eleMsg. SetAttribute(type,objGh gType ToStringO) XML XMI switch(obj ghgType case opjType LINE:∥ Graph Line In=(Graph Line)(objGh. Graph): eleMsg. SetAttribute(x1,In P1.XToString() [ihttp://msdn.microsoftcom/library/,Msdn,2008 eleMsg. SetAttribute(x2", In P2.XToString() GDI+ eleMsg. SetAttribute(y1, In P1. YToString() 2011-06-01) eleMsg. SetAttribute(y2, In P2. Y ToString() break. 2011.19 63 C1994-2012ChinaAcademicJournalElectronicPublishingHouse.Allrightsreservedhtlp://www.cnki.net 【实例截图】
【核心代码】
标签:
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论