Solved

Make pop up menu close with executable

Posted on 2006-06-10
6
249 Views
Last Modified: 2012-05-05
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
Comment
Question by:ahammar
  • 3
  • 2
6 Comments
 
LVL 33

Expert Comment

by:hongjun
Comment Utility
Post more code..
It should work.
0
 
LVL 23

Author Comment

by:ahammar
Comment Utility
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
 
LVL 33

Expert Comment

by:hongjun
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 23

Author Comment

by:ahammar
Comment Utility
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
 
LVL 26

Accepted Solution

by:
EDDYKT earned 500 total points
Comment Utility
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
 
LVL 23

Author Comment

by:ahammar
Comment Utility
Thanks EDDYKT,

That sure fixed it.  It works fine now.

Thanks again!

Cheers!
ahammar
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now