【问题标题】:C# square splitC# 正方形分割
【发布时间】:2017-04-10 11:05:29
【问题描述】:

我这样做纯粹是出于好奇,而且我已经尝试了好几天,但我似乎被卡住了。

我正在尝试执行以下操作:

目前,我试图让其中一个方块在我每次单击时无限分裂。 这是我正在处理的代码:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace divideSquare
{
    public partial class Form1 : Form
    {
        private Button centerSquare = new Button();
        private Button topLeftSquare = new Button();
        private Button topRightSquare = new Button();
        private Button bottomLeftSquare = new Button();
        private Button bottomRightSquare = new Button();

        public Form1()
        {
            InitializeComponent();
        }

        private void square_Click( object sender, EventArgs e )
        {
            topLeftSquare.Click += new EventHandler( topLeftSquare_Click );
            //topRightSquare.Click += new EventHandler( topRightSquare_Click );
            //bottomLeftSquare.Click += new EventHandler( bottomLeftSquare_Click );
            //bottomRightSquare.Click += new EventHandler( bottomRightSquare_Click );

            topLeftSquare.Size = new System.Drawing.Size( centerSquare.Height / 2, centerSquare.Width / 2 );
            topRightSquare.Size = new System.Drawing.Size( centerSquare.Height / 2, centerSquare.Width / 2 );
            bottomLeftSquare.Size = new System.Drawing.Size( centerSquare.Height / 2, centerSquare.Width / 2 );
            bottomRightSquare.Size = new System.Drawing.Size( centerSquare.Height / 2, centerSquare.Width / 2 );

            topLeftSquare.Location = new Point( 0, 0 );
            topRightSquare.Location = new Point( 50, 0 );
            bottomLeftSquare.Location = new Point( 0, 50 );
            bottomRightSquare.Location = new Point( 50, 50 );

            topLeftSquare.BackColor = Color.Red;
            topRightSquare.BackColor = Color.Red;
            bottomLeftSquare.BackColor = Color.Red;
            bottomRightSquare.BackColor = Color.Red;

            this.Controls.Add( topLeftSquare );
            this.Controls.Add( topRightSquare );
            this.Controls.Add( bottomLeftSquare );
            this.Controls.Add( bottomRightSquare );

            centerSquare.Dispose();
        }

        private void topLeftSquare_Click( object sender, EventArgs e )
        {
            topLeftSquare.Click += new EventHandler( topLeftSquare_Click );
            //topRightSquare.Click += new EventHandler( topRightSquare_Click );
            //bottomLeftSquare.Click += new EventHandler( bottomLeftSquare_Click );
            //bottomRightSquare.Click += new EventHandler( bottomRightSquare_Click );

            topLeftSquare.Size = new System.Drawing.Size( topLeftSquare.Height / 2, topLeftSquare.Width / 2 );
            topRightSquare.Size = new System.Drawing.Size( topLeftSquare.Height / 2, topLeftSquare.Width / 2 );
            bottomLeftSquare.Size = new System.Drawing.Size( topLeftSquare.Height / 2, topLeftSquare.Width / 2 );
            bottomRightSquare.Size = new System.Drawing.Size( topLeftSquare.Height / 2, topLeftSquare.Width / 2 );

            topLeftSquare.Location = new Point( 0, 0 );
            topRightSquare.Location = new Point( 10, 0 );
            bottomLeftSquare.Location = new Point( 0, 10 );
            bottomRightSquare.Location = new Point( 10, 10 );

            topLeftSquare.BackColor = Color.Red;
            topRightSquare.BackColor = Color.Red;
            bottomLeftSquare.BackColor = Color.Red;
            bottomRightSquare.BackColor = Color.Red;

            this.Controls.Add( topLeftSquare );
            this.Controls.Add( topRightSquare );
            this.Controls.Add( bottomLeftSquare );
            this.Controls.Add( bottomRightSquare );

        }

        private void Form1_Load( object sender, EventArgs e )
        {
            centerSquare.Click += new EventHandler( square_Click );
            centerSquare.Size = new System.Drawing.Size( 50, 50 );
            centerSquare.BackColor = Color.Red;
            this.Controls.Add( centerSquare );
        }
    }
}

但是每次我点击一个正方形时,它都不会分裂,而是所有按钮都变得越来越小(这是预期的行为,但它只适用于一个正方形,并且预计也会被切成 4 个) .

任何帮助将不胜感激。

****************************** 编辑 ****************** **************

非常感谢尼诺斯的回答,解决了这个问题。 我做了一些清洁工作,所以对于那些也在寻找解决方案的人来说,这是主要结果。

