0

我有一个窗体。我想,当单击某个按钮以动画形式展开表单时,显示表单的新部分(无需表单,并对其中一个进行动画处理)。

那可能吗?

4

1 回答 1

0

您可以通过依赖Timer. 在这里,您有一个示例代码,显示了如何在单击按钮后“动画化”主窗体的大小增加。通过增加/减少所有变量(X/Y inc. 值或间隔),您可以完全控制动画的“外观”。只需在您的表单和下面的代码中包含一个按钮 ( button1) 和一个计时器 ( )。timer1

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{

    public partial class Form1 : Form
    {
        int timerInterval, curWidth, curHeight, incWidth, incHeight, maxWidth, maxHeight;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            curWidth = this.Location.X + this.Width;
            curHeight = this.Location.Y + this.Height;
            incWidth = 100;
            incHeight = 20;
            maxWidth = 2000;
            maxHeight = 1500;
            timerInterval = 100;
            timer1.Enabled = false;
            timer1.Interval = timerInterval;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            curWidth = curWidth + incWidth;
            curHeight = curHeight + incHeight;
            if (curWidth >= maxWidth)
            {
                curWidth = maxWidth;
            }
            if (curHeight >= maxHeight)
            {
                curHeight = maxHeight;
            }

            this.Width = curWidth;
            this.Height = curHeight;

            if (this.Width == maxWidth && this.Height == maxHeight)
            {
                timer1.Stop();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
              timer1.Enabled = !timer1.Enabled;
        }
    }
}
于 2013-07-21T13:58:56.783 回答