[英]How to make the startup form initially invisible or hidden
如何使啟動表單最初不可見或隱藏
我有一個帶有2個表單的GUI項目,表單必須單獨顯示。 即,當顯示表單1時,必須隱藏所有其他表單。
我可以隱藏所有其他表單,但我無法隱藏啟動表單。所以我的應用程序的圖標在系統托盤中。
例如,防火牆/防病毒和即時消息應用程序執行此操作,以便在后台運行,並且用戶仍可從系統托盤訪問。
我猜你所問的是如何使表單不出現在任務欄中,只出現在系統托盤中,就像IM或反病毒一樣?
如果是這樣,只需將Form
的ShowInTaskbar
屬性設置為false
。
作為使初步形成無形的,你必須使用ApplicationContext
內Application.Run
而不是主要形式。
將ShowInTaskbar屬性設置為false並將WindowState設置為最小化
微軟寫了一個關於此的網頁。 它給出了使用ApplicationContext
的示例。 基本上沒有表單應用程序,你有一個運行Main()
和Main
的應用程序,然后打開表單。
http://msdn.microsoft.com/en-us/library/Aa984417
但是,這會失去功能,因為您必須禁用“應用程序框架”。 它會讓你的Windows變得丑陋。
這是一個不同的解決方案,幾乎是一個黑客但不是太糟糕。 當Windows啟動您的表單應用程序並將Visible
設置為true時,會導致調用SetVisibleCore
。 您可以覆蓋該功能。 在第一次調用SetVisibleCore
,將其設置為false。 從那時起,只是通過。
請記住,如果表單未顯示,則應用程序啟動時不會觸發Form.Load
因此將所有代碼移動到Sub New()
。
這是整個事情:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.