Minimizing To Tray?

vba Egypt
  • 10 years ago

    i have found a code to minimize to tray

    This goes in your module
    ' Create an Icon in System Tray Needs
    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 WM_MOUSEMOVE = &H200
    Public Const NIF_MESSAGE = &H1
    Public Const NIF_ICON = &H2
    Public Const NIF_TIP = &H4
    Public Const WM_LBUTTONDBLCLK = &H203 'Double-click
    Public Const WM_LBUTTONDOWN = &H201 'Button down
    Public Const WM_LBUTTONUP = &H202 'Button up
    Public Const WM_RBUTTONDBLCLK = &H206 'Double-click
    Public Const WM_RBUTTONDOWN = &H204 'Button down
    Public Const WM_RBUTTONUP = &H205 'Button up

    Public Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean

     

    Then it said 

    This goes in your form1
     Dim nid As NOTIFYICONDATA ' trayicon variable

    '----------------------
    '--- command1 click ---
    '----------------------
    Private Sub Command1_Click()
    minimize_to_tray
    End Sub

    '------------------------
    '--- create tray icon ---
    '------------------------
    Sub minimize_to_tray()
    Me.Hide
    nid.cbSize = Len(nid)
    nid.hwnd = Me.hwnd
    nid.uId = vbNull
    nid.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
    nid.uCallBackMessage = WM_MOUSEMOVE
    nid.hIcon = Me.Icon ' the icon will be your Form1 project icon
    nid.szTip = "blablabla text u want to show when mouse over tray iicon" & vbNullChar
    Shell_NotifyIcon NIM_ADD, nid
    End Sub

    '---------------------------------------------------
    '-- Tray icon actions when mouse click on it, etc --
    '---------------------------------------------------
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    Dim msg As Long
    Dim sFilter As String
    msg = x / Screen.TwipsPerPixelX
    Select Case msg
    Case WM_LBUTTONDOWN
    Me.Show ' show form
    Shell_NotifyIcon NIM_DELETE, nid ' del tray icon
    Case WM_LBUTTONUP
    Case WM_LBUTTONDBLCLK
    Case WM_RBUTTONDOWN
    Case WM_RBUTTONUP
    Me.Show
    Shell_NotifyIcon NIM_DELETE, nid
    Case WM_RBUTTONDBLCLK
    End Select
    End Sub

    '------------------------------
    '--- form Actions On unload ---
    '------------------------------
    Private Sub Form_Unload(Cancel As Integer)
    Shell_NotifyIcon NIM_DELETE, nid ' del tray icon
    end Sub

     

     

    now i asked around for a code that would make it so when i click the minimize button it would go to the tray as well, so this is what i got

     

    Form_Resize just check If Me.WindowState = vbMinimized Then 'put in tray

     

     

    so now my code looks like this

      Dim nid As NOTIFYICONDATA ' trayicon variable
    '------------------------
    '--- create tray icon ---
    '------------------------
    Sub minimize_to_tray()
    Me.Hide
    nid.cbSize = Len(nid)
    nid.hwnd = Me.hwnd
    nid.uId = vbNull
    nid.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
    nid.uCallBackMessage = WM_MOUSEMOVE
    nid.hIcon = Me.Icon ' the icon will be your Form1 project icon
    nid.szTip = "blablabla text u want to show when mouse over tray iicon" & vbNullChar
    Shell_NotifyIcon NIM_ADD, nid
    End Sub

    '---------------------------------------------------
    '-- Tray icon actions when mouse click on it, etc --
    '---------------------------------------------------
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    Dim msg As Long
    Dim sFilter As String
    msg = x / Screen.TwipsPerPixelX
    Select Case msg
    Case WM_LBUTTONDBLCLK
    Me.Show ' show form
    Shell_NotifyIcon NIM_DELETE, nid ' del tray icon
    Case WM_LBUTTONDBLCLK
    Me.Show
    Shell_NotifyIcon NIM_DELETE, nid
    Case WM_RBUTTONDBLCLK
    End Select
    End Sub

    '------------------------------
    '--- form Actions On unload ---
    '------------------------------
    Private Sub Form_Unload(Cancel As Integer)
    Shell_NotifyIcon NIM_DELETE, nid ' del tray icon
    End Sub

    Private Sub Form_Resize()
        If Me.WindowState = vbMinimized Then minimize_to_tray
    End Sub

     

    And now when i minimize it does go to the system tray, but when i double click it to pop up again it does not show up in the taskbar or the tray or on screen, it can only be ENDED in windows task manager

  • 10 years ago

    Hey, you hid & minimised the form, then hid the Icon.

    You need to restore the form first

     Case WM_LBUTTONDBLCLK
        Me.WindowState = vbNormal
        Me.Show ' show form
        Shell_NotifyIcon NIM_DELETE, nid ' del tray icon
    Case WM_LBUTTONDBLCLK
        Me.WindowState = vbNormal
        Me.Show ' show form
        Shell_NotifyIcon NIM_DELETE, nid

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Most software today is very much like an Egyptian pyramid with millions of bricks piled on top of each other, with no structural integrity, but just done by brute force and thousands of slaves” - Alan Kay