Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以Winforms形式滑动_C#_Winforms_Loops - Fatal编程技术网

C# 以Winforms形式滑动

C# 以Winforms形式滑动,c#,winforms,loops,C#,Winforms,Loops,我正在屏幕底部制作一个表单,希望它向上滑动,因此我编写了以下代码: int destinationX = (Screen.PrimaryScreen.WorkingArea.Width / 2) - (this.Width / 2); int destinationY = Screen.PrimaryScreen.WorkingArea.Height - this.Height; this.Location = new Point(destinationX, destinationY + th

我正在屏幕底部制作一个表单,希望它向上滑动,因此我编写了以下代码:

int destinationX = (Screen.PrimaryScreen.WorkingArea.Width / 2) - (this.Width / 2);
int destinationY = Screen.PrimaryScreen.WorkingArea.Height - this.Height;

this.Location = new Point(destinationX, destinationY + this.Height);

while (this.Location != new Point(destinationX, destinationY))
{
    this.Location = new Point(destinationX, this.Location.Y - 1);
    System.Threading.Thread.Sleep(100);
}

但是代码只是运行并显示结束位置,而没有显示我想要的滑动形式。我尝试过刷新,DoEvents-有什么想法吗?

尝试使用计时器事件而不是循环。

在后台线程中运行代码。例如:

        int destinationX = (Screen.PrimaryScreen.WorkingArea.Width / 2) - (this.Width / 2);
        int destinationY = Screen.PrimaryScreen.WorkingArea.Height - this.Height;

        Point newLocation = new Point(destinationX, destinationY + this.Height);

        new Thread(new ThreadStart(() =>
        {
            do
            {
                 // this line needs to be executed in the UI thread, hence we use Invoke
                this.Invoke(new Action(() => { this.Location = newLocation; }));

                newLocation = new Point(destinationX, newLocation.Y - 1);
                Thread.Sleep(100);
            }
            while (newLocation != new Point(destinationX, destinationY));
        })).Start();

有点黑,但我会试试:)@rs-不黑,这是唯一的好方法。我很惊讶
DoEvents
不起作用,但这确实是一件好事,因为
DoEvents
的黑客行为非常严重。@rs:我在标题中加入了“Winforms”以区别于WPF,并删除了C,因为它已经在标记中,不应该也在标题中。我使用WinForms标记,因为这是您正在使用的技术。你反对的原因是什么?这很好,但我看到它在一台旧机器上运行得太慢了,即使我将睡眠值设置为1。如果它有一个完成动画的总时间,并根据经过的时间量增加位置,则效果会更好。