Hi;
I'm working on a small project and what I want to achieve is, when the UserForm comes on the screen, an icon is to be placed in the system tray right-bottom corner of the screen. Then I want to catch the mouse left-right clicks or the double clicks on this icon.
So far with the codes below, an icon is placed in the system tray but still, I can not catch the mouse events.
On the UserForm, Image1 and 2 CommandButtons are placed. An icon file path is entered to Image1's picture property.
The following goes into the UserForm:
And, the following goes into a standard code Module;
I will be glad to hear from you.
I'm working on a small project and what I want to achieve is, when the UserForm comes on the screen, an icon is to be placed in the system tray right-bottom corner of the screen. Then I want to catch the mouse left-right clicks or the double clicks on this icon.
So far with the codes below, an icon is placed in the system tray but still, I can not catch the mouse events.
On the UserForm, Image1 and 2 CommandButtons are placed. An icon file path is entered to Image1's picture property.
The following goes into the UserForm:
Code:
Private Sub CommandButton1_Click()
CreateIcon
End Sub
'
Private Sub CommandButton2_Click()
DeleteIcon
End Sub
'
Private Sub UserForm_Initialize()
CreateIcon
End Sub
'
Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Dim MyX As Long
[B1] = ActiveWindow.PointsToScreenPixelsX(X)
MyX = [B1]
Select Case MyX
Case WM_MOUSEMOVE
'Me.Caption= "MouseMove!"
Case WM_LBUTTONDOWN
Me.Caption = "Left MouseDown"
Case WM_LBUTTONUP
Me.Caption = "Left MouseUp"
Case WM_LBUTTONDBLCLK
Me.Caption = "Left DoubleClick"
Case WM_RBUTTONDOWN
Me.Caption = "Right MouseDown"
Case WM_RBUTTONUP
Me.Caption = "Right MouseUp"
Case WM_RBUTTONDBLCLK
Me.Caption = "Right DoubleClick"
End Select
End Sub
'
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
DeleteIcon
End Sub
And, the following goes into a standard code Module;
Code:
Public Type NOTIFYICONDATA
cbSize As Long
hWnd As Long
uID As Long
uFlags As Long
uCallbackMessage As Long
hIcon As Long
szTip As String * 64
End Type
'
Public Const NIM_ADD = &H0
Public Const NIM_MODIFY = &H1
Public Const NIM_DELETE = &H2
Public Const NIF_MESSAGE = &H1
Public Const NIF_ICON = &H2
Public Const NIF_TIP = &H4
'
Public Const NIF_DOALL = NIF_MESSAGE Or NIF_ICON Or NIF_TIP
'
Public Const WM_MOUSEMOVE = &H200
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_LBUTTONUP = &H202
Public Const WM_LBUTTONDBLCLK = &H203
Public Const WM_RBUTTONDOWN = &H204
Public Const WM_RBUTTONUP = &H205
Public Const WM_RBUTTONDBLCLK = &H206
Public Const WM_MBUTTONDOWN = &H207
Public Const WM_MBUTTONUP = &H208
Public Const WM_MBUTTONDBLCLK = &H209
'
Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias _
"Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As _
NOTIFYICONDATA) As Long
Declare Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
'
Dim Tic As NOTIFYICONDATA
'
Sub CreateIcon()
Dim hWnd As Long
hWnd = FindWindowA(vbNullString, UserForm1.Caption)
Tic.cbSize = Len(Tic)
Tic.hWnd = hWnd
Tic.uID = 1&
Tic.uFlags = NIF_DOALL
Tic.uCallbackMessage = WM_MOUSEMOVE
Tic.hIcon = UserForm1.Image1.Picture
Tic.szTip = UserForm1.Caption & Chr$(0)
erg = Shell_NotifyIcon(NIM_ADD, Tic)
End Sub
'
Sub DeleteIcon()
erg = Shell_NotifyIcon(NIM_DELETE, Tic)
End Sub
I will be glad to hear from you.