`
luozhong915127
  • 浏览: 185751 次
  • 性别: Icon_minigender_1
  • 来自: 湖南
文章分类
社区版块
存档分类
最新评论

C#画板画板实现与问题的解决

阅读更多

 C#项目的解析 

     C#语言继承了CC++语言的特性,并且与java有紧密的联系。C#语言和.NET框架协调工作,一起构建了一个高度优雅的编程环境。可以说C#的核心是面向对象程序设计(opp);

    好吧,我说这么多,就进入软件的操作,呵呵。首先要注意的问题是,Mainm要大些,代码如下:static void  Main()。在命令口编写,应该start/Visual Studio 2005/Visual Studio Tools/Visual Stdio 2005 Command  Prompt,    你就可以在命令口操作了,呵呵,这是高手来的地方哟!嗯,我能发现这个地方我也是高手的高手哟!代码如下:\>csc  Example.cs.

    现在我讲一下我的画板的重绘是怎样实现的,首先,你要清楚重绘在C#的方法是什么,则是onPaint(object senderPaintEventArgs  e),这个方法是重写Froms的方法,而这个界面类继承了它,格式这样的From1Froms,在这里我讲一下抽象类的用方法,因为下面我要用直线类去实现抽象类,我遇到一个问题,嗯,就是我去是实现这个抽象类时,就是要重写抽象类的方法,在voidoverride,可是提示如不加override,否则加new,但是运行报错说“你级别不高”经过一番的查找,终于找到了问题,那就是你的抽象类的方法中的abstract改为Virtual方法。即可。

 

如果是抽象类的方法时候
public override void draw();
 如果是虚拟类的方法时候
public new void draw();

 

 

 

     每一次画的你要你要保存下来,在这里我使用的是List,没有使用ArrayListList是一个接口,是Collection接口的一个子接口。是一个有序的集合。而ArrayListList的一个实现类,可以实现数组大小的可变,可以很方便的进行增加和删减数组内元素的操作。 List list=new ArrayList();这种形式成为向上转型,ArrayList实现了List接口,可以看成是从List继承而来,一个子类的对象可以指向它父类。比如,狗从动物继承而来,狗是一只动物,所以狗的对象可以当作一只普通的动物来看待。肯定你要使用泛类才能保存。把你要的颜色和点和paint等等一些属性封装起来。 在C#画曲线,没有想java里有MoseDrag方法,可是有一个MouseMove这个方法。代码如下:

 

 From1.cs类

C#代码  
<P><SPAN style="FONT-SIZE: 14pt"><DIV class=Section0>   
<PRE class=c# name="code">using System;   
using System.Collections.Generic;   
using System.ComponentModel;   
using System.Data;   
using System.Drawing;   
using System.Linq;   
using System.Text;   
using System.Windows.Forms;   
  
  
namespace WindowsFormsApplication1   
{   
    public partial class Form1 : Form   
    {   
         //定义坐标
        int x1, y1, x2, y2,x3,y3;   
         //定义画布
         Graphics g; 
        // 创建画笔 
        Pen paint = new Pen(Color.Red, 3); 
        // 定义按钮名字 
        String name;  
        // 创建保存形状集合
      List<NetShape> shapes = new List<NetShape>();  
      //新建形状对象
        NetShape shape;   
        public Form1()   
        {   
            InitializeComponent();  
            //获得画布 
            g = this.panel1.CreateGraphics();  
            // 给panel加画笔监听
            this.panel1.Paint += new PaintEventHandle(this.OnPaint);   
             // // 给panel加鼠标监听
            this.panel1.MouseMove += new MouseEventHandler

(this.F_MouseMove);   
        }   

        private void Form1_Load(object sender, EventArgs e)   
        {   
        }   
  
  
        private void button2_Click(object sender, EventArgs e)   
        {   
            //获得按钮的字符串
            name = ((Button)sender).Name;   
             
        }   
          
        public void Form1_MouseDown(object sender, MouseEventArgs 

e)   
        {   
             //得到坐标   
            x1 = e.X;   
            y1 = e.Y;   
  
        }   
  
        public void F_MouseMove(object sender, MouseEventArgs e)   
        {   
             //判断是否按钮符合
            if (name.Equals("button1")) {   
                x3 = e.X;   
                y3 = e.Y; 
                 // 用包装好的类画直线 
                shape = new ImpLine(paint, x1, y1, x3, y3);   
                shape.draw(g);  
                // 添加到集合
                shapes.Add(shape);   
                x1 = x3;   
                y1 = y3;    
            }          
        }   
  
        public void Form1_MouseUp(object sender, MouseEventArgs e) 

  
        {   
            x2 = e.X;   
            y2 = e.Y;   
            if (name.Equals("button2"))   
            {   
  
                shape = new ImpLine(paint, x1, y1, x2, y2);   
                 
            }   
            shape.draw(g);   
            shapes.Add(shape);   
        }   
         /*
           *重绘
           *
         */
        public void OnPaint(object sender, PaintEventArgs e)   
        {   
            //遍历集合的形状
            for(int i=0;i < shapes.Count;i++)   
            {   
                NetShape shape = shapes.ElementAt<NetShape>(i);   
                shape.draw(g);   
  
            }   
        }   
  
          
          
    }   
}   
  
  
</PRE>   
</DIV>   
<P class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt">  直线

类</P>   
<DIV class=Section0>   
<PRE class=c# name="code">using System;   
using System.Collections.Generic;   
using System.Linq;   
using System.Text;   
using System.Drawing;   
  
  
namespace WindowsFormsApplication1   
{   
    public class ImpLine:NetShape   
{   
        int x1, y1, x2, y2;   
        Pen paint;   
        public ImpLine(Pen paint,int x1, int y1, int x2, int y2)   
        {   
            this.x1 = x1;   
            this.x2 = x2;   
            this.y1 = y1;   
            this.y2 = y2;   
            this.paint = paint;   
        }   
        public override void draw(Graphics g)   
        {   
            g.DrawLine( paint,x1, y1, x2, y2);   
  
        }   
  
  
    }   
}   
</PRE>   
</DIV>   
<P class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt"> </P>  

 
<P class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt"> </P>  

 
<P class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt">形状

类</P>   
<P class=p0 style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt"> </P>  

 
  
<DIV class=Section0>   
<PRE class=c# name="code">using System;   
using System.Collections.Generic;   
using System.Linq;   
using System.Text;   
using System.Drawing;   
  
namespace WindowsFormsApplication1   
{   
     public abstract class NetShape   
    {   
  
        public abstract void draw(Graphics g);   
  
    }   
}   
</PRE>   
</DIV>   
</SPAN>   
<P></P><DIV class=Section0>   
<PRE class=c# name="code"></PRE>   
</DIV>   
   


C#代码  

using System;   
using System.Collections.Generic;  
 using System.ComponentModel;   
using System.Data;   using 

System.Drawing;   
using System.Linq;   
using System.Text;  
using System.Windows.Forms;       
namespace WindowsFormsApplication1  
 {       
public partial class Form1 : Form {          
      int x1, y1, x2, y2,x3,y3;            
     Graphics g;          
     Pen  paint = new Pen(Color.Red, 3);          
    String name;         

    List<NetShape> shapes = new List<NetShape>();                       

   NetShape shape;           
      public Form1()  {               

            InitializeComponent();            
          g = this.panel1.CreateGraphics();     

          this.panel1.Paint += new PaintEventHandler(this.OnPaint);      

         this.panel1.MouseMove += new MouseEventHandle(this.F_MouseMove);         
         }                  
        private void Form1_Load(object sender, EventArgs e) {       
        }              
     private void  button2_Click(object sender, EventArgs e)  {             
       name = ((Button)sender).Name;                     
      }                    
    public  void   Form1_MouseDown(object sender, MouseEventArgs e) {          

                    x1 = e.X;               
                   y1 = e.Y;             
     }             
     public   void F_MouseMove(object sender, MouseEventArgs e)           {           

                 if (name.Equals("button1")) {                  
                        x3 = e.X;                   

                        y3 = e.Y;                   
                        shape = new ImpLine(paint, x1, y1, x3, y3);      

                        shape.draw(g);                   
                        shapes.Add(shape);                   

                        x1 = x3;                                 
                       y1 = y3;                  
               }                                         

          }             
            public void Form1_MouseUp(object sender, MouseEventArgs e) {               
                      x2 = e.X;              
                     y2 = e.Y;       

                  if (name.Equals("button2")) {                     
                         shape = new ImpLine(paint, x1, y1, x2, y2);                                }               

                         shape.draw(g);             
                         shapes.Add(shape);                             }       

        public void OnPaint(object sender, PaintEventArgs e) {                      
                          for(int i=0;i < shapes.Count;i++) {                   
                             NetShape shape = shapes.ElementAt<NetShape>(i);                   
                            shape.draw(g);             

                             }          
         }                             
      }  
 }  


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int x1, y1, x2, y2,x3,y3;
         Graphics g;
        Pen paint = new Pen(Color.Red, 3);
        String name;
      List<NetShape> shapes = new List<NetShape>();

       
        NetShape shape;
        public Form1()
        {
            InitializeComponent();
            g = this.panel1.CreateGraphics();
            this.panel1.Paint += new PaintEventHandler(this.OnPaint);
          
            this.panel1.MouseMove += new MouseEventHandler

(this.F_MouseMove);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }


        private void button2_Click(object sender, EventArgs e)
        {
            name = ((Button)sender).Name;
          
        }
       
        public void Form1_MouseDown(object sender, MouseEventArgs 

e)
        {
            
            x1 = e.X;
            y1 = e.Y;

        }

        public void F_MouseMove(object sender, MouseEventArgs e)
        {

            if (name.Equals("button1")) {
                x3 = e.X;
                y3 = e.Y;
                shape = new ImpLine(paint, x1, y1, x3, y3);
                shape.draw(g);
                shapes.Add(shape);
                x1 = x3;
                y1 = y3;
            }  
        }

        public void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            x2 = e.X;
            y2 = e.Y;
            if (name.Equals("button2"))
            {

                shape = new ImpLine(paint, x1, y1, x2, y2);
              
            }
            shape.draw(g);
            shapes.Add(shape);
           


        }
        
        


        
        public void OnPaint(object sender, PaintEventArgs e)
        {
     
            for(int i=0;i < shapes.Count;i++)
            {
                NetShape shape = shapes.ElementAt<NetShape>(i);
                shape.draw(g);

            }
        }

       
       
    }
}




  直线类

C#代码  using System;   using System.Collections.Generic;   using 

System.Linq;   using System.Text;   using System.Drawing;       

namespace WindowsFormsApplication1   {       
public  class  ImpLine:NetShape   {           
               int x1, y1, x2, y2;           
               Pen paint;         

      public ImpLine(Pen paint,int x1, int y1, int x2, int y2) {          

                this.x1 = x1; 
               this.x2 = x2;
               this.y1 = y1;            

               this.y2 = y2; 
              this.paint = paint;          
       }           
        public   override void draw(Graphics g) {                                     g.DrawLine( paint,x1, y1, x2, y2);            
      } 
    }  
 } 

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;


namespace WindowsFormsApplication1
{
    public class ImpLine:NetShape
{
        int x1, y1, x2, y2;
        Pen paint;
        public ImpLine(Pen paint,int x1, int y1, int x2, int y2)
        {
            this.x1 = x1;
            this.x2 = x2;
            this.y1 = y1;
            this.y2 = y2;
            this.paint = paint;
        }
        public override void draw(Graphics g)
        {
            g.DrawLine( paint,x1, y1, x2, y2);

        }


    }
}


 
 
形状类
 


C#代码  using System;  
 using System.Collections.Generic;  
 using  System.Linq;   
 using System.Text;   
using System.Drawing;     

namespace WindowsFormsApplication1   {      
  public abstract class NetShape {           
       public abstract   void draw(Graphics g);         
   } 

 }  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace WindowsFormsApplication1
{
     public abstract class NetShape
    {

        public abstract void draw(Graphics g);

    }
}

 

1
2
分享到:
评论
3 楼 JuliaAilse 2012-03-02  
  提个建议哈!你完全可以拆分为两个或多个博客写啊!可以用写小标题,会显的很有条理。这样太长了,看的我晕晕的。
2 楼 pywepe 2012-02-27  
这是什么东西? 这么长
1 楼 luliangy 2012-02-27  
顶!

相关推荐

Global site tag (gtag.js) - Google Analytics