• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 339
  • Last Modified:

In/Out Board - Right-click menus and Icon

Hi All,
I have a In/Out Board application I would like to run in icon mode ( near the clock ), but when I open it I don't want it on the task bar, just always visible, so when you close it the icon stays near the clock.
Also any way I can get menus by right clicking on the icon?
Also is it hard to get the icon to change? IE from red to green?

Cheers

Steven
0
StevenFay
Asked:
StevenFay
  • 4
  • 3
1 Solution
 
SmashmadCommented:
When you open it.. do you want it always on top?
0
 
SmashmadCommented:
Hi Steven, i hope this work.

'YOU MUST PUT THIS IN A .BAS MODULE

'this type is necessary for the Shell_NotifyIcon API
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

'constants for use with SetWindowLong API
Public Const GWL_WNDPROC = (-4)
Public Const GWL_USERDATA = (-21)

'Constants for use with Shell_NotifyIcon API
Public Const NIM_ADD = &H0
Public Const NIM_MODIFY = &H1
Public Const NIM_DELETE = &H2

'Constants for use with NOTIFYICONDATA Type
Public Const NIF_MESSAGE = &H1
Public Const NIF_ICON = &H2
Public Const NIF_TIP = &H4

'Constants for use in Callback lParam value
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_MBUTTONDBCLICK = &H209

'API declarations for setting the tray icon
'Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean

'API declarations for capturing mouse events in the tray icon
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long



Public IconTray As NOTIFYICONDATA
Private WindowProc As Long



'NOW THE FUNCTONS
Public Sub AddIconTray(Frm As Form, tolTipText As String, icon)
With IconTray
    .cbSize = Len(IconTray)
    .hwnd = Frm.hwnd
    .uId = vbNull
    .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
    .uCallBackMessage = WM_MOUSEMOVE
    .hIcon = icon
    .szTip = tolTipText & vbNullChar
End With
       
Shell_NotifyIcon NIM_ADD, IconTray
       
'set tray callback function
WindowProc = SetWindowLong(Frm.hwnd, GWL_WNDPROC, AddressOf CallBack)
End Sub


Public Function CallBack(ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
       
        'if the user clicked on the tray icon then
        'lets look at how they clicked and decide what to do.
        'when we set the tray icon, we gave it a callback message value of WM_MOUSEMOVE
        'if we receive that value, then we know its the tray icon responding.
       
        'the lParam value specifies what action was performed.
        'ie: left mouse down, middle mouse double click, etc..
        If msg = WM_MOUSEMOVE Then Call Form1.TrayMessage(lParam)
       
        CallBack = CallWindowProc(WindowProc, hwnd, msg, wParam, lParam)
End Function


Public Sub RemoveIconTray()
    'Disable callback
    SetWindowLong FrmMain.hwnd, GWL_WNDPROC, WindowProc
   
    Shell_NotifyIcon NIM_DELETE, IconTray
   
End Sub




'NOW, IN FORM1 (if its called form1, if not, you must change the name in the CallBack function) PLACE THIS CODE:

Public Sub TrayMessage(msg As Long)
Select Case msg
Case WM_LBUTTONDBLCLK 'doble click
     'example.. you can use any of WM_ constant, defined in the module
Case WM_RBUTTONUP 'click con el boton derecho
    'to popup a menu, create it like you create a menu for a form, and use it like that:
    PopupMenu mnuSysTray, , , , mnuTrayOpen
End Select
End Sub


DONT FORGET to call RemoveIconTray(), before exiting your program:
Private Sub Form_Unload(Cancel As Integer)
     Call RemoveIconTray
End Sub


Good Luck!
0
 
SmashmadCommented:
If you dont want it to appear in the taskbar, set form's property
ShowInTaskBar = False

If you want the program to hide in the tray icon instead of close, you have to use the QueryUnload Event:


Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
'If user press the "X" in the top-Right corner
If UnloadMode = 0 Then
    Cancel = 1
    me.hide
End If
End Sub

BE VERY CAREFULL WHILE PROGRAMING WITH THOSE FUNCTIONS.. if you are debuging your app, and you force it to Stop, you will get a system error and VB will close.. (and you could lose your code).. be shure that you call removeiconTry before finish the app.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
StevenFayAuthor Commented:
Thanks for the replies guys
Am playing with it now;
Smashmad when I try and use the ShowInTaskBar it gives me a "Function or interface marked as restricted; or the function uses an automation type not supported in Visual basic"
0
 
StevenFayAuthor Commented:
Thanks for the replies guys
Am playing with it now;
Smashmad when I try and use the ShowInTaskBar it gives me a "Function or interface marked as restricted; or the function uses an automation type not supported in Visual basic"
0
 
StevenFayAuthor Commented:
Thanks for the replies guys
Am playing with it now;
Smashmad when I try and use the ShowInTaskBar it gives me a "Function or interface marked as restricted; or the function uses an automation type not supported in Visual basic"
0
 
SmashmadCommented:
Hi Steven.
What VB version are you using?
you have to set up that property in forms, not in modules. And Im not sure, but it could be a property that you cant set in ejecution time.  So set it with the property dialog of the form.
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now