按钮不会按比例调整大小

时间:2014-08-03 20:14:10

标签: c# resize

我正在开发一个Paint程序,我需要相对于Resizing事件调整表单中的所有按钮。

示例:

表单启动大小(800,600)

按钮启动大小(41,41)

如果我将表单调整为(1000,900),则按钮必须调整为(241,341)

之后..

如果我将表单调整为(900,800),则按钮必须调整为(141,241)

之后..

如果我将表单调整为(400,400),则按钮必须调整为(41,41)

我尝试编写代码来实现这一点,但它确实有效。

但是当我减少表单的大小时,它会不断减少,我可以看到按钮几乎消失,而不是保持默认大小(41,41)

注意:如果使用向下传递按钮大小的值减少表单,按钮应保持默认大小(41,41)

Pen是一个按钮。

我的代码:

    private int previousHeight;
    private int previousWidth;
    private int OriginalRectSize = 41;

     public Form1()
    {
        InitializeComponent();
        previousHeight = this.Size.Height;
        previousWidth = this.Size.Width;
    }


 private void Form1_Resize(object sender, EventArgs e)
    {

            int newWidth = this.Width - previousWidth; // difference value to add/decrease
            int newHeight = this.Height - previousHeight; // difference value to add/decrease

            //decrease case
            if (newWidth < 0 && newHeight < 0)
            {
                //below button's default size case
                if ((Pen.Width + newWidth) < OriginalRectSize || (Pen.Height + newHeight) < OriginalRectSize)
                {
                    Pen.Width = OriginalRectSize;
                    Pen.Height = OriginalRectSize;
                }
                //decrease 
                else
                {
                    Pen.Width = Pen.Width + newWidth;
                    Pen.Height = Pen.Height + newHeight;
                }
            }

            // increase case
            else
            {
                Pen.Width = Pen.Width + newWidth;
                Pen.Height = Pen.Height + newHeight;
            }


        //set new form size
            previousHeight = this.Height;
            previousWidth = this.Width;

    }

0 个答案:

没有答案