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
How to make the startup form initially invisible or hidden
提问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 ShowInTaskbar
property of the Form
to false
.
如果是这样,只需将 的ShowInTaskbar
属性设置Form
为false
。
As for making the initial form invisible, you'll have to use an ApplicationContext
within Application.Run
instead 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 Main
then 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 Visible
to true, that causes a call to SetVisibleCore
. You can override that function. On the first time that SetVisibleCore
is called, set it false. From then out, just pass through.
这是一个不同的解决方案,几乎是一个黑客,但还不错。当 Windows 启动您的表单应用程序并设置Visible
为 true 时,这会导致调用SetVisibleCore
. 您可以覆盖该功能。在第一次SetVisibleCore
调用时,将其设置为 false。从此出去,就过去了。
Keep in mind that Form.Load
won'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