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。如果它有一个完成动画的总时间,并根据经过的时间量增加位置,则效果会更好。