2011年12月31日星期六

C#生活模仿

软件就像生活,生活中的人,

代表一个类(Class),他有自己的特点(属性),

这个特点有时候又会变化(属性的访问get,set)

下面是模仿杯中倒水的代码

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;using System.IO;namespace WaterPumper{public partial class Form1 : Form{private Point _leftPoint = Point.Empty;public Point LeftPoint{get { return _leftPoint; }set { _leftPoint = value; }}private Point _rightPoint = Point.Empty;public Point RightPoint{get { return _rightPoint; }set { _rightPoint = value; }}private Point _deltaLeftPoint = Point.Empty;public Point DeltaLeftPoint{get { return _deltaLeftPoint; }set { _deltaLeftPoint = value; }}private Point _deltaRightPoint = Point.Empty;public Point DeltaRightPoint{get { return _deltaRightPoint; }set { _deltaRightPoint = value; }} private int delta_y = 3;private int delta_x = 0;public Bitmap bmap = null;public Form1(){InitializeComponent();}private void timer1_Tick(object sender, EventArgs e){bmap = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);delta_y = delta_y + 1;if (this.delta_y < 0){timer1.Enabled = false;timer1.Stop();}Pump(delta_x, delta_y);}public void Pump(int x,int y){y = delta_y;x = delta_x;_leftPoint = new Point(this.pictureBox1.Location.X - 12, (this.pictureBox1.Height - y));_rightPoint = new Point(this.pictureBox1.Width, (this.pictureBox1.Height - y));Graphics grap = Graphics.FromImage(bmap);Pen pen = new Pen(Brushes.Blue, 3 + delta_y + delta_y);grap.DrawLine(pen,_leftPoint,_rightPoint);this.pictureBox1.Image = bmap;}private void button1_Click(object sender, EventArgs e){this.timer1.Enabled = true;this.timer1.Start();}private void Form1_Load(object sender, EventArgs e){bmap = new Bitmap(this.pictureBox1.Width,this.pictureBox1.Height);Pump(delta_x, delta_y);}private void btnStop_Click(object sender, EventArgs e){this.timer1.Enabled = false;this.timer1.Stop();}}}

  


C#生活模仿

TAG: