I have left his explanations in the User Control intact. When first activated, the "Tray" program starts as a Icon in the system tray surrounded by red. A balloon will appear stating "Connecting to Server". It will normally time out, but I am using a timer to simulate establishing a connection. This causes the balloon to disappear and the red background on the Icon to also disappear. Moving the mouse over the Icon will show "Connected to Server". Ten seconds later, a second timer is used to simulate an incoming message, which will flash with instructions.
Clicking on the Tray Icon will activate a program called "Sample.exe". You will have to compile that program first before it can be activated.
To return the "Tray" program to it's normal state, or to exit the program, right click on the Tray Icon.
J.A. Coutts
Updated 12/06/2017
Last edited by couttsj; Dec 6th, 2017 at 02:37 PM.
This sample program has been modified to change the window that is being subclassed from the UserControl to the main form. The reason for doing this is because only the top level form receives "WM_POWERBROADCAST" messages from the operating system. To test this function, run the Tray Icon program and then let the system go to sleep. When you wake up the system, you should see 2 or 3 Debug messages (4, 7, & 18) depending on how the system was resumed. According to Microsoft, you have about 2 seconds to perform a function after receiving a "PBT_APMSUSPEND" (&H4) message.
Also, the flashing balloon was changed to a steady balloon because of the way that Windows 10 now handles "Toasts".
Not a bug. That code was experimental and unused. Initialize should be:
Code:
Private Sub UserControl_Initialize()
nid.cbSize = LenB(nid)
End Sub
Public Sub Show()
SubclassMe FrmHandle, Me
With nid
.hWnd = FrmHandle
.uId = FrmHandle
.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE Or NIF_INFO
.hIcon = mpicIcon.Handle
.uCallBackMessage = WM_APP_NIF
.szTip = mstrToolTip & vbNullChar
End With
Shell_NotifyIcon NIM_ADD, VarPtr(nid)
mblnShown = True
End Sub
Unlike the UserControl handle, the form handle is not available at the time the UserControl is initialized.
J.A. Coutts
Last edited by couttsj; Dec 6th, 2017 at 11:32 PM.
Attached is a simplified version of the Tray Activation program. It does not use a User Control or subclassing, and is based on PCNotifyIcon by Richard L. McCutchen. Instead it uses a single Class module. The User interface with the Tray Icon is achieved through the only visible part; the icon image. As far as I know, the only thing directly usable is the MouseMove event, and from that he was able to determine Left and Right Button Down. The original code used the Double Click events, but I was unable to make those work on Win 8.1, and I really didn't need them. I then added the Context Menu and the Balloon routines. Usage is pretty much the same as the original sample. So far I have tested it on Win Vista, Win 8.1, & Win 10.
Thanks for sharing.
It is working fine in XP
However the Sample project gives run time error 13, on this line -
IcoHwnd = GetSetting("S Msg", "Settings", "H_Msg")
This also happens when I run Sample project directly (not being called from the Tray application)
I have never used the Registry, so the solution is not jumping off the page at me.
Thanks for sharing.
It is working fine in XP
However the Sample project gives run time error 13, on this line -
IcoHwnd = GetSetting("S Msg", "Settings", "H_Msg")
This also happens when I run Sample project directly (not being called from the Tray application)
I have never used the Registry, so the solution is not jumping off the page at me.
Thanks,
Rob
Any reference to IcoHwnd can be commented out. It was used to test a different program.