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.
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi Raider...
Not an easy task as you need to Minmise the form to the system tray and then setect Mousemove events from this instead of relying on a timer to detect movement over the system tray object.
Also you need either an API routine to handle the Click routines (Which you have from the Forms Mousemove routine)..........I haven't been able to get this to function properly yet ?
Will try further...............time permitting :)
 
Upvote 0
Hi Ivan;

Nice to hear from you. I'll keep working on this and wait for the good news from you :)
 
Upvote 0
Hi Ivan,

And the project itself will be more nice if we can catch the mouse events on the icon when the UserForm is not minimized to the system tray :)
 
Upvote 0
Hi Ivan;

Since the last post, I've tried SubClassing the UserForm to get the messages from the mouse clicks. But, for the time being I'm not able to achieve the goal.

Do you have any suggestions related with the subject ?

Regards,
 
Upvote 0
Hi Raider

I have been able to get this to work. Basically you need to set up your
own CallBack message & Routine and define your WM_USER + x
Where this is your own defined Windows msg.
I'll post it to my site soon.
 
Upvote 0
Ivan-

How did you get the excel icon to appear whenever one mouses over the command button? That is so cool!!
 
Upvote 0
CT Witter said:
Ivan-

How did you get the excel icon to appear whenever one mouses over the command button? That is so cool!!

Hi CT
The button is a commandbutton from the Control Tollbox i.e. it is an ActiveX control similar to ones available from a Userform control.
These controls have the property [MouseIcon] & [Mousepointer] avail via
> design mode > right click control THEN seelect Properties.
You need to use these 2 properties in tandem
I.e. browse for an Icon > select it THEN
[mousepointer] > select 99 frmMousepointercustom.

HTH
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,746
Members
448,989
Latest member
mariah3

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