Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

Make pop up menu close with executable

I have an app that is minimized as an icon in the system tray written in VB6.  When I right click on the icon, it pops up a menu.

I use this code:
PopUpMenu mnuRun

The problem is when the menu pops up, if I don't click on anything and go back to something else, the menu does not close.

I put this in the forms Lost Focus event:
Close mnuRun

That works fine when I run it from vb.  The menu closes if I click somewhere else.  But when I make it into an executable, then it stops working.  The menu does not close.  I don't understand why.

Anybody have a suggestion?

Thanks,
Cheers!
ahammar
0
ahammar
Asked:
ahammar
  • 3
  • 2
1 Solution
 
hongjunCommented:
Post more code..
It should work.
0
 
ahammarAuthor Commented:
Ok, this is kinda long but here it goes.

This is the code in my module

       'user defined type required by Shell_NotifyIcon API call
             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 required by Shell_NotifyIcon API call:
             Public Const NIM_ADD = &H0
             Public Const NIM_MODIFY = &H1
             Public Const NIM_DELETE = &H2
             Public Const NIF_MESSAGE = &H1
             Public Const NIF_ICON = &H2
             Public Const NIF_TIP = &H4
             Public Const WM_MOUSEMOVE = &H200

       Public Const WM_LBUTTONDOWN = &H201 'Button down Public Const WM_LBUTTONUP = &H202 'Button up
       Public Const WM_LBUTTONUP = &H202
       Public Const WM_LBUTTONDBLCLK = &H203 'Double-click
       Public Const WM_RBUTTONDOWN = &H204 'Button down
       Public Const WM_RBUTTONUP = &H205 'Button up
       Public Const WM_RBUTTONDBLCLK = &H206  'Double-click
       Public Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" _
       (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
       Public nid As NOTIFYICONDATA


 This is in my form's load event

       With nid
            .cbSize = Len(nid)
            .hwnd = Me.hwnd
            .uId = vbNull
            .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
            .uCallBackMessage = WM_MOUSEMOVE
            .hIcon = Me.Icon
            .szTip = "Explorer Paths" & vbNullChar
       End With
       Shell_NotifyIcon NIM_ADD, nid

And this is in my form's Mouse Move event

msg = x / Screen.TwipsPerPixelX
If Me.ScaleMode = vbPixels Then msg = x

       Select Case msg
            Case WM_LBUTTONUP '514 restore form window
                     If StopRun = True Then
                        StopRun = False
                        Exit Sub
                    End If
               Me.Hide
                Me.WindowState = vbMinimized
                mnuLayer1(4).Visible = True
                PopupMenu mnuPathS
                Me.SetFocus
             Case WM_LBUTTONDBLCLK '515 restore form window
                StopRun = True
                AppActivate MeCaption
                Me.WindowState = vbNormal
                Me.Show
                Me.SetFocus
                Me.Refresh
             Case WM_RBUTTONUP '517 display popup Menu
                Me.Hide
                Me.WindowState = vbMinimized
                mnuLayer1(4).Visible = True
                PopupMenu mnuRun
                Me.SetFocus
        End Select

This might be a little more than you expected, but maybe not.  
I did find out that I should not need any code to close the menu.  It is suppose to do that by itself when I click somewhere else withoug making a selection from the menu.  I have it that way in some of my other apps and it works fine.  So I'm thinking that somewhere within this app something is causing it not to work right.  The part I don't get is why it works fine when running from vb, but not after I make it an executable.
Anyhow, I'm still working on it.

Cheers!
ahammar
0
 
hongjunCommented:
Can't you not do something like this?

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  If Button = vbRightButton Then
    PopupMenu mnuRun
  End If
End Sub
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ahammarAuthor Commented:
Hi hongjun,

hey, your suggestion does work, but I'm still having the same problem.  It works if I click run from VB, but not after I make it into an executable.

Very weird...

Still working on it though.  Like I said before, I think there is a conflict with some other code somewhere.  It's kind of confusing as I wrote this app a long time ago and just now trying to fix it.  It may be that I will not be able to put enough information here to have this question answered, since I don't know yet exactly what is causing the problem.  All my other apps that I use this code in work fine.

Thanks for your efforts here.  I'll be back.

Cheers!
ahammar

0
 
EDDYKTCommented:
you need to call


SetForegroundWindow(Me.hwnd)


before calling popupmenu

i.e

private Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hwnd As Long) As Long

SetForegroundWindow Me.hwnd
PopupMenu mnuPathS
0
 
ahammarAuthor Commented:
Thanks EDDYKT,

That sure fixed it.  It works fine now.

Thanks again!

Cheers!
ahammar
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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