我有一个应该通过单击主窗体中的按钮显示的窗体,我希望当用户关闭第二个窗体时,主窗体再次显示在屏幕中央。我使用以下代码来执行此操作:
private void button_Click(object sender, EventArgs e)
{
this.Hide(); //Hides the main form .
form2.ShowDialog(); //Shows the second form .
this.Show(); // Re-shows the main form after closing the second form ( just in the taskbar , not on the screen ) .
this.StartPosition = FormStartPosition.CenterScreen; // I write this code because I want to show the main form on the screen , not just in the taskbar .
}
这些命令做我想做的事,但问题是在关闭第二个窗体后,主窗体会以小跳转显示,就像眨眼一样! (它不是连续的,它只是在第一次。)我想要的是顺利地做这件事,一开始没有任何眨眼。怎么可能?
提前致谢。
最佳答案
尝试平滑地设置不透明度(主窗体必须将 AllowTransparency
属性设置为 true
)。这是同步执行它的非常基本的方法(阻塞主线程)但是因为它会持续很短的时间所以应该没问题;不需要 Application.DoEvents():
double opacity = 0.00;
while (opacity < 1)
{
Opacity = opacity; // update main form opacity - transparency
opacity += 0.04; // this can be changed
}
Opacity = 1.00 // make sure Opacity is 100% at the end
--- 编辑
请注意,您可以执行相同的操作来隐藏其他形式:只需将初始不透明度设置为 1.00 而不是 0.00,然后在循环中递减 (-=) 而不是递增:
form2.Opacity -= opacity
关于c# - 如何在 winforms 中第二次显示表单 "smoothly"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25105278/