7,655
社区成员
发帖
与我相关
我的任务
分享
Public Class NotifyIcon
' Events
Public Custom Event Click As EventHandler
AddHandler(ByVal value As EventHandler)
Me.Click = Delegate.Combine(Me.Click, value)
End AddHandler
RemoveHandler(ByVal value As EventHandler)
Me.Click = Delegate.Remove(Me.Click, value)
End RemoveHandler
End Event
' Methods
Public Sub New()
Me.messageWindow = New MyMessageWindow(Me)
Me.messageWindow.uID = Me.uID
End Sub
Public Sub Add()
If (Not Me._Icon Is Nothing) Then
Me.NotifyMessage(Me.messageWindow.get_Hwnd, 0, Me.uID, Me._Icon.get_Handle)
End If
End Sub
Public Sub Add(ByVal icon As Icon)
Me.NotifyMessage(Me.messageWindow.get_Hwnd, 0, Me.uID, icon.get_Handle)
End Sub
Public Sub Add(ByVal hIcon As IntPtr)
Me.NotifyMessage(Me.messageWindow.get_Hwnd, 0, Me.uID, hIcon)
End Sub
Public Sub Add(ByVal IconRes As String)
Dim hIcon As IntPtr = NotifyIcon.LoadIcon(NotifyIcon.GetModuleHandle(Nothing), IconRes)
Me.NotifyMessage(Me.messageWindow.get_Hwnd, 0, Me.uID, hIcon)
End Sub
Protected Overrides Sub Finalize()
Try
Me.Remove
Finally
MyBase.Finalize
End Try
End Sub
<DllImport("coredll.dll")> _
Friend Shared Function GetFocus() As IntPtr
End Function
<DllImport("coredll.dll")> _
Friend Shared Function GetModuleHandle(ByVal lpModuleName As String) As IntPtr
End Function
<DllImport("coredll.dll")> _
Friend Shared Function LoadIcon(ByVal hInst As IntPtr, ByVal IconName As String) As IntPtr
End Function
Public Sub Modify(ByVal hIcon As IntPtr)
Me.NotifyMessage(Me.messageWindow.get_Hwnd, 1, Me.uID, hIcon)
End Sub
Private Sub NotifyMessage(ByVal hwnd As IntPtr, ByVal dwMessage As Integer, ByVal uID As UInt32, ByVal hIcon As IntPtr)
Dim pnid As New NOTIFYICONDATA
pnid.cbSize = &H98
pnid.hIcon = hIcon
pnid.hWnd = hwnd
pnid.uCallbackMessage = &HBD1
pnid.uFlags = 3
pnid.uID = uID
Dim num As Integer = NotifyIcon.Shell_NotifyIcon(dwMessage, (pnid))
End Sub
Public Sub Remove()
Me.NotifyMessage(Me.messageWindow.get_Hwnd, 2, Me.uID, IntPtr.Zero)
End Sub
<DllImport("coredll.dll")> _
Friend Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As Integer
End Function
<DllImport("coredll.dll")> _
Friend Shared Function Shell_NotifyIcon(ByVal dwMessage As Integer, ByRef pnid As NOTIFYICONDATA) As Integer
End Function
<DllImport("coredll.dll")> _
Friend Shared Function ShowWindow(ByVal hWnd As IntPtr, ByVal nCmdShow As Integer) As Integer
End Function
' Properties
Public WriteOnly Property Icon As Icon
Set(ByVal value As Icon)
Me._Icon = value
End Set
End Property
' Fields
Private _Icon As Icon
Private Click As EventHandler
Private messageWindow As MyMessageWindow
Private Const NIF_ICON As Integer = 2
Private Const NIF_MESSAGE As Integer = 1
Friend Const NIM_ADD As Integer = 0
Friend Const NIM_DELETE As Integer = 2
Friend Const NIM_MODIFY As Integer = 1
Private uID As Integer = &H1388
Friend Const WM_LBUTTONDOWN As Integer = &H201
Friend Const WM_NOTIFY_TRAY As Integer = &HBD1
' Nested Types
Friend Class MyMessageWindow
Inherits MessageWindow
' Methods
Public Sub New(ByVal notIcon As NotifyIcon)
Me.notifyIcon = notIcon
End Sub
Protected Overrides Sub WndProc(ByRef msg As Message)
If (((msg.get_Msg Is &HBD1) AndAlso (msg.get_LParam Is &H201)) AndAlso ((msg.get_WParam Is Me._uID) AndAlso (Not Me.notifyIcon.Click Is Nothing))) Then
Me.notifyIcon.Click.Invoke(Me.notifyIcon, Nothing)
End If
End Sub
' Properties
Public WriteOnly Property uID As Integer
Set(ByVal value As Integer)
Me._uID = value
End Set
End Property
' Fields
Private _uID As Integer = 0
Private notifyIcon As NotifyIcon
End Class
<StructLayout(LayoutKind.Sequential)> _
Friend Structure NOTIFYICONDATA
Friend cbSize As Integer
Friend hWnd As IntPtr
Friend uID As UInt32
Friend uFlags As UInt32
Friend uCallbackMessage As UInt32
Friend hIcon As IntPtr
End Structure
End Class