| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1157 人关注过本帖
标题:C#点击按钮画图不刷新
只看楼主 加入收藏
wsbd10086
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-8-31
收藏
 问题点数:0 回复次数:0 
C#点击按钮画图不刷新
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Drawing.Drawing2D;

namespace SnakeTest
{
    class Snake
    {
        IMovingSate movesate;
        public int[][] map;
        public LinkedList<Point> linkSnake;
        SnakeUI snakeUI;

        public Snake(int mapWidth, int mapHeight)
        {
            map = new int[mapHeight][];
            linkSnake = new LinkedList<Point>();
            for (int i = 0; i < mapHeight; i++)
            {
                map[i] = new int[mapWidth];
            }
            Point start = new Point(mapWidth / 2 , mapHeight / 2 );
            linkSnake.AddFirst(start);
        }

        public void MoveLeft()
        {
            movesate = new MoveLeft();
            movesate.UpdateUI += new Action<LinkedList<Point>>(snakeUI.DrawGraph);
            movesate.Move(map, linkSnake);
        }

        public void MoveUp()
        {
            movesate = new MoveUp();
            movesate.UpdateUI += new Action<LinkedList<Point>>(snakeUI.DrawGraph);
            movesate.Move(map, linkSnake);
        }

        public void MoveRight()
        {
            movesate = new MoveRight();
            movesate.UpdateUI += new Action<LinkedList<Point>>(snakeUI.DrawGraph);
            movesate.Move(map, linkSnake);
        }

        public void MoveDown()
        {
            movesate = new MoveDown();
            movesate.UpdateUI += new Action<LinkedList<Point>>(snakeUI.DrawGraph);
            movesate.Move(map, linkSnake);
        }

        public void Run()
        {
            snakeUI = new SnakeUI(this);
            Application.Run(snakeUI);

            //movesate = new MoveLeft();
            //movesate.UpdateUI += new Action<LinkedList<Point>>( snakeUI.DrawGraph);
            //movesate.Move(map, linkSnake);

            //movesate = new MoveUp();
            //movesate.UpdateUI += new Action<LinkedList<Point>>(snakeUI.DrawGraph);
            //movesate.Move(map, linkSnake);
        }


    }

    class SnakeUI : Form
    {
        int cellWidth = 20;//地图格子宽度
        int cellHeiht = 20;//地图格子高度
        int paddingLeft = 10;
        int paddingUp = 40;
        int lineWidth = 1;//线的宽度
        PictureBox pb;
        Snake snake;
        
        public SnakeUI(Snake snake)
        {
            int[][] map = snake.map;
            this.snake = snake;
            this.SuspendLayout();
            this.Text = "SnakeTest Beta";
            pb = new PictureBox();
            pb.Dock = DockStyle.Fill;
            pb.BackColor = Color.White;
            pb.BorderStyle = BorderStyle.Fixed3D;
            int mapWidth = (lineWidth + cellWidth) * map[0].Length + lineWidth;
            int mapHeight = (cellHeiht + lineWidth) * map.Length + lineWidth;
            pb.Width = paddingLeft + mapWidth;
            pb.Height = paddingUp + mapHeight;

            this.Width = pb.Width + paddingLeft * 2+10*2;
            //绘制方向键
            TableLayoutPanel p = new TableLayoutPanel();
            p.RowCount = 1;
            p.ColumnCount = 4;
            p.Dock = DockStyle.Top;
            this.Controls.Add(p);

            Button b = new Button();
            b.Name = "btnLeft";
            b.Click += new EventHandler(ButtonClick);
            b.Text = "Left";
            p.Controls.Add(b);

            b = new Button();
            b.Name = "btnUp";
            b.Click += new EventHandler(ButtonClick);
            b.Text = "Up";
            p.Controls.Add(b);

            b = new Button();
            b.Name = "btnRight";
            b.Click += new EventHandler(ButtonClick);
            b.Text = "Right";
            p.Controls.Add(b);

            b = new Button();
            b.Name = "btnDown";
            b.Click += new EventHandler(ButtonClick);
            b.Text = "Down";
            p.Controls.Add(b);

            p.Height = b.Height + 4;

            DrawGraph(snake.linkSnake);
            this.DoubleBuffered = true;
            this.ResumeLayout(true);

        }

