پرستو پارسایی
پنج شنبه 14 اردیبهشت 1402, 10:47 صبح
برای نمایش یا حذف آیکون برنامه در taskbar و انجام رویدادهای خاص ، کد زیر را به کلاس یا فرم پروژه خود وارد کنید :
Private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Integer, ByRef lpData As NOTIFYICONDATA) As Boolean
Private Const NIM_ADD As Integer = &H0
Private Const NIM_MODIFY As Integer = &H1
Private Const NIM_DELETE As Integer = &H2
Private Const NIF_MESSAGE As Integer = &H1
Private Const NIF_ICON As Integer = &H2
Private Const NIF_TIP As Integer = &H4
Private Const WM_MOUSEMOVE As Integer = &H200
Private Const WM_LBUTTONDOWN As Integer = &H201
Private Const WM_LBUTTONUP As Integer = &H202
Private Const WM_RBUTTONDOWN As Integer = &H204
Private Const WM_RBUTTONUP As Integer = &H205
Private Structure NOTIFYICONDATA
Public cbSize As Integer
Public hWnd As IntPtr
Public uID As Integer
Public uFlags As Integer
Public uCallbackMessage As Integer
Public hIcon As IntPtr
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=64)> Public szTip As String
End Structure
Private Sub AddNotifyIcon()
Dim nid As NOTIFYICONDATA
nid.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(nid)
nid.hWnd = Me.Handle
nid.uID = 1
nid.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
nid.uCallbackMessage = WM_MOUSEMOVE
nid.hIcon = My.Resources.icon_name.Handle 'نام فایل آیکون خود را وارد کنید
nid.szTip = "Tooltip Text" 'متن مورد نظر برای توضیحات آیکون را وارد کنید
Shell_NotifyIcon(NIM_ADD, nid)
End Sub
Private Sub RemoveNotifyIcon()
Dim nid As NOTIFYICONDATA
nid.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(nid)
nid.hWnd = Me.Handle
nid.uID = 1
Shell_NotifyIcon(NIM_DELETE, nid)
End Sub
Private Sub NotifyIcon_MouseClick(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
If e.Button = MouseButtons.Left Then
'کد مربوط به کلیک چپ روی آیکون
ElseIf e.Button = MouseButtons.Right Then
'کد مربوط به کلیک راست روی آیکون
End If
End Sub
Private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Integer, ByRef lpData As NOTIFYICONDATA) As Boolean
Private Const NIM_ADD As Integer = &H0
Private Const NIM_MODIFY As Integer = &H1
Private Const NIM_DELETE As Integer = &H2
Private Const NIF_MESSAGE As Integer = &H1
Private Const NIF_ICON As Integer = &H2
Private Const NIF_TIP As Integer = &H4
Private Const WM_MOUSEMOVE As Integer = &H200
Private Const WM_LBUTTONDOWN As Integer = &H201
Private Const WM_LBUTTONUP As Integer = &H202
Private Const WM_RBUTTONDOWN As Integer = &H204
Private Const WM_RBUTTONUP As Integer = &H205
Private Structure NOTIFYICONDATA
Public cbSize As Integer
Public hWnd As IntPtr
Public uID As Integer
Public uFlags As Integer
Public uCallbackMessage As Integer
Public hIcon As IntPtr
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=64)> Public szTip As String
End Structure
Private Sub AddNotifyIcon()
Dim nid As NOTIFYICONDATA
nid.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(nid)
nid.hWnd = Me.Handle
nid.uID = 1
nid.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
nid.uCallbackMessage = WM_MOUSEMOVE
nid.hIcon = My.Resources.icon_name.Handle 'نام فایل آیکون خود را وارد کنید
nid.szTip = "Tooltip Text" 'متن مورد نظر برای توضیحات آیکون را وارد کنید
Shell_NotifyIcon(NIM_ADD, nid)
End Sub
Private Sub RemoveNotifyIcon()
Dim nid As NOTIFYICONDATA
nid.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(nid)
nid.hWnd = Me.Handle
nid.uID = 1
Shell_NotifyIcon(NIM_DELETE, nid)
End Sub
Private Sub NotifyIcon_MouseClick(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
If e.Button = MouseButtons.Left Then
'کد مربوط به کلیک چپ روی آیکون
ElseIf e.Button = MouseButtons.Right Then
'کد مربوط به کلیک راست روی آیکون
End If
End Sub