using System;
using System.Drawing;
using System.Windows.Forms;

namespace divideSquare
{
    public partial class Form1 : Form
    {
        private Random rnd = new Random();
        private int _initHeight = 500;
        private int _initWidth = 500;
        private Button centerSquare = new Button();

        public Form1()
        {
            InitializeComponent();
        }

        private void setSquareLocation( Button[] squareArray, Button senderSquare, int newHeight, int newWidth )
        {
            squareArray[ 0 ].Location = new Point( senderSquare.Left, senderSquare.Top );
            squareArray[ 1 ].Location = new Point( senderSquare.Left + newHeight, senderSquare.Top );
            squareArray[ 2 ].Location = new Point( senderSquare.Left, senderSquare.Top + newWidth );
            squareArray[ 3 ].Location = new Point( senderSquare.Left + newHeight, senderSquare.Top + newWidth );
        }

        private void square_Click( object sender, EventArgs e )
        {
            Button topLeftSquare = new Button();
            Button topRightSquare = new Button();
            Button bottomLeftSquare = new Button();
            Button bottomRightSquare = new Button();
            Button senderSquare = sender as Button;
            Button[] squareArray = { topLeftSquare, topRightSquare, bottomLeftSquare, bottomRightSquare };

            int newSquareHeight = senderSquare.Height / 2;
            int newSquareWidth = senderSquare.Width / 2;

            foreach (var square in squareArray ) {
                square.Click += new EventHandler( square_Click );
                square.Size = new Size( newSquareWidth, newSquareHeight );
                square.BackColor = Color.FromArgb( rnd.Next( 256 ), rnd.Next( 256 ), rnd.Next( 256 ) );
            }

            setSquareLocation( squareArray, senderSquare, newSquareHeight, newSquareWidth );

            foreach ( var square in squareArray ) {
                this.Controls.Add( square );
            }

            this.Controls.Remove( senderSquare );
        }

        private void Form1_Load( object sender, EventArgs e )
        {
            this.Width = _initWidth + 18;
            this.Height = _initHeight + 40;
            this.MaximumSize = new Size(_initWidth + 18, _initHeight + 40 );
            this.MinimumSize = new Size( _initWidth + 18, _initHeight + 40 );

            centerSquare.Click += new EventHandler( square_Click );
            centerSquare.Size = new System.Drawing.Size(_initWidth, _initHeight );
            centerSquare.BackColor = Color.FromArgb( rnd.Next( 256 ), rnd.Next( 256 ), rnd.Next( 256 ) );
            this.Controls.Add( centerSquare );
        }
    }
}

【问题讨论】:

  • 你需要记住,你每次都有更多的方块。但是您的代码正在更改现有的正方形..您需要删除分割的正方形,并将 ti 替换为 4 个新正方形
  • 我注意到,我试图用最少数量的对象来做到这一点,这就是我尝试使用其他方块的原因。我想我需要听从你的提示,对于每个点击的方块,我需要创建 n+3 个方块并处理原来点击的方块。
  • 你不需要创建 n+3,只需缩小你开始的 1 并添加 3 个

标签: c# forms winforms drawing


【解决方案1】:

这是我对此的看法......

首先,我将按钮声明删除到square_Click 方法中,因为每次您需要创建新按钮时,您的代码只会重用在创建表单时实例化的按钮。这是现有代码的最大问题。

其次,由于单击每个按钮执行相同的代码,我将两种方法(square_ClicktopLeftSquare_Click)合并为一种方法。

看看:

public partial class Form1 : Form
{
    private Button centerSquare = new Button();

    public Form1()
    {
        InitializeComponent();
    }

