Know when popup menu is open?

Just starting out? Need help? Post your questions and find answers here.
PB Fanatic
User
User
Posts: 49
Joined: Wed Dec 17, 2014 11:54 am

Know when popup menu is open?

Post 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?
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Know when popup menu is open?

Post 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
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Know when popup menu is open?

Post by Dude »

In a callback, the #WM_UNINITMENUPOPUP message signals when any menu type (popup menu or systray menu) has been closed.
User avatar
RSBasic
Moderator
Moderator
Posts: 1228
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Know when popup menu is open?

Post 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)
Image
Image
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Know when popup menu is open?

Post 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
Post Reply