Apa itu sistem tray ?? itu loh yang ada di pojok kanan bawah . tau kan ??
Ok sekarang kita lanjut ke code nya ....
pertama kita buat dulu sebuah module baru ( Pilih project lalu add module ) .
lalu copas code di bawah ini dalam modul tersebut .
Option Explicit
Const NIF_MESSAGE As Long = &H1
Const NIF_ICON As Long = &H2
Const NIF_TIP As Long = &H4
Const NIM_ADD As Long = &H0
Const NIM_MODIFY As Long = &H1
Const NIM_DELETE As Long = &H2
Const NIF_INFO As Long = &H10
Private Type NOTIFYICONDATA
cbSize As Long
hwnd As Long
uID As Long
uFlags As Long
uCallbackMessage As Long
hIcon As Long
szTip As String * 128
dwState As Long
dwStateMask As Long
szInfo As String * 256
uTimeout As Long
szInfoTitle As String * 64
dwInfoFlags As Long
End Type
Public Enum TrayRetunEventEnum
MouseMove = &H200
LeftUp = &H202
LeftDown = &H201
LeftDblClick = &H203
RightUp = &H205
RightDown = &H204
RightDblClick = &H206
MiddleUp = &H208
MiddleDown = &H207
MiddleDblClick = &H209
BalloonClick = &H405
BalloonClose = &H404
End Enum
Public Enum ModifyItemEnum
Tray_ToolTip = 1
Tray_Icon = 2
End Enum
Public Enum BalloonIcon
NIIF_NONE = &H0
NIIF_INFO = &H1
NIIF_WARNING = &H2
NIIF_ERROR = &H3
NIIF_GUID = &H4
End Enum
Private TrayIcon As NOTIFYICONDATA
Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Sub TrayAdd(hwnd As Long, Icon As Picture, ToolTip As String)
With TrayIcon
.cbSize = Len(TrayIcon)
.hwnd = hwnd
.uID = vbNull
.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
.uCallbackMessage = &H200
.hIcon = Icon
.szTip = ToolTip & vbNullChar
End With
Shell_NotifyIcon NIM_ADD, TrayIcon
End Sub
Public Sub TrayDelete()
Shell_NotifyIcon NIM_DELETE, TrayIcon
End Sub
Public Sub TrayModify(Item As ModifyItemEnum, vNewValue As Variant)
TrayHideBalloon
Select Case Item
Case Tray_ToolTip
TrayIcon.szTip = vNewValue & vbNullChar
Case Tray_Icon
TrayIcon.hIcon = vNewValue.Handle
End Select
Shell_NotifyIcon NIM_MODIFY, TrayIcon
End Sub
Public Sub TrayShowBalloon(Prompt As String, Title As String, Optional Icon As BalloonIcon = NIIF_INFO)
TrayHideBalloon
With TrayIcon
.uFlags = NIF_ICON Or NIF_TIP Or NIF_INFO Or NIF_MESSAGE Or NIM_MODIFY 'Or NIF_TIP 'NIF_TIP Or NIF_MESSAGE
.szInfo = Prompt & Chr(0)
.szInfoTitle = Title & Chr(0)
.dwInfoFlags = Icon
End With
Shell_NotifyIcon NIM_MODIFY, TrayIcon
End Sub
Public Sub TrayHideBalloon()
With TrayIcon
.uFlags = NIF_ICON Or NIF_TIP Or NIF_INFO Or NIF_MESSAGE Or NIM_MODIFY 'Or NIF_TIP 'NIF_TIP Or NIF_MESSAGE
.szInfo = Chr(0)
.szInfoTitle = Chr(0)
End With
Shell_NotifyIcon NIM_MODIFY, TrayIcon
End Sub
Const NIF_MESSAGE As Long = &H1
Const NIF_ICON As Long = &H2
Const NIF_TIP As Long = &H4
Const NIM_ADD As Long = &H0
Const NIM_MODIFY As Long = &H1
Const NIM_DELETE As Long = &H2
Const NIF_INFO As Long = &H10
Private Type NOTIFYICONDATA
cbSize As Long
hwnd As Long
uID As Long
uFlags As Long
uCallbackMessage As Long
hIcon As Long
szTip As String * 128
dwState As Long
dwStateMask As Long
szInfo As String * 256
uTimeout As Long
szInfoTitle As String * 64
dwInfoFlags As Long
End Type
Public Enum TrayRetunEventEnum
MouseMove = &H200
LeftUp = &H202
LeftDown = &H201
LeftDblClick = &H203
RightUp = &H205
RightDown = &H204
RightDblClick = &H206
MiddleUp = &H208
MiddleDown = &H207
MiddleDblClick = &H209
BalloonClick = &H405
BalloonClose = &H404
End Enum
Public Enum ModifyItemEnum
Tray_ToolTip = 1
Tray_Icon = 2
End Enum
Public Enum BalloonIcon
NIIF_NONE = &H0
NIIF_INFO = &H1
NIIF_WARNING = &H2
NIIF_ERROR = &H3
NIIF_GUID = &H4
End Enum
Private TrayIcon As NOTIFYICONDATA
Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Sub TrayAdd(hwnd As Long, Icon As Picture, ToolTip As String)
With TrayIcon
.cbSize = Len(TrayIcon)
.hwnd = hwnd
.uID = vbNull
.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
.uCallbackMessage = &H200
.hIcon = Icon
.szTip = ToolTip & vbNullChar
End With
Shell_NotifyIcon NIM_ADD, TrayIcon
End Sub
Public Sub TrayDelete()
Shell_NotifyIcon NIM_DELETE, TrayIcon
End Sub
Public Sub TrayModify(Item As ModifyItemEnum, vNewValue As Variant)
TrayHideBalloon
Select Case Item
Case Tray_ToolTip
TrayIcon.szTip = vNewValue & vbNullChar
Case Tray_Icon
TrayIcon.hIcon = vNewValue.Handle
End Select
Shell_NotifyIcon NIM_MODIFY, TrayIcon
End Sub
Public Sub TrayShowBalloon(Prompt As String, Title As String, Optional Icon As BalloonIcon = NIIF_INFO)
TrayHideBalloon
With TrayIcon
.uFlags = NIF_ICON Or NIF_TIP Or NIF_INFO Or NIF_MESSAGE Or NIM_MODIFY 'Or NIF_TIP 'NIF_TIP Or NIF_MESSAGE
.szInfo = Prompt & Chr(0)
.szInfoTitle = Title & Chr(0)
.dwInfoFlags = Icon
End With
Shell_NotifyIcon NIM_MODIFY, TrayIcon
End Sub
Public Sub TrayHideBalloon()
With TrayIcon
.uFlags = NIF_ICON Or NIF_TIP Or NIF_INFO Or NIF_MESSAGE Or NIM_MODIFY 'Or NIF_TIP 'NIF_TIP Or NIF_MESSAGE
.szInfo = Chr(0)
.szInfoTitle = Chr(0)
End With
Shell_NotifyIcon NIM_MODIFY, TrayIcon
End Sub
Lalu pada form ketikan code berikut ini :
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim TrayEvent As TrayRetunEventEnum
TrayEvent = X / Screen.TwipsPerPixelX
Select Case TrayEvent
Case RightDown
SetForegroundWindow (Me.hwnd)
' Disini bisa sobat gunakan untuk popupmenu ketika klik kanan pada icon tray aplikasi sobat
Case LeftDown
SetForegroundWindow (Me.hwnd)
WindowState = vbNormal
Me.Show
Me.SetFocus
End Select
End Sub
Private Sub Form_Resize()
If WindowState = vbMinimized Then
TrayAdd Me.hwnd, Me.Icon, Me.Caption
Me.Hide
Else
TrayDelete
Me.Show
End If
End Sub
Dim TrayEvent As TrayRetunEventEnum
TrayEvent = X / Screen.TwipsPerPixelX
Select Case TrayEvent
Case RightDown
SetForegroundWindow (Me.hwnd)
' Disini bisa sobat gunakan untuk popupmenu ketika klik kanan pada icon tray aplikasi sobat
Case LeftDown
SetForegroundWindow (Me.hwnd)
WindowState = vbNormal
Me.Show
Me.SetFocus
End Select
End Sub
Private Sub Form_Resize()
If WindowState = vbMinimized Then
TrayAdd Me.hwnd, Me.Icon, Me.Caption
Me.Hide
Else
TrayDelete
Me.Show
End If
End Sub
Tidak ada komentar:
Posting Komentar