c# - 如何在 winforms 中第二次显示表单 "smoothly"?

标签 c# winforms form-control

我有一个应该通过单击主窗体中的按钮显示的窗体,我希望当用户关闭第二个窗体时,主窗体再次显示在屏幕中央。我使用以下代码来执行此操作:

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/

相关文章:

c# - 从 Python 或其他语言中使用 MassTransit

c# - 如何在字符串中找到匹配的一对大括号?

c# - 如何以编程方式设置 DataGridViewComboBoxCell 值?

c# - 是否有现成的方法可以确定事件窗口位于多个屏幕中的哪一个?

javascript - 无法使用此嵌套对象数组创建响应式(Reactive)表单

angular - 使用 *ngTemplateOutlet 的条件渲染在 formGroup 内部无法正常工作

C# 属性.设置

c# - .NET MVC 自定义路由

C#.Net 面板控制和 MDI 子表单 - 问题

formarray - 如何正确使用 formGroup 和角度 react 形式的嵌套数据