Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Make pop up menu close with executable

Posted on 2006-06-10
6
Medium Priority
?
259 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 33

Expert Comment

by:hongjun
ID: 16877178
Post more code..
It should work.
0
 
LVL 23

Author Comment

by:ahammar
ID: 16878169
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
ID: 16879285
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's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 23

Author Comment

by:ahammar
ID: 16881741
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 2000 total points
ID: 16884649
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
ID: 16891007
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

604 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