        public void DrawMap()
        {
            int[][] map = snake.map;
            pb = new PictureBox();
            pb.Dock = DockStyle.Fill;
            pb.BackColor = Color.White;
            pb.BorderStyle = BorderStyle.Fixed3D;
            int mapWidth = (lineWidth + cellWidth) * map[0].Length + lineWidth;
            int mapHeight = (cellHeiht + lineWidth) * map.Length + lineWidth;
            pb.Width = paddingLeft + mapWidth;
            pb.Height = paddingUp + mapHeight;

            //绘制地图
            Bitmap flag = new Bitmap(pb.Width, pb.Height);
            Graphics g = Graphics.FromImage(flag);
            g.Clear(Color.White);
            //横线
            for (int i = 0; i <= map[0].Length; i++)
            {
                g.DrawLine(Pens.Black, new Point(0 + paddingLeft, paddingUp + i * (cellHeiht + lineWidth)), new Point(paddingLeft + mapWidth, paddingUp + i * (cellHeiht + lineWidth)));
            }
            //竖线
            for (int j = 0; j <= map.Length; j++)
            {
                g.DrawLine(Pens.Black, new Point(paddingLeft + j * (cellWidth + lineWidth), paddingUp + 0), new Point(paddingLeft + j * (cellWidth + lineWidth), paddingUp + mapHeight));
            }
            pb.Image = flag;
            this.DoubleBuffered = true;
            //return flag;
        }
        
        public void DrawGraph(LinkedList<Point> snake)
        {
            //this.SuspendLayout();
            DrawMap();
            foreach (Point pointAt in snake)
                DrawPoint(pointAt);
            //this.ResumeLayout();
            this.Controls.Remove(pb);
            this.Controls.Add(pb);
            this.Refresh();
            Thread.Sleep(100);
            Application.DoEvents();
        }

        private void DrawPoint(Point pointAt)
        {
            //this.Controls.Remove(pb);
            if (pb.Image == null)
                pb.Image = new Bitmap(pb.Width, pb.Height);
            Graphics g = Graphics.FromImage(pb.Image);
            Rectangle rec = new Rectangle(pointAt.X * (cellWidth + lineWidth) + paddingLeft + lineWidth, pointAt.Y * (cellHeiht + lineWidth) + paddingUp + lineWidth, cellWidth, cellHeiht);
            Image bgImg = new Bitmap(cellWidth - 1, cellHeiht - 1);
            GraphicsPath graphPath = new GraphicsPath();
            graphPath.AddRectangle(rec);
            g.FillPath(new SolidBrush(Color.Blue), graphPath);
            //pb.Image = new Bitmap(pb.Width, pb.Height);
            //pb = new PictureBox();
           // pb.Image = map;
            //this.Controls.Add(pb);
            pb.Image.Save("D:\\a.bmp");

        }

        private void ButtonClick(object sender, EventArgs e)
        {
            switch ((sender as Button).Text)
            {
                case"Left":
                    snake.MoveLeft();
                    break;
                case"Up":
                    snake.MoveUp();
                    break;
                case "Right":
                    snake.MoveRight();
                    break;
                case "Down":
                    snake.MoveDown();
                    break;
            }
        }

        static void Main()
        {
            Snake snake = new Snake(11, 11);
            snake.Run();
        }
    }

    interface IMovingSate
    {
        event Action<LinkedList<Point>> UpdateUI;
        void Move(int[][] map, LinkedList<Point> snake);
    }

    class MoveUp : IMovingSate
    {
        public event Action<LinkedList<Point>> UpdateUI;

        public void Move(int[][] map, LinkedList<Point> snake)
        {
            Point last = snake.Last.Value;
            Point first = snake.First.Value;

            if (first.Y - 1 > 0)
            {
                first.Y--;
                snake.RemoveLast();
                snake.AddFirst(first);
                if (UpdateUI != null)
                    UpdateUI(snake);
            }
        }
    }

    class MoveLeft : IMovingSate
    {
        public event Action<LinkedList<Point>> UpdateUI;
        public void Move(int[][] map, LinkedList<Point> snake)
        {
            Point last = snake.Last.Value;
            Point first = snake.First.Value;

            if (first.X - 1 > 0)
            {
                first.X--;
                snake.RemoveLast();
                snake.AddFirst(first);
                UpdateUI(snake);
            }
        }
    }

    class MoveDown : IMovingSate
    {
        public event Action<LinkedList<Point>> UpdateUI;
        public void Move(int[][] map, LinkedList<Point> snake)
        {
            Point last = snake.Last.Value;
            Point first = snake.First.Value;

            if (first.Y + 1 < map.Length)
            {
                first.Y++;
                snake.RemoveLast();
                snake.AddFirst(first);
                UpdateUI(snake);
            }
        }
    }

    class MoveRight : IMovingSate
    {
        public event Action<LinkedList<Point>> UpdateUI;
        public void Move(int[][] map, LinkedList<Point> snake)
        {
            Point last = snake.Last.Value;
            Point first = snake.First.Value;

            if (first.Y + 1 < map[0].Length)
            {
                first.X++;
                snake.RemoveLast();
                snake.AddFirst(first);
                UpdateUI(snake);
            }
        }
    }

   
}
搜索更多相关主题的帖子: 画图 public 
2012-08-31 12:08
快速回复:C#点击按钮画图不刷新
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018427 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved