windows 如何使启动窗体最初不可见或隐藏

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

How to make the startup form initially invisible or hidden

c#.netwindowswinforms

提问by amexn

How to make the startup form initially invisible or hidden

如何使启动窗体最初不可见或隐藏

I have a GUI project with 2 form and the forms have to be displayed separately. i.e. When form 1 is shown, all the other forms must be hidden.

我有一个带有 2 个表单的 GUI 项目,并且这些表单必须单独显示。即当显示表单 1 时,必须隐藏所有其他表单。

I can hide all the other forms, but I cannot hide the startup form.so that my application's icon in the System Tray.

我可以隐藏所有其他表单,但无法隐藏启动表单。这样我的应用程序图标就会出现在系统托盘中。

For example, firewall/antivirus and instant messaging applications do this so as to run in the background and still be accessible to the user from the System Tray.

例如,防火墙/防病毒和即时消息应用程序这样做是为了在后台运行并且用户仍然可以从系统托盘访问。

采纳答案by djdd87

I'm guessing that what you're asking is how to make the form not appear in the task bar and only appear in the system tray, just like an IM or an anti virus?

我猜您要问的是如何使表单不出现在任务栏中而只出现在系统托盘中,就像 IM 或防病毒一样?

If so, just set the ShowInTaskbarproperty of the Formto false.

如果是这样,只需将 的ShowInTaskbar属性设置Formfalse

As for making the initial form invisible, you'll have to use an ApplicationContextwithin Application.Runinstead of the main form.

至于使初始表单不可见,您必须使用ApplicationContext内部Application.Run而不是主表单。

回答by X-Plore-It

set ShowInTaskbar property to false and set the WindowState to minimized

将 ShowInTaskbar 属性设置为 false 并将 WindowState 设置为最小化

回答by Eyal

Microsoft wrote a webpage about this. It gives an example of using the ApplicationContext. Basically instead of having a forms application, you have an app that runs Main()and Mainthen opens the forms.

微软为此写了一个网页。它给出了一个使用ApplicationContext. 基本上,而不是具有窗体应用程序,你有一个应用程序,运行Main()Main然后打开表单。

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

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

You lose a lose of functionality that way, however, because you have to disable the "application framework". It'll make your Windows ugly.

但是,您会因此失去功能,因为您必须禁用“应用程序框架”。它会让你的 Windows 变得丑陋。

Here's a different solution, almost a hack but not too bad. When Windows starts your form app and set Visibleto true, that causes a call to SetVisibleCore. You can override that function. On the first time that SetVisibleCoreis called, set it false. From then out, just pass through.

这是一个不同的解决方案,几乎是一个黑客,但还不错。当 Windows 启动您的表单应用程序并设置Visible为 true 时,这会导致调用SetVisibleCore. 您可以覆盖该功能。在第一次SetVisibleCore调用时,将其设置为 false。从此出去,就过去了。

Keep in mind that Form.Loadwon't trigger when your app starts if the form isn't showing so move all the code there into Sub New().

请记住,Form.Load如果表单未显示,则不会在您的应用程序启动时触发,因此将所有代码移至Sub New().

Here's the whole thing:

这是整件事:

Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    config.LoadFromRegistry() 'this gets config.StartMinimized from the registry
    ' Code that needs to run at start, even if the form isn't showing,
    ' should be here.  Form.Load will only happen when the Form is actually
    ' visible for the first time.
End Sub

Dim FirstSetVisible As Boolean = True

Protected Overrides Sub SetVisibleCore(ByVal value As Boolean)
    If config.StartMinimized And FirstSetVisible Then
        MyBase.SetVisibleCore(False) 'ignore Windows attempt to set Visible
        FirstSetVisible = False 'never do this again
    Else
        MyBase.SetVisibleCore(value)
    End If
End Sub