Solved

Determine if right click menu is shown

Posted on 2004-04-13
13
233 Views
Last Modified: 2010-05-02
Hi,

I built an Auto logoff mechanism in my application.
If user left system after he right click on one of my controls, and right click menu has appeared,
the auto logoff is working incorrectly - when I unload the form, the function that operate the pop up menu is still active.

I want to cancel the auto logoff if pop up menu is shown.
Is there a way to determine if a pop up menu is shown ?
Of course, I can set some flag before I open menus, but I prefer a better way

Thanks
0
Comment
Question by:amnav
  • 5
  • 2
  • 2
  • +1
13 Comments
 
LVL 17

Expert Comment

by:zzzzzooc
ID: 10811759
PopupMenu() and TrackPopupMenu(Ex) (API) both wait for returns before processing anything else so the thread will be "blocked". If your "Auto logoff mechanism" is within it's own thread (a Timer control for example), it won't be effected by it. Your only likely option is just to use a flag-variable to check if any PopupMenus are open at the time and I doubt there are any more efficient methods (will take note to any if they're posted). A few boolean variables shouldn't harm much.

You could always just create your own windows (WS_POPUP) and try to simulate PopupMenus though. You can avoid the input-asynchronous behavior that way (at a costly price however).



FYI, interested in possible solutions for this question also now.
0
 
LVL 3

Expert Comment

by:redfordb
ID: 10812342
I think the simplest way to do this would be to add a global boolean variable ie blnPopupShown

Then when you display a popup menu do the following

blnPopupShown = True
popupmeeu  ....
blnPopupShown = False

Then in your timeout feature just add
If Not blnPopUpshow Then
 timeout code
End If

0
 
LVL 39

Expert Comment

by:appari
ID: 10812586
you can check for the visibilty of the popupmenu, in case the menu is invisible at design time. something like this




if not myPop.Visible then unload me

as long as the popup is displayed the visible property returns true. once the popup is closed its visible property returns false.
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 3

Expert Comment

by:redfordb
ID: 10812673
If the menus are only used as popup, then you can just check the visibility of the menu.
0
 
LVL 15

Expert Comment

by:ameba
ID: 10814608
Hi,
If you don't use SetCapture in your code, you can use GetCapture to determine if popup window captured mouse.
GetCapture will return 0 if there is no popup shown, and <> 0 if your popup, or textbox's built-in popup menu is shown.  (If you are using SetCapture in your code, you know that it is similar in behavior, i.e. normally code waits for another mouse action and it should be treated the same as popup.)

' Form1, add menu mnuPop (visible=false) and a submenu
'        also, add Textbox and Timer (interval=1000)
Option Explicit
Private Declare Function GetCapture Lib "user32" () As Long

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = vbRightButton Then Me.PopupMenu Me.mnuPop
End Sub

Private Sub Timer1_Timer()
    Beep
    Caption = GetCapture ' shows 0 or, if menu is shown, some value <>0
End Sub
0
 
LVL 15

Expert Comment

by:ameba
ID: 10814708
GetCapture will also return non-zero, if your mouse action is in progress, e.g. you are holding mouse down and moving it.  Hope it is OK behavior.
0
 
LVL 15

Accepted Solution

by:
ameba earned 125 total points
ID: 10814855
Instead of cancelling logoff, cancel user's popup :-)

Private Sub Timer1_Timer()
    Beep
    If GetCapture <> 0 Then
        On Error Resume Next
        SendKeys "{ESC}"
    End If
    ' your logoff proc.
End Sub
0
 
LVL 17

Assisted Solution

by:zzzzzooc
zzzzzooc earned 125 total points
ID: 10819088
Interesting GetCapture idea. :)

There's another method which will check to see if a Popup-Menu window exists within your application's thread. This will include right-clicking on the title-bar and on the app's bar in the taskbar.

Example...

Form1:
=============

Private Sub Form_Load()
    Timer1.Interval = 1000
    Timer1.Enabled = True
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Call Me.PopupMenu(mnuTest)
End Sub
Private Sub Timer1_Timer()
    If IsMenuOpen = True Then
        Me.Caption = "Menu=true"
    Else
        Me.Caption = "Menu=false"
    End If
End Sub



Module1:
==================

Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
Public Function IsMenuOpen() As Boolean
    IsMenuOpen = Not EnumWindows(AddressOf EnumWindowsProc, 0)
End Function
Public Function EnumWindowsProc(ByVal hWnd As Long, ByVal lParam As Long) As Boolean
    Dim sBuff As String * 256, iLen As Integer, sClass As String
    If GetWindowThreadProcessId(hWnd, 0) = App.ThreadID Then
        iLen = GetClassName(hWnd, sBuff, Len(sBuff))
        If iLen > 0 Then
            sClass = Left$(sBuff, iLen)
            If sClass = "#32768" Then
                EnumWindowsProc = False
                Exit Function
            End If
        End If
    End If
    EnumWindowsProc = True
End Function



0
 
LVL 15

Expert Comment

by:ameba
ID: 10828980
Cool!
0
 
LVL 15

Expert Comment

by:ameba
ID: 11408044
My recommendation:
    Split: ameba {http:#10814855} & zzzzzooc {http:#10819088}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using "ScreenUpdating" 6 66
How to incorporate an error traping routing to existing code in VB6 11 40
Add and format columns in vb6 7 59
Added a column screws up code 5 58
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

831 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