minimize to system tray

Dear All,

How to minimize my access window to the system tray using VBA code

Thank U 4 help.


Answer this question

minimize to system tray

  • Derek Mehlhorn - MSFT

    MS ISV Buddy Team

    I made a DB, When I open the DB, The Access Application Window becomes hide, just a form appears as stand alone window, and I made a button on that form, when click it, the application window minimize to the system tray, but when I click on the icon in system tray, the Application window and the form appear, I dont like the Application window apears, I need the Application window be hide, and just the form appears, How it is possible

    Thanks


  • Budfoot

    Here's some suggested code by one of our support engineers:

    1.       Add a module in Access and paste following code< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

    =================================

    Option Compare Database 


    Public OldWindowProc As Long
     

    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

    Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

    Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long

    Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

    Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long

    Declare Function GetClassLong Lib "user32" Alias "GetClassLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long

    Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

    Declare Function GetWindowPlacement Lib "user32" (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long

    Declare Function CreatePopupMenu Lib "user32" () As Long

    Declare Function TrackPopupMenu Lib "user32" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal X As Long, ByVal Y As Long, ByVal nReserved As Long, ByVal hwnd As Long, ByVal lprc As Any) As Long

    Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long

    Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Long

    Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

     

    Public Const WM_USER = &H400

    Public Const WM_LBUTTONUP = &H202

    Public Const WM_MBUTTONUP = &H208

    Public Const WM_RBUTTONUP = &H205

    Public Const TRAY_CALLBACK = (WM_USER + 1001&)

    Public Const GWL_WNDPROC = (-4)

    Public Const GWL_USERDATA = (-21)

    Public Const NIF_ICON = &H2

    Public Const NIF_TIP = &H4

    Public Const NIM_ADD = &H0

    Public Const NIF_MESSAGE = &H1

    Public Const NIM_MODIFY = &H1

    Public Const NIM_DELETE = &H2

     

    Public Const SW_HIDE = 0

    Public Const SW_MINIMIZE = 6

    Public Const SW_MAXIMIZE = 3

    Public Const SW_RESTORE = 9

    Public Const SW_SHOWMAXIMIZED = 3

    Public Const SW_SHOW = 5

    Public Const SW_SHOWMINIMIZED = 2

    Public Const SW_SHOWMINNOACTIVE = 7

    Public Const SW_SHOWNA = 8

    Public Const SW_SHOWNOACTIVATE = 4

    Public Const SW_SHOWNORMAL = 1

     

    Public Const GCL_HICON = (-14)

     

    Const MF_CHECKED = &H8&

    Const MF_APPEND = &H100&

    Const TPM_LEFTALIGN = &H0&

    Const MF_DISABLED = &H2&

    Const MF_GRAYED = &H1&

    Const MF_SEPARATOR = &H800&

    Const MF_STRING = &H0&

     

    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 Type POINTAPI

            X As Long

            Y As Long

    End Type

     

    Public Type RECT

            Left As Long

            Top As Long

            Right As Long

            Bottom As Long

    End Type

     

    Public Type WINDOWPLACEMENT

            Length As Long

            flags As Long

            showCmd As Long

            ptMinPosition As POINTAPI

            ptMaxPosition As POINTAPI

            rcNormalPosition As RECT

    End Type

     

     

    Private TheData As NOTIFYICONDATA

    Private hMenu As Long

    Public Function GetWindowHWnd(ByVal WindowName As String) As Long

        GetWindowHWnd = FindWindow(WindowName, vbNullString)

    End Function

    Public Function GetWindowSysMenu(ByVal WindowName As String) As Long

        If GetWindowHWnd(WindowName) <> 0 Then

            GetWindowSysMenu = GetSystemMenu(GetWindowHWnd(WindowName), 0)

         Else

            GetWindowSysMenu = 0

        End If

    End Function

    Public Function GetWindowIcon(ByVal hwnd As Long) As Long

          GetWindowIcon = GetClassLong(hwnd, GCL_HICON)

    End Function

    Public Function GetWindowStatus(ByVal hwnd As Long) As WINDOWPLACEMENT

        Dim winPlacement As WINDOWPLACEMENT

        GetWindowPlacement hwnd, winPlacement

        GetWindowStatus = winPlacement

    End Function

     

    Public Function NewWindowProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

        Dim menuSelection As Integer

        If Msg = TRAY_CALLBACK Then

            If lParam = WM_LBUTTONUP Then

                Debug.Print "Left Button Clicked!"

     

                menuSelection = TrackPopupMenu(hMenu, TPM_LEFTALIGN, Int(lParam / 65536), Int(lParam Mod 65536), 0, hwnd, ByVal 0&)

                Select Case menuSelection

                    Case 1:

                        Application.Quit acQuitSaveAll

                End Select

                Exit Function

            End If

            If lParam = WM_RBUTTONUP Then

                Debug.Print "Right Button Clicked!"

                menuSelection = TrackPopupMenu(GetWindowSysMenu("OMain"), TPM_LEFTALIGN, lParam / 65536, lParam Mod 65536, 0, hwnd, ByVal 0&)

                Exit Function

            End If

           

           

        End If

       

        NewWindowProc = CallWindowProc(OldWindowProc, hwnd, Msg, wParam, lParam)

    End Function

     

    Public Sub AddToTray(frmhWnd As Long, mnuhWnd As Long)

        OldWindowProc = SetWindowLong(frmhWnd, GWL_WNDPROC, AddressOf NewWindowProc)

        With TheData

            .Uid = 0

            .hwnd = frmhWnd

            .cbSize = Len(TheData)

            .hIcon = GetWindowIcon(frmhWnd)

            .UFlags = NIF_ICON

            .UCallbackMessage = TRAY_CALLBACK

            .UFlags = NIF_MESSAGE Or NIF_ICON Or NIF_TIP

            .cbSize = Len(TheData)

        End With

        Shell_NotifyIcon NIM_ADD, TheData

    End Sub

     

    Public Sub RemoveFromTray(frmhWnd As Long)

        With TheData

            .UFlags = 0

        End With

        Shell_NotifyIcon NIM_DELETE, TheData

     

        SetWindowLong frmhWnd, GWL_WNDPROC, OldWindowProc

    End Sub

    Public Sub SetTrayTip(tip As String)

        With TheData

            .SzTip = tip & vbNullChar

            .UFlags = NIF_TIP

        End With

        Shell_NotifyIcon NIM_MODIFY, TheData

    End Sub

     

    Public Sub CreatePopup()

        hMenu = CreatePopupMenu()

        AppendMenu hMenu, MF_STRING, ByVal 0&, "Restore"

        AppendMenu hMenu, MF_GRAYED Or MF_DISABLED, ByVal 0&, "Exit"

        AppendMenu hMenu, MF_SEPARATOR, ByVal 0&, ByVal 0&

        AppendMenu hMenu, MF_CHECKED, ByVal 0&, "About..."

    End Sub

    =================================

     

     

    2.       Add a Form in Access and add following code

    =================================

    Private Sub Form_Load()

        Debug.Print "HWND: " & Hex(GetWindowHWnd("OMain"))

        Debug.Print "MENU: " & Hex(GetWindowSysMenu("OMain"))

        AddToTray GetWindowHWnd("OMain"), GetWindowSysMenu("OMain")

        SetTrayTip "Hollo World"

        CreatePopup

        ShowWindow GetWindowHWnd("OMain"), SW_HIDE

    End Sub

     

    Private Sub Form_Unload(Cancel As Integer)

        RemoveFromTray GetWindowHWnd("OMain")

        ShowWindow GetWindowHWnd("OMain"), SW_SHOW Or SW_RESTORE

        DestroyMenu hMenu

    End Sub

    =================================

     

    3.       Run the Form to have following result.

    I attached the MDB file. Anything unclear, please feel free to let me know. Thanks.


    If you'd like to see the MDB file and the image above, please email us at budsup@microsoft.com.
    thanks,
    -brenda (ISV Buddy Team)



  • Bkuser

    Per the support engineer:

    The ISV wanted to hide his main Access window while his form is visible and this is what he can do in order to achieve this-

    Go to open databasea Toolsa Startupa Uncheck the box for "Display Database Window"

    This should hide the database window. In case we would like to have it back, then we can press the F11 key. Here is a link with the screen shots for the same

    http://www.techonthenet.com/access/database/hide_db.php

    -brenda (ISV Buddy Team)



  • minimize to system tray