System Tray

Haluk

Rules Violation
Joined
Oct 26, 2002
Messages
1,075
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:

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.
 
Hi Ivan;

Sorry for the late answer, I've been out of the city for the last 2 weeks.

I just returned a few hours ago and found your posts.

Your solution to the problem is just VERY GOOD.......

And I believe this is the first ever released solution on the net, for the Excel Userforms.

Best regards from Turkiye (Turkey)
 
Upvote 0

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Upvote 0

Forum statistics

Threads
1,212,927
Messages
6,110,695
Members
448,293
Latest member
jin kazuya

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top