何时上下滚动Winform



c#in c#in c#in there,我想制作一种自定义的胜利表格,它的行为是这样的:

  • 当我双击第一次(从正常大小)时,它会卷起
  • 胜利形式的高度更改为大约20
  • 当我双击第二次时,它将向下滚回原始高度

这是我的版本,但是我看不到它的工作原理,只能起作用,但是当我双击第二次时,它并没有滚下来。

namespace WindowsFormsApplication15
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();                
        }
        bool thisRolled = false;
        public static void resiseMe(Form frm, int newHeight, bool maximIt)
        {
            if (newHeight > 27)
            {
                bool loopEnd = false;
                if (maximIt == false)
                {
                    while (loopEnd == false && frm.Height >= newHeight)
                    {
                        // ticks2 = Stopwatch.GetTimestamp();
                        int thisHeight = frm.Height--;
                        Application.DoEvents();
                        if (thisHeight == newHeight)
                        {
                            loopEnd = true;
                        }
                        //Thread.Sleep(2);
                    }
                }
                else
                {
                    while (loopEnd == false && frm.Height <= newHeight)
                    {
                        int thisHeight = frm.Height++;
                        if (thisHeight == newHeight)
                        {
                            loopEnd = true;
                        }
                    }
                }
            }
        }
        protected override void OnDoubleClick(EventArgs e)
        {                
            if (thisRolled == true)
            {
                resiseMe(this, 28, false);
            }
            else
            {
                resiseMe(this, 320, true);
            }
            base.OnDoubleClick(e);
        }
        protected override void OnSizeChanged(EventArgs e)
        {
            if (this.Height <= 50)
            {
                thisRolled = true;
            }
            else
            {
                thisRolled = false;
            }
            base.OnSizeChanged(e);
        }
    }
}

请有人帮助/建议我如何使它起作用。

尝试一下,它可能会有所帮助

    public static void resiseMe(Form frm, int newHeight)
    {
        new Task(() =>
        {
            int pause = 100;
            int steps = 5;
            int diff = newHeight - frm.Height;
            int adjust = diff / steps;
            for (int i = 0; i < steps; i++)
            {
                frm.Invoke(new MethodInvoker(()=>{
                    frm.Height += adjust;
                    frm.Refresh();
                    System.Threading.Thread.Sleep(pause);
                }));
            }
        }).Start();
    }
    protected override void OnDoubleClick(EventArgs e)
    {                
        if (thisRolled)
        {
            resiseMe(this, this.Height - 50);
        }
        else
        {
            resiseMe(this, this.Height + 50);
        }
        thisRolled = !thisRolled; // flip the thisRolled Value
        base.OnDoubleClick(e);
    }

如果要加快动画的速度,请降低pause变量。

thiseded始终是false。

添加thisRolled = true;在您的第二个循环中,

您的复杂性目前是一种静态方法。因此,您要么需要将此声明为静态(静态bool thisRolled = false;),要么从公共静态void reseme

中删除静态

最新更新