    private void square_Click(object sender, EventArgs e)
    {
        Button topLeftSquare = new Button();
        Button topRightSquare = new Button();
        Button bottomLeftSquare = new Button();
        Button bottomRightSquare = new Button();

        Button senderSquare = sender as Button;
        topLeftSquare.Click += new EventHandler(square_Click);
        topRightSquare.Click += new EventHandler(square_Click);
        bottomLeftSquare.Click += new EventHandler(square_Click);
        bottomRightSquare.Click += new EventHandler(square_Click);

        int newSquareHeight = senderSquare.Height / 2;
        int newSquareWidth = senderSquare.Width / 2;

        topLeftSquare.Size = new System.Drawing.Size(newSquareHeight, newSquareWidth);
        topRightSquare.Size = new System.Drawing.Size(newSquareHeight, newSquareWidth);
        bottomLeftSquare.Size = new System.Drawing.Size(newSquareHeight, newSquareWidth);
        bottomRightSquare.Size = new System.Drawing.Size(newSquareHeight, newSquareWidth);

        topLeftSquare.Location = new Point(senderSquare.Left, senderSquare.Top);
        topRightSquare.Location = new Point(senderSquare.Left + newSquareHeight, senderSquare.Top);
        bottomLeftSquare.Location = new Point(senderSquare.Left, senderSquare.Top + newSquareWidth);
        bottomRightSquare.Location = new Point(senderSquare.Left + newSquareHeight, senderSquare.Top + newSquareWidth);

        topLeftSquare.BackColor = Color.Red;
        topRightSquare.BackColor = Color.Red;
        bottomLeftSquare.BackColor = Color.Red;
        bottomRightSquare.BackColor = Color.Red;

        this.Controls.Add(topLeftSquare);
        this.Controls.Add(topRightSquare);
        this.Controls.Add(bottomLeftSquare);
        this.Controls.Add(bottomRightSquare);

        this.Controls.Remove(senderSquare);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        centerSquare.Click += new EventHandler(square_Click);
        centerSquare.Size = new System.Drawing.Size(50, 50);
        centerSquare.BackColor = Color.Red;
        this.Controls.Add(centerSquare);
    }
}

【讨论】:

  • 这很好。不知道我可以使用像这样的发件人:Button senderSquare = sender as Button; 我试图弄清楚,所以我可以使用我在获取属性上单击的按钮。非常好。
【解决方案2】:

我刚刚尝试了您的代码并进行了一些修改。请使用下面的代码,它按您需要的方式工作。代码是不言自明的。如果您需要任何代码行的帮助,请告诉我。希望对你有帮助

public partial class Form1 : Form
    {
        private Button centerSquare = new Button();
        private Button topLeftSquare = new Button();
        private Button topRightSquare = new Button();
        private Button bottomLeftSquare = new Button();
        private Button bottomRightSquare = new Button();
        public Form1()
        {
            InitializeComponent();
        }

        private void square_Click(object sender, EventArgs e)
        {
            Split(sender as Button);
            centerSquare.Dispose();
        }

        private void topLeftSquare_Click(object sender, EventArgs e)
        {
            Split(sender as Button);
        }

        private void Split(Button source)
        {
            Button topRightSquare = new Button();
            Button bottomLeftSquare = new Button();
            Button bottomRightSquare = new Button();

            topLeftSquare.Click += new EventHandler(topLeftSquare_Click);

            int width = source.Height / 2;

            topLeftSquare.Size = new System.Drawing.Size(width,width);
            topRightSquare.Size = new System.Drawing.Size(width, width);
            bottomLeftSquare.Size = new System.Drawing.Size(width, width);
            bottomRightSquare.Size = new System.Drawing.Size(width, width);

            topLeftSquare.Location = new Point(0, 0);
            topRightSquare.Location = new Point(topLeftSquare.Width, 0);
            bottomLeftSquare.Location = new Point(0, topLeftSquare.Height);
            bottomRightSquare.Location = new Point(topLeftSquare.Width , topLeftSquare.Height );

            topLeftSquare.BackColor = Color.Red;
            topRightSquare.BackColor = Color.Red;
            bottomLeftSquare.BackColor = Color.Red;
            bottomRightSquare.BackColor = Color.Red;

            this.Controls.Add(topLeftSquare);
            this.Controls.Add(topRightSquare);
            this.Controls.Add(bottomLeftSquare);
            this.Controls.Add(bottomRightSquare);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            centerSquare.Click += new EventHandler(square_Click);
            centerSquare.Size = new System.Drawing.Size(400, 400);
            centerSquare.BackColor = Color.Red;
            this.Controls.Add(centerSquare);
        }

    }

