vb.net 如何有一个无形的启动窗体?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12393715/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:16:56  来源:igfitidea点击:

How to have an invisible start up form?

vb.netwinformsvisual-studio-2010

提问by user1632018

I have an application that is a part of a solution of projects. In this project I would like for it to start up form to be invisible, but still have a notification icon in the tray visible for this form.

我有一个应用程序,它是项目解决方案的一部分。在这个项目中,我希望它启动表单时不可见,但在此表单的托盘中仍然有一个通知图标。

I know that adding me.hideinto the form_loaddoesn't work. I tried adding a module that instantiates the startup form and I set it as the startup object. Although that didn't work either. I am running out of ideas to have this form invisible. Could anyone help out? I am using VB.NET.

我知道添加me.hideform_load中不起作用。我尝试添加一个实例化启动表单的模块,并将其设置为启动对象。虽然那也没有用。我已经没有办法让这种形式隐形了。有人可以帮忙吗?我正在使用 VB.NET。

回答by Hans Passant

Paste this in your form code:

将此粘贴到您的表单代码中:

Protected Overrides Sub SetVisibleCore(ByVal value As Boolean)
    If Not Me.IsHandleCreated Then
        Me.CreateHandle()
        value = False
    End If
    MyBase.SetVisibleCore(value)
End Sub

The way that works is that the very first request to show the form, done by the Application class, this code overrides the Visible property back to False. The form will behave as normal after this, you can call Show() to make it visible and Close() to close it, even when it was never visible. Note that the Load event doesn't fire until you show it so be sure to move any code in your event handler for it, if any, to the constructor or this override.

工作的方式是,由 Application 类完成的第一个显示表单的请求,此代码将 Visible 属性覆盖回 False。在此之后表单将正常运行,您可以调用 Show() 使其可见,并调用 Close() 关闭它,即使它从不可见。请注意,Load 事件在您显示它之前不会触发,因此请确保将事件处理程序中的任何代码(如果有)移动到构造函数或此覆盖。

回答by UnhandledExcepSean

Put this in the form's Shown event

把它放在表单的 Shown 事件中

Me.Visible = False

回答by craisin

The easiest way is to set the opacity of the form to 0%. When you want it to appear, set it back to 100%

最简单的方法是将表单的不透明度设置为 0%。当您希望它出现时,将其设置回 100%

回答by Mark K

Here is another way that I've found to do this.

这是我发现的另一种方法。

Set the form properties with

设置表单属性

ShowInTaskbar = False

Then in the form's constructor add

然后在表单的构造函数中添加

WindowState = FormWindowState.Minimized

This is very easy to implement and works with no flicker. In my case I also use a NotifyIcon to access the program from the notification tray and just set

这很容易实现并且没有闪烁。在我的情况下,我还使用 NotifyIcon 从通知托盘访问程序并设置

WindowState = FormWindowState.Normal
Show()
BringToFront()

In the Notify_MouseClick event handler.

在 Notify_MouseClick 事件处理程序中。

To hide the form again after displaying it, just minimizing again doesn't quite do the job. In my case I use the Form_Closing event and just hide the form.

要在显示后再次隐藏表单,仅仅再次最小化并不能完成这项工作。就我而言,我使用 Form_Closing 事件并隐藏表单。

Hide()

回答by SimpleCoder

Use Me.Opacity = 0to hide the form on load event.

用于Me.Opacity = 0在加载事件时隐藏表单。

Then use the following code in the form.Shown event

然后在form.Shown事件中使用如下代码

Me.Hide()
Me.Opacity = 100

回答by tcarvin

Just to throw out a completely different approach, have you considered notusing the overload of Application.Run()that takes (and automatically shows) a Form? If you use the one that passes in an ApplicationContext(or more tyoically, your own subclass of ApplicationContext) then you can choose what your behavior is. See here for more details:

只是为了放弃一种完全不同的方法,您是否考虑过使用Application.Run()take (并自动显示) a的重载Form?如果您使用传入 an ApplicationContext(或更典型地,您自己的 子类ApplicationContext)的那个,那么您可以选择您的行为。请参阅此处了解更多详情:

http://msdn.microsoft.com/en-us/library/ms157901

http://msdn.microsoft.com/en-us/library/ms157901

回答by Bogdan Damian

Try this:

尝试这个:

Sub New()
    MyBase.SetVisibleCore(False)
End Sub