任何人都可以請建議一個好代碼示例 vb.net/c#代碼將應用程序放入系統托盤時最小化。如何在應用程序最小化時將.net應用程序放入系統托盤?
9
A
回答
18
添加NotifyIcon控件到窗體,然後使用下面的代碼:
private void frm_main_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.ShowInTaskbar = false;
this.Hide();
notifyIcon1.Visible = true;
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
notifyIcon1.Visible = false;
}
您可能不需要設置ShowInTaskBar屬性。
+0
您的示例嚴重不完整。 – FlySwat 2008-09-16 19:41:52
+0
你能詳細說一下嗎? – 2008-09-16 19:45:17
2
您可以利用名爲NotifyIcon的內置控件。顯示時會創建一個托盤圖標。 @Phillip的代碼示例有點完整。
有一個疑難雜症,但:
您必須重寫你的應用程序主要形式Dispose方法來調用Dispose NotifyIcon的,否則會留在後申請退出你的托盤。
public void Form_Dispose(object sender, EventArgs e)
{
if (this.Disposing)
notifyIcon1.Dispose();
}
類似的東西。
0
您可以通過將NotifyIcon添加到窗體並處理窗體的resize事件來完成此操作。從托盤手柄返回NotifyIcon的雙擊事件。
如果你想添加一個小動畫,你也可以這樣做......
1)增加以下模塊:
Module AnimatedMinimizeToTray
Structure RECT
Public left As Integer
Public top As Integer
Public right As Integer
Public bottom As Integer
End Structure
Structure APPBARDATA
Public cbSize As Integer
Public hWnd As IntPtr
Public uCallbackMessage As Integer
Public uEdge As ABEdge
Public rc As RECT
Public lParam As IntPtr
End Structure
Enum ABMsg
ABM_NEW = 0
ABM_REMOVE = 1
ABM_QUERYPOS = 2
ABM_SETPOS = 3
ABM_GETSTATE = 4
ABM_GETTASKBARPOS = 5
ABM_ACTIVATE = 6
ABM_GETAUTOHIDEBAR = 7
ABM_SETAUTOHIDEBAR = 8
ABM_WINDOWPOSCHANGED = 9
ABM_SETSTATE = 10
End Enum
Enum ABNotify
ABN_STATECHANGE = 0
ABN_POSCHANGED
ABN_FULLSCREENAPP
ABN_WINDOWARRANGE
End Enum
Enum ABEdge
ABE_LEFT = 0
ABE_TOP
ABE_RIGHT
ABE_BOTTOM
End Enum
Public Declare Function SHAppBarMessage Lib "shell32.dll" Alias "SHAppBarMessage" (ByVal dwMessage As Integer, ByRef pData As APPBARDATA) As Integer
Public Const ABM_GETTASKBARPOS As Integer = &H5&
Public Const WM_SYSCOMMAND As Integer = &H112
Public Const SC_MINIMIZE As Integer = &HF020
Public Sub AnimateWindow(ByVal ToTray As Boolean, ByRef frm As Form, ByRef icon As NotifyIcon)
' get the screen dimensions
Dim screenRect As Rectangle = Screen.GetBounds(frm.Location)
' figure out where the taskbar is (and consequently the tray)
Dim destPoint As Point
Dim BarData As APPBARDATA
BarData.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(BarData)
SHAppBarMessage(ABMsg.ABM_GETTASKBARPOS, BarData)
Select Case BarData.uEdge
Case ABEdge.ABE_BOTTOM, ABEdge.ABE_RIGHT
' Tray is to the Bottom Right
destPoint = New Point(screenRect.Width, screenRect.Height)
Case ABEdge.ABE_LEFT
' Tray is to the Bottom Left
destPoint = New Point(0, screenRect.Height)
Case ABEdge.ABE_TOP
' Tray is to the Top Right
destPoint = New Point(screenRect.Width, 0)
End Select
' setup our loop based on the direction
Dim a, b, s As Single
If ToTray Then
a = 0
b = 1
s = 0.05
Else
a = 1
b = 0
s = -0.05
End If
' "animate" the window
Dim curPoint As Point, curSize As Size
Dim startPoint As Point = frm.Location
Dim dWidth As Integer = destPoint.X - startPoint.X
Dim dHeight As Integer = destPoint.Y - startPoint.Y
Dim startWidth As Integer = frm.Width
Dim startHeight As Integer = frm.Height
Dim i As Single
For i = a To b Step s
curPoint = New Point(startPoint.X + i * dWidth, startPoint.Y + i * dHeight)
curSize = New Size((1 - i) * startWidth, (1 - i) * startHeight)
ControlPaint.DrawReversibleFrame(New Rectangle(curPoint, curSize), frm.BackColor, FrameStyle.Thick)
System.Threading.Thread.Sleep(15)
ControlPaint.DrawReversibleFrame(New Rectangle(curPoint, curSize), frm.BackColor, FrameStyle.Thick)
Next
If ToTray Then
' hide the form and show the notifyicon
frm.Hide()
icon.Visible = True
Else
' hide the notifyicon and show the form
icon.Visible = False
frm.Show()
End If
End Sub
End Module
2)將NotifyIcon添加到您的形式添加以下:
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_SYSCOMMAND AndAlso m.WParam.ToInt32() = SC_MINIMIZE Then
AnimateWindow(True, Me, NotifyIcon1)
Exit Sub
End If
MyBase.WndProc(m)
End Sub
Private Sub NotifyIcon1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles NotifyIcon1.DoubleClick
AnimateWindow(False, Me, NotifyIcon1)
End Sub
相關問題
- 1. 如何將Silverlight應用程序最小化到系統托盤
- 2. 如何最小化應用程序到系統托盤?
- 3. 如何在系統托盤中放入Java應用程序?
- 4. 麻煩以最小化的應用程序到系統托盤
- 5. 窗口系統托盤應用程序像卷應用程序
- 6. 系統托盤應用程序退出
- 7. Windows系統托盤應用程序
- 8. 將應用程序最小化到系統托盤 - 沒有圖標出現
- 9. 最大化系統托盤中的應用程序?
- 10. 使用WPF最小化應用程序到系統托盤(不使用NotifyIcon)
- 11. 最小化整個WPF應用程序到系統托盤在C#
- 12. 使用WPF最小化/關閉應用程序到系統托盤
- 13. 紅寶石鞋:最小化應用程序到Windows上的系統托盤
- 14. 點擊按鈕後,應用程序最小化到系統托盤?
- 15. 如何在系統托盤中保留棱鏡應用程序?
- 16. 如何在Linux中編寫系統托盤的應用程序
- 17. 如何僅在系統托盤中顯示VB應用程序
- 18. Windows Phone Silverlight 8應用程序系統托盤顏色變化
- 19. 如何使用命令行參數將應用程序最小化到托盤
- 20. 在啓動時在系統托盤中放入一個程序
- 21. 如何直接在系統托盤中啓動應用程序? (.NET C#)
- 22. C#程序崩潰的最小化到系統托盤
- 23. 如何添加應用程序到系統托盤
- 24. 如何添加系統托盤圖標到應用程序?
- 25. 在後臺運行程序,將其放入系統托盤中
- 26. 最小化Eclipse RCP托盤應用程序
- 27. 最小化到托盤應用程序後未正確關閉
- 28. 在Ubuntu上使用Clojure的GUI系統托盤應用程序
- 29. c#創建應用程序關閉後保留在托盤中的系統托盤應用程序
- 30. 托盤NotifyIcon丟失,如果應用程序啓動系統
這似乎是http://stackoverflow.com/questions/46918/whats-the-proper-way-to-minimize-to-tray-ac-winforms-app的副本。 – 2008-12-29 16:44:12