【讨论】:

    【解决方案3】:

    您需要一个正方形的层次结构。请参阅下面的更改。我没有调试代码,只是给出了一个基本的问题解决方案。

    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;
    
    namespace divideSquare
    {
        public partial class Form1 : Form
        {
            public ChildSquare root = new ChildSquare();
            public Form1()
            {
                InitializeComponent();
                this.Load += new EventHandler(Form1_Load);
            }
    
            private void square_Click(object sender, EventArgs e)
            {
                ChildSquare  oldButton = sender as ChildSquare;
                oldButton.topLeftSquare.Click += new EventHandler(topLeftSquare_Click);
                //topRightSquare.Click += new EventHandler(topRightSquare_Click);
                //bottomLeftSquare.Click += new EventHandler(bottomLeftSquare_Click);
                //bottomRightSquare.Click += new EventHandler(bottomRightSquare_Click);
    
                oldButton.topLeftSquare.Size = new System.Drawing.Size(oldButton.centerSquare.Height / 2, oldButton.centerSquare.Width / 2);
                oldButton.topRightSquare.Size = new System.Drawing.Size(oldButton.centerSquare.Height / 2, oldButton.centerSquare.Width / 2);
                oldButton.bottomLeftSquare.Size = new System.Drawing.Size(oldButton.centerSquare.Height / 2, oldButton.centerSquare.Width / 2);
                oldButton.bottomRightSquare.Size = new System.Drawing.Size(oldButton.centerSquare.Height / 2, oldButton.centerSquare.Width / 2);
    
                oldButton.topLeftSquare.Location = new Point(0, 0);
                oldButton.topRightSquare.Location = new Point(50, 0);
                oldButton.bottomLeftSquare.Location = new Point(0, 50);
                oldButton.bottomRightSquare.Location = new Point(50, 50);
    
                oldButton.topLeftSquare.BackColor = Color.Red;
                oldButton.topRightSquare.BackColor = Color.Red;
                oldButton.bottomLeftSquare.BackColor = Color.Red;
                oldButton.bottomRightSquare.BackColor = Color.Red;
    
                this.Controls.Add(oldButton.topLeftSquare);
                this.Controls.Add(oldButton.topRightSquare);
                this.Controls.Add(oldButton.bottomLeftSquare);
                this.Controls.Add(oldButton.bottomRightSquare);
    
                oldButton.centerSquare.Dispose();
            }
    
            private void topLeftSquare_Click(object sender, EventArgs e)
            {
                ChildSquare childTopLeftSquare = sender as ChildSquare;
                childTopLeftSquare.topLeftSquare.Click += new EventHandler(topLeftSquare_Click);
                //topRightSquare.Click += new EventHandler(topRightSquare_Click);
                //bottomLeftSquare.Click += new EventHandler(bottomLeftSquare_Click);
                //bottomRightSquare.Click += new EventHandler(bottomRightSquare_Click);
    
                childTopLeftSquare.topLeftSquare.Size = new System.Drawing.Size(childTopLeftSquare.topLeftSquare.Height / 2, childTopLeftSquare.topLeftSquare.Width / 2);
                childTopLeftSquare.topRightSquare.Size = new System.Drawing.Size(childTopLeftSquare.topLeftSquare.Height / 2, childTopLeftSquare.topLeftSquare.Width / 2);
                childTopLeftSquare.bottomLeftSquare.Size = new System.Drawing.Size(childTopLeftSquare.topLeftSquare.Height / 2, childTopLeftSquare.topLeftSquare.Width / 2);
                childTopLeftSquare.bottomRightSquare.Size = new System.Drawing.Size(childTopLeftSquare.topLeftSquare.Height / 2, childTopLeftSquare.topLeftSquare.Width / 2);
    
                childTopLeftSquare.topLeftSquare.Location = new Point(0, 0);
                childTopLeftSquare.topRightSquare.Location = new Point(10, 0);
                childTopLeftSquare.bottomLeftSquare.Location = new Point(0, 10);
                childTopLeftSquare.bottomRightSquare.Location = new Point(10, 10);
    
                childTopLeftSquare.topLeftSquare.BackColor = Color.Red;
                childTopLeftSquare.topRightSquare.BackColor = Color.Red;
                childTopLeftSquare.bottomLeftSquare.BackColor = Color.Red;
                childTopLeftSquare.bottomRightSquare.BackColor = Color.Red;
    
                childTopLeftSquare.Controls.Add(childTopLeftSquare.topLeftSquare);
                childTopLeftSquare.Controls.Add(childTopLeftSquare.topRightSquare);
                childTopLeftSquare.Controls.Add(childTopLeftSquare.bottomLeftSquare);
                childTopLeftSquare.Controls.Add(childTopLeftSquare.bottomRightSquare);
    
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                root.Click += new EventHandler(square_Click);
                root.Size = new System.Drawing.Size(50, 50);
                root.BackColor = Color.Red;
                this.Controls.Add(root);
            }
        }
        public class ChildSquare : Button
        {
            public Button centerSquare = new Button();
            public Button topLeftSquare = new Button();
            public Button topRightSquare = new Button();
            public Button bottomLeftSquare = new Button();
            public Button bottomRightSquare = new Button();
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-09
      • 1970-01-01
      • 1970-01-01
      • 2013-07-16
      • 1970-01-01
      相关资源
      最近更新 更多