什么是“权利"?将 Windows 窗体应用程序置于前台的方法? [英] What is the "right" way to bring a Windows Forms Application to the foreground?
问题描述
我正在用 C# 编写 Windows 窗体应用程序.我需要能够把它带到前台.经过一些谷歌搜索和实验后,我有了一个看起来很笨拙的可行解决方案.
I am writing a Windows Forms Application in C#. I need to be able to bring it to the foreground. After some Googling and experimentation, I have a working solution that looks pretty hacky.
如果有的话,我想知道这样做的优雅方式.我需要应用程序恢复并进入前台,无论它是最小化的,还是没有最小化但在后台.
I would like to know the elegant way to do this, if there is one. I need the app to restore and come to the foreground whether it was minimized, or not minimized but in background.
当前代码如下所示:
WindowState = FormWindowState.Minimized;
WindowState = FormWindowState.Normal;
BringToFront();
Focus();
推荐答案
您是否尝试过 Form.Activate?
此代码似乎可以满足您的要求,将表单最小化后恢复到正常大小,然后激活它以设置焦点:
This code seems to do what you want, by restoring the form to normal size if minimized and then activating it to set the focus:
if (this.WindowState == FormWindowState.Minimized)
{
this.WindowState = FormWindowState.Normal;
}
this.Activate();
警告:这很烦人!如果它只是供您个人使用的应用程序,如您所说,也许您可以接受它.:)
Warning: this is annoying! If it's just an app for your personal use, as you say, maybe you can live with it. :)
这篇关于什么是“权利"?将 Windows 窗体应用程序置于前台的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!