C# 启动时隐藏单一表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/70272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Single Form Hide on Startup
提问by Pondidum
I have an application with one form in it, and on the Load method I need to hide the form.
我有一个包含一个表单的应用程序,在 Load 方法上我需要隐藏该表单。
The form will display itself when it has a need to (think along the lines of a outlook 2003 style popup), but I can' figure out how to hide the form on load without something messy.
表单将在需要时自行显示(按照 Outlook 2003 样式弹出窗口的思路思考),但我无法弄清楚如何在加载时隐藏表单而不会出现混乱。
Any suggestions?
有什么建议?
采纳答案by Grokys
I'm coming at this from C#, but should be very similar in vb.net.
我是从 C# 来的,但在 vb.net 中应该非常相似。
In your main program file, in the Main method, you will have something like:
在您的主程序文件中,在 Main 方法中,您将拥有如下内容:
Application.Run(new MainForm());
This creates a new main form and limits the lifetime of the application to the lifetime of the main form.
这将创建一个新的主窗体并将应用程序的生命周期限制为主窗体的生命周期。
However, if you remove the parameter to Application.Run(), then the application will be started with no form shown and you will be free to show and hide forms as much as you like.
但是,如果您删除 Application.Run() 的参数,则应用程序将在不显示任何表单的情况下启动,您可以随意显示和隐藏表单。
Rather than hiding the form in the Load method, initialize the form before calling Application.Run(). I'm assuming the form will have a NotifyIcon on it to display an icon in the task bar - this can be displayed even if the form itself is not yet visible. Calling Form.Show()
or Form.Hide()
from handlers of NotifyIcon events will show and hide the form respectively.
与其在 Load 方法中隐藏窗体,不如在调用 Application.Run() 之前初始化窗体。我假设表单上将有一个 NotifyIcon 以在任务栏中显示一个图标 - 即使表单本身尚不可见,也可以显示该图标。调用Form.Show()
或Form.Hide()
来自 NotifyIcon 事件的处理程序将分别显示和隐藏表单。
回答by aku
Here is a simple approach:
It's in C# (I don't have VB compiler at the moment)
这是一个简单的方法:
它在 C# 中(我目前没有 VB 编译器)
public Form1()
{
InitializeComponent();
Hide(); // Also Visible = false can be used
}
private void Form1_Load(object sender, EventArgs e)
{
Thread.Sleep(10000);
Show(); // Or visible = true;
}
回答by deemer
In the designer, set the form's Visible property to false. Then avoid calling Show() until you need it.
在设计器中,将窗体的 Visible 属性设置为 false。然后避免调用 Show() 直到你需要它。
A better paradigm is to not create an instance of the form until you need it.
更好的范例是在您需要之前不要创建表单的实例。
回答by Roger Willcocks
Launching an app without a form means you're going to have to manage the application startup/shutdown yourself.
在没有表单的情况下启动应用程序意味着您将不得不自己管理应用程序的启动/关闭。
Starting the form off invisible is a better option.
从不可见的形式开始是一个更好的选择。
回答by Matías
I use this:
我用这个:
private void MainForm_Load(object sender, EventArgs e)
{
if (Settings.Instance.HideAtStartup)
{
BeginInvoke(new MethodInvoker(delegate
{
Hide();
}));
}
}
Obviously you have to change the if condition with yours.
显然,您必须更改 if 条件。
回答by Matías
This example supports total invisibility as well as only NotifyIcon in the System tray and no clicks and much more.
此示例支持完全不可见,并且仅支持系统托盘中的 NotifyIcon,并且没有点击等等。
More here: http://code.msdn.microsoft.com/TheNotifyIconExample
回答by Benjamin Autin
Why do it like that at all?
为什么会这样呢?
Why not just start like a console app and show the form when necessary? There's nothing but a few references separating a console app from a forms app.
为什么不像控制台应用程序一样启动并在必要时显示表单?只有一些参考将控制台应用程序与表单应用程序分开。
No need in being greedy and taking the memory needed for the form when you may not even need it.
当你甚至不需要它时,不需要贪婪并占用表单所需的内存。
回答by Benjamin Autin
Extend your main form with this one:
用这个扩展你的主表单:
using System.Windows.Forms;
namespace HideWindows
{
public class HideForm : Form
{
public HideForm()
{
Opacity = 0;
ShowInTaskbar = false;
}
public new void Show()
{
Opacity = 100;
ShowInTaskbar = true;
Show(this);
}
}
}
For example:
例如:
namespace HideWindows
{
public partial class Form1 : HideForm
{
public Form1()
{
InitializeComponent();
}
}
}
More info in this article (spanish):
本文中的更多信息(西班牙语):
回答by Jon Onstott
Based on various suggestions, all I had to do was this:
根据各种建议,我所要做的就是:
To hide the form:
隐藏表格:
Me.Opacity = 0
Me.ShowInTaskbar = false
To show the form:
显示表格:
Me.Opacity = 100
Me.ShowInTaskbar = true
回答by kilsek
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
MainUIForm mainUiForm = new MainUIForm();
mainUiForm.Visible = false;
Application.Run();
}