新浪首页免费邮件用户注册网站地图
上移动梦网
赢手机大奖

新浪首页 > 科技时代 > 网上学园 > 开 发 者 > 正文
使用C#编写Windows Forms应用程序(下)

http://www.sina.com.cn 2001/10/29 17:50 新浪科技

  文/George Shepherd


  清单 1  
  public struct BoardSpace {
     public BoardSpace(Mark mark,
              int left,
              int top,
              int right,
              int bottom) {
        // Initialize internal state?
     }
     public void SetMark(Player player) {
           // if the space is blank, mark it using
           // the player enumeration
     }
     public void Render(Graphics g) {
        Pen pen =
           new Pen(Color.FromARGB(170, Color.Black), 3);
        switch(m_mark) {
           case Mark.XMark:
              g.DrawLine(pen, m_left, m_top, m_right,
                       m_bottom);
              g.DrawLine(pen, m_left, m_bottom, m_right,
                       m_top);
              break;
           case Mark.OMark:
              int cx = m_right - m_left;
              int cy = m_bottom - m_top;
              g.DrawEllipse(pen, m_left, m_top, cx, cy);
              break;
           default:
              break;
        }
     }
     public Mark m_mark;
     public int m_top, m_left, m_right, m_bottom;
  };

  清单 2
  public struct TicTacToeBoard {
     BoardSpace[,] m_BoardSpaces;
     public void Initialize() {
        m_BoardSpaces = new BoardSpace[3,3];
        // Initialize each space with a location on the screen and a
        //  blank mark.      
        // Here's the first space:
        m_BoardSpaces[0, 0] = new BoardSpace(Mark.Blank, 1,
                    1, 50, 50);
        // Do the rest like that?
     }
     public void ClearBoard() {
        // loop through the spaces clearing them
     }
     public Player EvaluateGame() {
        // Check adjacent marks and see who won.
     }
     public Positions HitTest(int x, int y, Player player) {
        // Test the incoming Coords and mark the right space
        //  using the player enumeration
     }
     public void Render(Graphics g) {
        Pen pen = new Pen(Color.FromARGB(170,
                    Color.Black), 5);
        g.DrawLine(pen, 1, 50, 150, 50);
        g.DrawLine(pen, 50, 1, 50, 150);
        g.DrawLine(pen, 1, 100, 150, 100);
        g.DrawLine(pen, 100, 1, 100, 150);
        for(int i = 0; i < 3; i++) {
           for(int j = 0; j < 3; j++) {
              m_BoardSpaces[i, j].Render(g);
           }
        }
     }
  };

  清单 3
  public class CSharpTicTacToe : Form {
     public Player m_Player = Player.XPlayer;
     TicTacToeBoard m_board = new TicTacToeBoard();
     public CSharpTicTacToe() {
        SetStyle(ControlStyles.Opaque, true);
        Size = new Size(500, 500);
        Text = "CSharp Tic Tac Toe";
        m_board.Initialize();
        //Finally add a button so that we can render to a bitmap
        Button buttonRestart = new Button();
        buttonRestart.Size=new Size(100,50);
        buttonRestart.Location=new Point(300,100);
        buttonRestart.Text="Restart";
        buttonRestart.AddOnClick(new EventHandler(Restart));
        this.Controls.Add(buttonRestart);
     }
     //Fired when the restart button is pressed
     private void Restart(object sender, EventArgs e) {
        m_Player = Player.XPlayer;
        m_board.ClearBoard();
        this.Invalidate();
     }
     protected override void OnMouseDown(MouseEventArgs e) {
        base.OnMouseDown(e);
        Positions position = m_board.HitTest(e.X, e.Y, m_Player);
        if(position == Positions.Unknown) {
           return;
        }
        if(m_Player == Player.XPlayer) {
           m_Player = Player.OPlayer;
        } else {
           m_Player = Player.XPlayer;
        }
           this.Invalidate();
        }
        protected override void OnPaint(PaintEventArgs e) {
           Graphics g = e.Graphics;
           e.Graphics.SmoothingMode =
                    SmoothingMode.AntiAlias;
           g.FillRectangle(new
                    SolidBrush(Color.FromARGB(250,
                    Color.White)), ClientRectangle);
           m_board.Render(g);
        }
        public static void Main() {
           Application.Run(new CSharpTicTacToe());
        }
     }
  }

<< 上一页

发表评论】【初学者园地】【科技聊天】【关闭窗口

新 闻 查 询

 相关链接
微软打算脱离Java语言用C#取代 (2001/10/18 08:34)
Visual C#的SQL Server编程 (2001/10/11 13:57)
“C#”软件开发语言简介 (2001/10/08 17:58)
用C#实现Web文件的上传 (2001/09/12 10:58)
微软果真在控制C#? (2001/04/26 12:48)
微软公司推出适用于“.NET”的“C#


科技时代意见反馈留言板 电话:010-82612286 或 010-82628888-3488   欢迎批评指正

网站简介 | 用户注册 | 广告服务 | 招聘信息 | 中文阅读 | Richwin | 联系方式 | 帮助信息

Copyright © 1996 - 2001 SINA.com, Stone Rich Sight. All Rights Reserved

版权所有 四通利方 新浪网