Results 1 to 3 of 3

Thread: [RESOLVED] Minimize to Sys Tray

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    294

    Resolved [RESOLVED] Minimize to Sys Tray

    When you click the "Lock" menu item in my program, I want it to minimize to the system tray, then show the Login form when you click the system tray icon. It works fine, except it doesn't detect the "MouseMove" event when you click the icon.

    Here's my Code:
    Code:
    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
                frmLogin.Show
                Shell_NotifyIcon NIM_DELETE, nid ' del tray icon
            Case WM_LBUTTONUP
            Case WM_LBUTTONDBLCLK
            Case WM_RBUTTONDOWN
            Case WM_RBUTTONUP
                frmLogin.Show
                Shell_NotifyIcon NIM_DELETE, nid
            Case WM_RBUTTONDBLCLK
        End Select
        
    End Sub
    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 nid As NOTIFYICONDATA
    
    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
    Code:
    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 = "Sun Life Dashboard" & vbNullChar
        Shell_NotifyIcon NIM_ADD, nid
    End Sub

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Minimize to Sys Tray

    Is the Form scale mode in Pixels or Twips?
    Most people write the MouseMove part like this,
    Code:
       If Me.ScaleMode = vbPixels Then    
            Msg = x
        Else
            Msg = x / Screen.TwipsPerPixelX
        End If
    I usually add a picture box set to pixel scalemode and just use the X variable, set the tray icon code to point to the pic Hwnd and use the pic box mouse move event, it just works better, especially on borderless forms for some reason.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    294

    Re: Minimize to Sys Tray

    Worked like a charm!!! Thanx!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width