Results 1 to 26 of 26

Thread: [RESOLVED] Form will not restore from system tray

  1. #1

    Thread Starter
    Frenzied Member Skitchen8's Avatar
    Join Date
    Feb 2001
    Location
    Binghamotn, NY
    Posts
    1,943

    Resolved [RESOLVED] Form will not restore from system tray

    I'm having an issue using the microsoft KB article http://support.microsoft.com/kb/176085

    I have copied the code exactly, into the exact parts of the form it asks me to. It creates the icon just fine in my system tray, but none of the click events work at all. I'm just not understanding what I'm doing wrong. The tooltip comes up when I hover the mouse over the icon, so I know it's recognizing the mouse interacting with the icon, but I cannot for the life of me figure out why the click messages aren't coming through.

    Any ideas? This is the last part of this project before I do a beta release, and I've been having this problem the whole time. I keep going back to it and trying to fix it, deleting all the code, re-inserting it into the project and I still haven't managed to find a solution.

    Thank you,
    Eric
    Government is another way to say better…than…you.
    It’s like ice but no pick, a murder charge that won’t stick,
    it’s like a whole other world where you can smell the food,
    but you can’t touch the silverware.
    Huh, what luck. Fascism you can vote for.
    Humph, isn’t that sweet?
    And we’re all gonna die some day, because that’s the American way
    -Stone Sour

  2. #2

  3. #3

    Thread Starter
    Frenzied Member Skitchen8's Avatar
    Join Date
    Feb 2001
    Location
    Binghamotn, NY
    Posts
    1,943

    Re: Form will not restore from system tray

    as in right click in Files window>Add>Module?

    I'm fairly certain everything in the module is working, as the icon is place correctly and the tooltip comes up when the mouse is hovered.

    I've tried practically every combination of

    Code:
    Result = SetForegroundWindow(Me.hwnd)
    Me.WindowState = 0
    Me.Show ' show form
    Me.Visible = True
    Me.Refresh
    Me.SetFocus
    with no results whatsoever, and I don't understand how it's still hiding.
    Government is another way to say better…than…you.
    It’s like ice but no pick, a murder charge that won’t stick,
    it’s like a whole other world where you can smell the food,
    but you can’t touch the silverware.
    Huh, what luck. Fascism you can vote for.
    Humph, isn’t that sweet?
    And we’re all gonna die some day, because that’s the American way
    -Stone Sour

  4. #4

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Form will not restore from system tray

    You also may want to look at this project in the CodeBank section
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Form will not restore from system tray

    Another one here for that matter.

  7. #7

    Thread Starter
    Frenzied Member Skitchen8's Avatar
    Join Date
    Feb 2001
    Location
    Binghamotn, NY
    Posts
    1,943

    Re: Form will not restore from system tray

    Works perfectly, and I did the same thing and started a new project, pasted the code, and it also works perfectly. That's why I'm so confused. I've used that exact code in plenty of projects before, and never had any issues.

    It's just not registering the clicks for this program, and I don't understand why, is there something I could have changed that would cause this?

    I also erased all of the old trayicon code, and replaced it with the class from the thread LaVolpe linked, and still nothing. It works in the demo project, but will not recognize button clicks in my project.
    Government is another way to say better…than…you.
    It’s like ice but no pick, a murder charge that won’t stick,
    it’s like a whole other world where you can smell the food,
    but you can’t touch the silverware.
    Huh, what luck. Fascism you can vote for.
    Humph, isn’t that sweet?
    And we’re all gonna die some day, because that’s the American way
    -Stone Sour

  8. #8

    Thread Starter
    Frenzied Member Skitchen8's Avatar
    Join Date
    Feb 2001
    Location
    Binghamotn, NY
    Posts
    1,943

    Re: Form will not restore from system tray

    dilettante: I'm afraid that code is a bit above me, I'm not comfortable working with custom controls, and I would much rather go without any custom controls if possible.

    I'm just not getting this.
    Government is another way to say better…than…you.
    It’s like ice but no pick, a murder charge that won’t stick,
    it’s like a whole other world where you can smell the food,
    but you can’t touch the silverware.
    Huh, what luck. Fascism you can vote for.
    Humph, isn’t that sweet?
    And we’re all gonna die some day, because that’s the American way
    -Stone Sour

  9. #9
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Form will not restore from system tray

    What value did you provide for nid.hWnd? Whatever that was (a form, a picturebox), what is the scalemode of that object? Last but not least, can you post your MouseMove event
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  10. #10

    Thread Starter
    Frenzied Member Skitchen8's Avatar
    Join Date
    Feb 2001
    Location
    Binghamotn, NY
    Posts
    1,943

    Re: Form will not restore from system tray

    well now that I'm using the class, nid (or in this case mtypIcon).hWnd is set to form1.hwnd (well, it's set in the class to pfrm.hwnd, which is Form1 when SysTray.Init is called and I have tried using both Form1 and Me)

    The scalemode of the form is set to "1 - Twip"

    and I have tried both:
    Code:
        Call SysTray.MouseMove(Button, X, Me)
    and
    Code:
    Dim msg As Single
    
    msg = ScaleX(X, ScaleMode, vbPixels)
    Call SysTray.MouseMove(Button, msg, Me)
    The second being something I tried after seeing a suggestion somewhere else in the forum (I believe on the first link provided)

    And also, in case its important, I'm running XP SP2

    Edit: now also tried with
    Code:
    msg = x / Screen.TwipsPerPixelX
    and still no success
    Last edited by Skitchen8; Jun 4th, 2011 at 02:31 PM.
    Government is another way to say better…than…you.
    It’s like ice but no pick, a murder charge that won’t stick,
    it’s like a whole other world where you can smell the food,
    but you can’t touch the silverware.
    Huh, what luck. Fascism you can vote for.
    Humph, isn’t that sweet?
    And we’re all gonna die some day, because that’s the American way
    -Stone Sour

  11. #11
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Form will not restore from system tray

    Your SysTray.MouseMove may be part of the problem. You are passing pixel values, but if the class is the converting that value by dividing by TwipsPerPixel you will not get what you expect. Guess it would be helpful to see how your class uses the parameters passed as Button, msg, & Me
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  12. #12

    Thread Starter
    Frenzied Member Skitchen8's Avatar
    Join Date
    Feb 2001
    Location
    Binghamotn, NY
    Posts
    1,943

    Re: Form will not restore from system tray

    the class (copied directly from the second codebank post into my project, only changing the form name where necessary)

    I'm guessing what you need is
    Code:
    Public Sub MouseMove(Button As Integer, ByVal X As Long, pfrm As Form)
       Select Case X
            Case WM_LBUTTONDBLCLK: RaiseEvent DoubleClick
            Case WM_LBUTTONUP: RaiseEvent LeftClick
            Case WM_RBUTTONUP: RaiseEvent RightClick
            Case NIN_BALLOONHIDE: RaiseEvent BalloonHide
            Case NIN_BALLOONTIMEOUT: RaiseEvent BalloonTimeOut
            Case NIN_BALLOONUSERCLICK: RaiseEvent BalloonClicked
       End Select
    End Sub
    this is the rest of the code in the class in case that wasn't enough

    Code:
    ' Written by Ellis Dee of vbforums.com
    '
    ' Thanks to...
    '   Edgemeal   http://www.vbforums.com/showthread.php?t=595193
    '   Mike D. Sutton   http://www.mvps.org/EDais/
    '   Steve McMahon   http://www.vbaccelerator.com/home/VB/Code/Libraries/Shell_Projects/SysTray_-_The_Easy_Way/article.asp
    '   Ben Baird for the original code
    Option Explicit
    
    ' Tray Icon
    Private Const NIF_ICON = &H2
    Private Const NIF_MESSAGE = &H1
    Private Const NIF_TIP = &H4
    Private Const NIF_STATE = &H8
    Private Const NIF_INFO = &H10
    
    Private Const NIM_ADD = &H0
    Private Const NIM_MODIFY = &H1
    Private Const NIM_DELETE = &H2
    Private Const NIM_SETFOCUS = &H3
    Private Const NIM_SETVERSION = &H4
    
    Private Const NOTIFYICON_VERSION = 3
    
    Private Type NOTIFYICONDATA
        cbSize As Long
        hwnd As Long
        uID As Long
        uFlags As Long
        uCallbackMessage As Long
        hIcon As Long
        szTip As String * 128
        dwState As Long
        dwStateMask As Long
        szInfo As String * 256
        uTimeoutOrVersion As Long
        szInfoTitle As String * 64
        dwInfoFlags As Long
    End Type
    Private Declare Function DestroyIcon Lib "user32" (ByVal hIcon As Long) As Long
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
    
    
    Private Declare Function BitBlt Lib "gdi32" (ByVal hdcDest As Long, ByVal nXDest As Long, ByVal nYDest As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hdcSrc As Long, ByVal nXSrc As Long, ByVal nYSrc As Long, ByVal dwRop As Long) As Long
    Private Declare Function CreateBitmap Lib "gdi32" (ByVal nWidth As Long, ByVal nHeight As Long, ByVal nPlanes As Long, ByVal nBitCount As Long, ByRef lpBits As Any) As Long
    Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDC As Long) As Long
    Private Declare Function CreateIconIndirect Lib "user32" (ByRef pIconInfo As IconInfo) As Long
    Private Declare Function DeleteDC Lib "gdi32" (ByVal hDC As Long) As Long
    Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, ByRef lpObject As Any) As Long
    Private Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long
    Private Declare Function SetBkColor Lib "gdi32" (ByVal hDC As Long, ByVal crColor As Long) As Long
    Private Declare Function SetTextColor Lib "gdi32" (ByVal hDC As Long, ByVal crColor As Long) As Long
    
    ' Tray Events (fire in Form1.frm)
    Public Event LeftClick()
    Public Event RightClick()
    Public Event DoubleClick()
    Public Event BalloonHide()
    Public Event BalloonTimeOut()
    Public Event BalloonClicked()
    Private Const WM_MOUSEMOVE = &H200
    Private Const WM_LBUTTONDBLCLK = &H203
    Private Const WM_LBUTTONDOWN = &H201
    Private Const WM_LBUTTONUP = &H202
    Private Const WM_RBUTTONDBLCLK = &H206
    Private Const WM_RBUTTONDOWN = &H204
    Private Const WM_RBUTTONUP = &H205
    Private Const WM_USER = &H400
    Private Const NIN_SELECT = WM_USER
    Private Const NINF_KEY = &H1
    Private Const NIN_KEYSELECT = (NIN_SELECT Or NINF_KEY)
    Private Const NIN_BALLOONSHOW = (WM_USER + 2)
    Private Const NIN_BALLOONHIDE = (WM_USER + 3)
    Private Const NIN_BALLOONTIMEOUT = (WM_USER + 4)
    Private Const NIN_BALLOONUSERCLICK = (WM_USER + 5)
    
    ' Ballon tips
    Public Enum BalloonIconEnum
       beNone = 0
       beInformation = 1
       beWarning = 2
       beError = 3
       beNoSound = &H10
    End Enum
    
    Private mstrTooltip As String
    
    Private mtypIcon As NOTIFYICONDATA
    
    
    ' METHODS
    
    
    ' Set tray icon to contents of 32x32 picturebox
    Public Sub DrawIcon(ppic As PictureBox, Optional plngTransparentColor As Long = -1)
        ppic.AutoRedraw = True
        ppic.Picture = ppic.Image
        If plngTransparentColor < 0 Then
            mtypIcon.hIcon = BitmapToIcon(ppic.Picture.Handle)
        Else
            mtypIcon.hIcon = BitmapToIconTransparent(ppic.Picture.Handle, plngTransparentColor)
        End If
        If mtypIcon.hIcon Then
            RefreshIcon
            DestroyIcon mtypIcon.hIcon
        End If
    End Sub
    
    Public Function Init(pfrm As Form, pstrTooltip As String) As Boolean
        Const NIF_MESSAGE = &H1
        Const WM_MOUSEMOVE = &H200
        Const NIM_ADD = &H0
        
        With mtypIcon
            .cbSize = Len(mtypIcon)
            .hwnd = pfrm.hwnd
            .hIcon = pfrm.Icon
            .uID = vbNull
            .uFlags = NIF_MESSAGE
            .uCallbackMessage = WM_MOUSEMOVE
        End With
        Shell_NotifyIcon NIM_ADD, mtypIcon
        SetIcon pfrm
        Me.TooltipText = pstrTooltip
    End Function
    
    Public Sub MouseMove(Button As Integer, ByVal X As Long, pfrm As Form)
       Select Case X
            Case WM_LBUTTONDBLCLK: RaiseEvent DoubleClick
            Case WM_LBUTTONUP: RaiseEvent LeftClick
            Case WM_RBUTTONUP: RaiseEvent RightClick
            Case NIN_BALLOONHIDE: RaiseEvent BalloonHide
            Case NIN_BALLOONTIMEOUT: RaiseEvent BalloonTimeOut
            Case NIN_BALLOONUSERCLICK: RaiseEvent BalloonClicked
       End Select
    End Sub
    
    ' Set tray icon to a form icon
    Public Sub SetIcon(pfrm As Form)
        mtypIcon.hIcon = pfrm.Icon
        RefreshIcon
    End Sub
    
    Public Sub ShowBalloonTip(ByVal pstrMessage As String, Optional ByVal penIcon As BalloonIconEnum, Optional ByVal pstrTitle As String, Optional ByVal plngTimeout = 30000)
        mtypIcon.szInfo = pstrMessage & vbNullChar
        mtypIcon.szInfoTitle = pstrTitle & vbNullChar
        mtypIcon.uTimeoutOrVersion = plngTimeout
        mtypIcon.dwInfoFlags = penIcon
        mtypIcon.uFlags = NIF_INFO
        Shell_NotifyIcon NIM_MODIFY, mtypIcon
    End Sub
    
    
    ' PROPERTIES
    
    
    Public Property Get TooltipText() As String
        TooltipText = mstrTooltip
    End Property
    
    Public Property Let TooltipText(ByVal pstrTooltip As String)
        Const NIF_TIP = &H4
        Const NIM_MODIFY = &H1
        
        mstrTooltip = pstrTooltip
        mtypIcon.szTip = mstrTooltip & vbNullChar
        mtypIcon.uFlags = NIF_TIP
        Shell_NotifyIcon NIM_MODIFY, mtypIcon
    End Property
    
    
    ' INTERNAL ROUTINES
    
    
    Private Sub Class_Terminate()
        Const NIM_DELETE = &H2
        
        Shell_NotifyIcon NIM_DELETE, mtypIcon
        Set Form1 = Nothing
    End Sub
    
    Private Sub RefreshIcon()
        Const NIF_ICON = &H2
        Const NIM_MODIFY = &H1
        
        mtypIcon.uFlags = NIF_ICON
        Shell_NotifyIcon NIM_MODIFY, mtypIcon
    End Sub
    
    
    
    Private Function GetColMask(ByVal inDC As Long, ByVal inMaskCol As Long) As Long
        Dim MaskDC As Long, MaskBMP As Long, OldMask As Long, OldBack As Long
        ' Create a new DC
        MaskDC = CreateCompatibleDC(inDC)
        If (MaskDC) Then ' Create a new 1-bpp Bitmap (DDB)
            MaskBMP = CreateBitmap(32, 32, 1, 1, ByVal 0&)
            If (MaskBMP) Then ' Select Bitmap into DC
                OldMask = SelectObject(MaskDC, MaskBMP)
                If (OldMask) Then ' Set mask colour
                    OldBack = SetBkColor(inDC, inMaskCol)
                    ' Generate mask image
                    If (BitBlt(MaskDC, 0, 0, 32, 32, inDC, 0, 0, vbSrcCopy) <> 0) Then GetColMask = MaskBMP
                    ' Clean up
                    Call SetBkColor(inDC, OldBack)
                    Call SelectObject(MaskDC, OldMask)
                End If
                ' Something went wrong, destroy mask Bitmap
                If (GetColMask = 0) Then Call DeleteObject(MaskBMP)
            End If
            ' Destroy temporary DC
            Call DeleteDC(MaskDC)
        End If
    End Function
    Just as a note I took out a couple sections related to turning a BMP into an icon to stay under the character limit for a forum post
    Government is another way to say better…than…you.
    It’s like ice but no pick, a murder charge that won’t stick,
    it’s like a whole other world where you can smell the food,
    but you can’t touch the silverware.
    Huh, what luck. Fascism you can vote for.
    Humph, isn’t that sweet?
    And we’re all gonna die some day, because that’s the American way
    -Stone Sour

  13. #13
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Form will not restore from system tray

    Converting scalemode to pixels (i.e., ScaleX(X, ScaleMode, vbPixels)) appears to be correct based on the Class you are using. Are you responding to the class' events? Have you declared your class using the keyword "WithEvents"?

    P.S. I'd say that class is probably not the best example about for various reasons. But if it does what you need then so be it.

    Edited: I am now a bit sorry I even referenced it as something to consider
    Last edited by LaVolpe; Jun 4th, 2011 at 03:11 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  14. #14

    Thread Starter
    Frenzied Member Skitchen8's Avatar
    Join Date
    Feb 2001
    Location
    Binghamotn, NY
    Posts
    1,943

    Re: Form will not restore from system tray

    the very first line in my project is
    Code:
    Public WithEvents SysTray As clsSysTray
    which is what it is supposed to be, correct?

    and the class event I'm responding to is as follows
    Code:
    Private Sub SysTray_LeftClick()
    
    Me.WindowState = 0
    Me.Show
    Me.Visible = True
    
    End Sub
    Which, from looking at the example project, seems to be the way I should do it.
    Government is another way to say better…than…you.
    It’s like ice but no pick, a murder charge that won’t stick,
    it’s like a whole other world where you can smell the food,
    but you can’t touch the silverware.
    Huh, what luck. Fascism you can vote for.
    Humph, isn’t that sweet?
    And we’re all gonna die some day, because that’s the American way
    -Stone Sour

  15. #15
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Form will not restore from system tray

    Quote Originally Posted by Skitchen8 View Post
    dilettante: I'm afraid that code is a bit above me, I'm not comfortable working with custom controls, and I would much rather go without any custom controls if possible.

    I'm just not getting this.
    Don't try to read the code, it doesn't require modifications.

    Just add the CTL and BAS modules to a project, then drop an instance of the control onto your Form, create a hidden menu for you to pop up and so on. Then write code for the events and such in your Form.

    Frankly, that other code is a terrible hack.

  16. #16
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Form will not restore from system tray

    Quote Originally Posted by Skitchen8 View Post
    Which, from looking at the example project, seems to be the way I should do it.
    Yes. Though you should use SetForegroundWindow to ensure your window is displayed forefront; else it can be hidden behind other windows.

    I don't immediately see the problem. I am assuming you are calling the class' MouseMove method from your Form_MouseMove event?

    Place a breakpoint on Me.WindowState = 0 and see if your code is actually being triggered. If not triggered, then place a Debug.Print statement in your class' MouseMove method:
    Code:
     Public Sub MouseMove(Button As Integer, ByVal X As Long, pfrm As Form)
       Select Case X
            Case WM_LBUTTONDBLCLK: RaiseEvent DoubleClick
            Case WM_LBUTTONUP: RaiseEvent LeftClick
            Case WM_RBUTTONUP: RaiseEvent RightClick
            Case NIN_BALLOONHIDE: RaiseEvent BalloonHide
            Case NIN_BALLOONTIMEOUT: RaiseEvent BalloonTimeOut
            Case NIN_BALLOONUSERCLICK: RaiseEvent BalloonClicked
    Case Else: Debug.Print "Value of X is "; X; Timer()
    ' you'll probably get a lot of these when moving the mouse over the icon, 
    ' but you'll want to see what value is being printed when you click
    ' Open your immediate window (Ctrl+G) to monitor these print statements
       End Select
    End Sub
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  17. #17

    Thread Starter
    Frenzied Member Skitchen8's Avatar
    Join Date
    Feb 2001
    Location
    Binghamotn, NY
    Posts
    1,943

    Re: Form will not restore from system tray

    Quote Originally Posted by LaVolpe View Post
    Yes. Though you should use SetForegroundWindow to ensure your window is displayed forefront; else it can be hidden behind other windows.

    I don't immediately see the problem. I am assuming you are calling the class' MouseMove method from your Form_MouseMove event?

    Place a breakpoint on Me.WindowState = 0 and see if your code is actually being triggered. If not triggered, then place a Debug.Print statement in your class' MouseMove method:
    Code:
     Public Sub MouseMove(Button As Integer, ByVal X As Long, pfrm As Form)
       Select Case X
            Case WM_LBUTTONDBLCLK: RaiseEvent DoubleClick
            Case WM_LBUTTONUP: RaiseEvent LeftClick
            Case WM_RBUTTONUP: RaiseEvent RightClick
            Case NIN_BALLOONHIDE: RaiseEvent BalloonHide
            Case NIN_BALLOONTIMEOUT: RaiseEvent BalloonTimeOut
            Case NIN_BALLOONUSERCLICK: RaiseEvent BalloonClicked
    Case Else: Debug.Print "Value of X is "; X; Timer()
    ' you'll probably get a lot of these when moving the mouse over the icon, 
    ' but you'll want to see what value is being printed when you click
    ' Open your immediate window (Ctrl+G) to monitor these print statements
       End Select
    End Sub
    It did not break, so I tried the debug.print statement and the last one I got was: Value of X is 676 59192.34

    the numbers don't seem to be changing as I move the mouse across the icon though, should they?

    If it would be easier for you guys I can switch back to the way I was doing it before. I really appreciate all the help here.
    Government is another way to say better…than…you.
    It’s like ice but no pick, a murder charge that won’t stick,
    it’s like a whole other world where you can smell the food,
    but you can’t touch the silverware.
    Huh, what luck. Fascism you can vote for.
    Humph, isn’t that sweet?
    And we’re all gonna die some day, because that’s the American way
    -Stone Sour

  18. #18

    Thread Starter
    Frenzied Member Skitchen8's Avatar
    Join Date
    Feb 2001
    Location
    Binghamotn, NY
    Posts
    1,943

    Re: Form will not restore from system tray

    Okay, now I'm back to the original code from: http://support.microsoft.com/kb/176085. This code is smaller, and easier for me to understand.

    Now I have
    Code:
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    'this procedure receives the callbacks from the System Tray icon.
          Dim Result As Long
          Dim msg As Long
           'the value of X will vary depending upon the scalemode setting
           If Me.ScaleMode = vbPixels Then
            msg = X
           Else
            msg = X / Screen.TwipsPerPixelX
           End If
           Select Case msg
            Case WM_LBUTTONUP        '514 restore form window
             Me.WindowState = vbNormal
             Result = SetForegroundWindow(Me.hwnd)
             Me.Show
            Case WM_LBUTTONDBLCLK    '515 restore form window
             Me.WindowState = vbNormal
             Result = SetForegroundWindow(Me.hwnd)
             Me.Show
            Case WM_RBUTTONUP        '517 display popup menu
             Result = SetForegroundWindow(Me.hwnd)
             Me.PopupMenu Me.mPopupSys
            Case Else: Debug.Print "Value of X is "; X; Timer()
           End Select
    End Sub
    I put the break-point in again and now the code is breaking at
    Code:
    Case WM_LBUTTONUP        '514 restore form window
    However it's doing it as I run the mouse across the form with no button press. I am thoroughly confused now.
    Government is another way to say better…than…you.
    It’s like ice but no pick, a murder charge that won’t stick,
    it’s like a whole other world where you can smell the food,
    but you can’t touch the silverware.
    Huh, what luck. Fascism you can vote for.
    Humph, isn’t that sweet?
    And we’re all gonna die some day, because that’s the American way
    -Stone Sour

  19. #19
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Form will not restore from system tray

    What you are experiencing is one reason why I don't like using that non-subclassing method. Though it is easier, it also can trigger your code when the form is displayed because your form will get mousemove events when it is displayed also. This occurs when your form can be more than 500 pixels in width.

    An easy workaround is to set a form-level boolean variable to True when you send your form to the system tray and False when you restore your form. In your mousemove event, only execute that code if that boolean is True. Understand?

    Edited: another workaround is simply to use a hidden picturebox. Set your nid.Hwnd to the picturebox hWnd & move your code to the Picturebox's MouseMove event, changing 'Me.ScaleMode" to read: Picture1.ScaleMode (change Picture1 name as necessary). Hidden controls will never get mousemove events except through the systray, in this case.
    Last edited by LaVolpe; Jun 4th, 2011 at 04:20 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  20. #20

    Thread Starter
    Frenzied Member Skitchen8's Avatar
    Join Date
    Feb 2001
    Location
    Binghamotn, NY
    Posts
    1,943

    Re: Form will not restore from system tray

    I think I understand, I set public isMin as boolean at top of code, set isMin = false on form_load, and then:
    Code:
    Private Sub Form_Resize()
           'this is necessary to assure that the minimized window is hidden
           If Me.WindowState = vbMinimized Then
           isMin = True
           Me.Hide
           End If
    End Sub
    and

    Code:
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    'this procedure receives the callbacks from the System Tray icon.
          Dim Result As Long
          Dim msg As Long
           'the value of X will vary depending upon the scalemode setting
           If Me.ScaleMode = vbPixels Then
            msg = X
           Else
            msg = X / Screen.TwipsPerPixelX
           End If
           Select Case msg
            Case WM_LBUTTONUP        '514 restore form window
             Me.WindowState = vbNormal
             Result = SetForegroundWindow(Me.hwnd)
             Me.Show
            Case WM_LBUTTONDBLCLK    '515 restore form window
             Me.WindowState = vbNormal
             Result = SetForegroundWindow(Me.hwnd)
             Me.Show
            Case WM_RBUTTONUP        '517 display popup menu
             Result = SetForegroundWindow(Me.hwnd)
             Me.PopupMenu Me.mPopupSys
            Case Else
            If isMin = True Then Debug.Print "Value of X is "; X; Timer()
           End Select
    End Sub
    That's pretty much what you were saying right?

    When doing this I get nothing printed to the immediate window, so it seems to me like it's not reading mouse movement at all once the window is minimized.
    Government is another way to say better…than…you.
    It’s like ice but no pick, a murder charge that won’t stick,
    it’s like a whole other world where you can smell the food,
    but you can’t touch the silverware.
    Huh, what luck. Fascism you can vote for.
    Humph, isn’t that sweet?
    And we’re all gonna die some day, because that’s the American way
    -Stone Sour

  21. #21
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Form will not restore from system tray

    Yes & no. Not quite. Also see my edited notes in previous reply.

    Where I was going was:
    - In your MouseMove event, the very first line: If isMin = False Then Exit Sub
    - In your Case WM_LBUTTONUP, WM_LBUTTONDBLCLK, WM_RBUTTONUP statements: isMin = False
    :: Anywhere where you restore your form, ensure isMin = False
    - In your Form_Resize event, ok.
    - isMin does not need to be public
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  22. #22
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Form will not restore from system tray

    I still get the feeling that the problem is the use of WM_MOUSEMOVE here. For a long time people have been suggesting avoiding that and using an app-defined message instead (which requires subclassing of course).

    Looking at MSDN again I'm starting to wonder if perhaps there was never a problem using WM_MOUSEMOVE.


    Going back to that old, old MS KB code sample I see one thing.

    Where they fiddle with the X argument for Form_MouseMove they assume only two possible ScaleMode settings: either Pixels or else they punt and assume Twips.

    Are you positive you have the ScaleMode = vbTwips?
    Last edited by dilettante; Jun 4th, 2011 at 04:34 PM.

  23. #23
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Form will not restore from system tray

    Well that method should work fine for a majority of the cases. Where it can fail is....

    1. Form width is > 512 pixels & form is visible. WM_LButtonDown for example has value of 513. In this case, one can get their systray code executed unexpectedly.

    2. Vista & above. There are additional messages that are lost using this method. For more details, one can review my reply (4th post) on this thread: http://www.vbforums.com/showthread.php?t=595990

    In short, though it is pretty reliable for XP & below. I would not continue using that "easy method" for Vista and above if one wants those extra messages.
    Last edited by LaVolpe; Jun 4th, 2011 at 04:34 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  24. #24

    Thread Starter
    Frenzied Member Skitchen8's Avatar
    Join Date
    Feb 2001
    Location
    Binghamotn, NY
    Posts
    1,943

    Re: Form will not restore from system tray

    Thank you all so much for your help, simply adding the code to the picturebox's mousemove, and changing the hwnd in the call for the tray icon seems to have completely fixed the issue.

    For future versions of the program I would like to look in to subclassing, and if I can learn enough to make it work I'd love to implement it. The issue I had with using a custom control is that if I can avoid it I don't want to use code that I don't/can't understand. While there are parts of this code I still don't quite understand I actually now see where one of the issues was, with my form being a little over 700 pixels wide, and now I understand that it thinks that a value of 513 for x is me clicking the mouse, whereas the picturebox placed on my form is much smaller. With this code if I want to add small features later I understand it well enough that I can implement those.

    Again I can't thank you enough for all your help. One of the next few features I plan on implementing involves me trying to find a way to figure out the density of certain colors in a picture (I want to start saving radar images only when there is precipitation in the area) so I feel like I'll have more questions soon.
    Government is another way to say better…than…you.
    It’s like ice but no pick, a murder charge that won’t stick,
    it’s like a whole other world where you can smell the food,
    but you can’t touch the silverware.
    Huh, what luck. Fascism you can vote for.
    Humph, isn’t that sweet?
    And we’re all gonna die some day, because that’s the American way
    -Stone Sour

  25. #25
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: [RESOLVED] Form will not restore from system tray

    Grumble grumble, give 'em an OCX and they whine "dependency." Give them an inline UserControl and they whine "I can' unnerstan it." What do they do, code everything themseves from API calls or what?


  26. #26
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: [RESOLVED] Form will not restore from system tray

    Just a final comment:

    Updated my NotifyIcon control to correct its bug in safe subclassing. Now withstands IDE Stop with no crashes. Even cleans up the tray icon rather than orphaning it now. However a true crash will still leave the icon hanging.
    Last edited by dilettante; Jun 5th, 2011 at 07:13 AM.

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