初涉C Sharp

C Sharp or C#,微软公司的某一产品。准确的说其实还是一种编程语言。它与C、C++、Java这三种语言有相似之处。而跟Java相比较,在某些方法可以说是一模一样。今天简单了解了一下Csharp,写了一个画图板程序。可以画直线,椭圆,矩形,还可以选择颜色,重绘也实现了。

一些核心代码:
1、获取画布对象
// 获取画板的画布对象¨
private System.Drawing.Graphics g ;
 g = this.panel1.CreateGraphics();


2、颜色
C Sharp 中有表示颜色的Color类,但改变颜色的话需要用到Pen类。
定义一个按钮来实现颜色选择

private static Color color = Color.Black;
private System.Drawing.Pen p = new System.Drawing.Pen(color);

//按下按钮时触发的事件

public  void  button4_Click(object sender, EventArgs e)
{
System.Windows.Forms.ColorDialog cd = new ColorDialog();
cd.ShowDialog();
color = cd.Color;
p = new Pen(color);
}



添加按钮事件监听器的代码为;
//改变画图板的颜色
this.button4.Click += new EventHandler(button4_Click);


3、鼠标监听器的添加方法
//鼠标的各种监听,此处添加到面板上。
this.panel1.MouseDown += new MouseEventHandler(Form1_MouseDown);
this.panel1.MouseUp += new MouseEventHandler(Form1_MouseUp);

再具体实现监听器的方法。
//鼠标松开
        public void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            x2 = e.X;
            y2 = e.Y;
            Shape shape = new Shape();

            if (s.Equals("Line"))
            {
                g.DrawLine(p, x1, y1, x2, y2);
                shape.x1 = x1;
                shape.y1 = y1;
                shape.x2 = x2;
                shape.y2 = y2;
                shape.item = "Line";
                shape.c = this.p.Color;
                this.list.Add(shape);

            }
            else if (s.Equals("Oval")) 
            {
                g.DrawEllipse(p, x1, y1, Math.Abs(x2-x1), Math.Abs(y2-y1));
                shape.x1 = x1;
                shape.y1 = y1;
                shape.x2 = x2;
                shape.y2 = y2;
                shape.item = "Oval";
                shape.c = this.p.Color;
                this.list.Add(shape);
            }
            else if (s.Equals("Rectangle"))
            {
                g.DrawRectangle(p, x1, y1, Math.Abs(x2 - x1), Math.Abs(y2 - y1));
                shape.x1 = x1;
                shape.y1 = y1;
                shape.x2 = x2;
                shape.y2 = y2;
                shape.item = "Rectangle";
                shape.c = this.p.Color;
                this.list.Add(shape);
            }
        }

        //鼠标按下
        public void Form1_MouseDown(object sender, MouseEventArgs e) 
        {
            x1 = e.X;
            y1 = e.Y;
        }

        class Shape 
        {
            public int x1, x2, y1, y2;
            public String item;
            public Color c;
        }


4、最后就是重绘的方法
C sharp中有相应的重绘方法,为OnPaint (PaintEventArgs e)
重写方法时,方法前要添加override。
 //重绘一下
protected override void OnPaint(PaintEventArgs e) 
{
        for (int i = 0; i < this.list.Count;i++ ) 
        {
             Shape shape = this.list[i];
             if(shape.item.Equals("Line"))
             {
                 Pen pen = new Pen(shape.c);
                 g.DrawLine(pen,shape.x1,shape.y1,shape.x2,shape.y2);
             }
             else if (shape.item.Equals("Oval")) 
             {
                 Pen pen = new Pen(shape.c);
                 g.DrawEllipse(pen, shape.x1, shape.y1, Math.Abs(shape.x2 - shape.x1), Math.Abs(shape.y2 - shape.y1));
              }
              else if (shape.item.Equals("Rectangle"))
              {
                 Pen pen = new Pen(shape.c);
                 g.DrawRectangle(pen, shape.x1, shape.y1, Math.Abs(shape.x2 - shape.x1), Math.Abs(shape.y2 - shape.y1));
              }
        }
}


另外,C Sharp中的队列,List可以使用泛型,但ArrayList貌似不行,这个还有待研究。

你可能感兴趣的:(画图板,重绘,C sharp)