Page 1 of 1

Know when popup menu is open?

Posted: Mon Feb 02, 2015 9:01 am
by PB Fanatic
Is there a way I can tell if a PopupMenu() is currently open (being displayed)? My program closes its main window when Escape is pressed, but if a PopupMenu() is open, then Escape closes it AND my program in one go. So I just want Escape to close the menu only if it's open, and not the main window.

EDIT: NVM, I set a flag to 1 when I open the menu, so if Esc is hit and the flag is 1, I don't close the main window, and set the flag to 0 again.

But my question still stands: is there a way (API perhaps?) to tell if a menu is currently open, so I don't need to use a flag?

Re: Know when popup menu is open?

Posted: Mon Feb 02, 2015 11:59 am
by Shield
Your problem seems to be your event logic, because popup menus will be closed first by default
if escape is being hit. You should handle the escape key on the main window via shortcuts.

This works as expected:

Code: Select all

OpenWindow(0, 0, 0, 800, 500, "", #PB_Window_ScreenCentered)
AddKeyboardShortcut(0, #PB_Shortcut_Escape, 99)
CreatePopupMenu(0)
MenuTitle("foobar")
MenuItem(0, "hello world")

Repeat
	Select WaitWindowEvent() 
		Case #PB_Event_CloseWindow
			Break
		Case #PB_Event_RightClick
			DisplayPopupMenu(0, WindowID(0))
		Case #PB_Event_Menu
			If EventMenu() = 99
				Break
			EndIf
	EndSelect
ForEver
End

Re: Know when popup menu is open?

Posted: Tue Jun 06, 2017 2:12 pm
by Dude
In a callback, the #WM_UNINITMENUPOPUP message signals when any menu type (popup menu or systray menu) has been closed.

Re: Know when popup menu is open?

Posted: Tue Jun 06, 2017 2:18 pm
by RSBasic
Dude wrote:In a callback, the #WM_UNINITMENUPOPUP message signals when any menu type (popup menu or systray menu) has been closed.
@PB Fanatic
If you need a example:
Menu: http://www.rsbasic.de/aktualisierung/wi ... 20wurde.pb
Popup menu: http://www.rsbasic.de/aktualisierung/wi ... 20wurde.pb
(Only Windows)

Re: Know when popup menu is open?

Posted: Sat Jun 10, 2017 10:40 am
by Dude
Just noticed that #WM_UNINITMENUPOPUP triggers for ANY menu, such as a SUBMENU. So if you want to know when only the ROOT menu closes, you need to set a flag to keep track, like the following example. ;)

Code: Select all

; Only show when the ROOT menu closes, instead of the SUBMENU.

EnableExplicit

Global rootmenuclosed

Procedure WinCallback(hWnd, uMsg, wParam, lParam)
  Select uMsg
    Case #WM_INITMENUPOPUP
      If rootmenuclosed=0
        rootmenuclosed=wParam
      EndIf
    Case #WM_UNINITMENUPOPUP
      If wParam=rootmenuclosed
        rootmenuclosed=0
        Debug "MenĂ¼ wurde geschlossen."
      EndIf
  EndSelect

  ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

If OpenWindow(0, 0, 0, 500, 400, "Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  If CreateMenu(1, WindowID(0))
    MenuTitle("File")
    MenuItem(1, "New")
    MenuItem(2, "Open")
    MenuItem(3, "Save")
    OpenSubMenu("More")
    MenuItem(4, "Close")
    CloseSubMenu()
  EndIf

  SetWindowCallback(@WinCallback())

  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        End
    EndSelect
  ForEver
EndIf