⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 5子棋 有小BUG
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;

namespace GDI_练习
{
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        public System.Windows.Forms.PictureBox pictureBox1;
        private Point p1 = Point.Empty, p2 = Point.Empty;
        private bool isMouseDown = false, isMouseUp = false;

        ArrayList addArray = new ArrayList();
        public struct SharpType
        {
            public string type;
            public Point p1, p2;
            public Color foreColor, backColor;
            public Brush brush;
            public SharpType(string type, Point p1, Point p2, Color foreColor, Color backColor, Brush brush)
            {
                this.type = type;
                this.p1 = p1;
                this.p2 = p2;
                this.foreColor = foreColor;
                this.backColor = backColor;
                this.brush = brush;
            }

        }

        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            //
            // Windows 窗体设计器支持所必需的
            //
            InitializeComponent();

            //
            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            //
        }

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.SuspendLayout();
            // 
            // pictureBox1
            // 
            this.pictureBox1.BackColor = System.Drawing.Color.White;
            this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.pictureBox1.Location = new System.Drawing.Point(0, 0);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(432, 397);
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
            this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
            this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
            this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(432, 397);
            this.Controls.Add(this.pictureBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }

        private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (!isMouseUp)
            {
                this.isMouseDown = true;
                this.p1 = new Point(e.X, e.Y);
            }

        }

        private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            Graphics g = this.pictureBox1.CreateGraphics();
            if (isMouseDown && p2 != Point.Empty)
                g.DrawEllipse(Pens.White, p1.X, p1.Y, Math.Abs(p1.X - p2.X), Math.Abs(p1.Y - p2.Y));


            if (isMouseDown && !isMouseUp)
            {
                p2 = new Point(e.X, e.Y);
                g.DrawEllipse(Pens.Black, p1.X, p1.Y, Math.Abs(p1.X - p2.X), Math.Abs(p1.Y - p2.Y));

            }
            foreach (SharpType type in addArray)
            {
                g.DrawEllipse(Pens.Black, type.p1.X, type.p1.Y, Math.Abs(type.p1.X - type.p2.X), Math.Abs(type.p1.Y - type.p2.Y));

            }
            g.Dispose();
        }

        private void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            this.isMouseDown = false;
            p2 = new Point(e.X, e.Y);
            Graphics g = this.pictureBox1.CreateGraphics();
            g.DrawEllipse(Pens.Black, p1.X, p1.Y, Math.Abs(p1.X - p2.X), Math.Abs(p1.Y - p2.Y));
            addArray.Add(new SharpType("a", p1, p2, Color.Black, Color.Empty, Brushes.Black));
            p1 = Point.Empty;
            p2 = Point.Empty;
            g.Dispose();

        }

        private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            foreach (SharpType type in addArray)
            {
                e.Graphics.DrawEllipse(Pens.Black, type.p1.X, type.p1.Y, Math.Abs(type.p1.X - type.p2.X), Math.Abs(type.p1.Y - type.p2.Y));

            }
        }



    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -