Dynamic menus  
Author Message
BrianMcCashin





PostPosted: Visual FoxPro General, Dynamic menus Top

Is it possible, in Visual FoxPro, to implement a dynamic Windows menu whereby I could add my forms to the menu when they open and remove them when they close

Visual FoxPro1  
 
 
Alex Feldstein





PostPosted: Visual FoxPro General, Dynamic menus Top

Yes, you can create, add and remove menu pads, bars, items programmatically.

See:

Menus and Menu Bars

http://msdn.microsoft.com/library/default.asp url=/library/en-us/dv_foxhelp9/html/273991b4-0bcb-426e-9052-ef7dda0507a4.asp frame=true

 

You could use a better menu generator: GenMenuX, a free tool to conditionally add/remove menu items :

http://fox.wikis.com/wc.dll Wiki~GenMenuX


 
 
Mark.OSVATH





PostPosted: Visual FoxPro General, Dynamic menus Top

Hi

I build the menu dynamically from a table.

The required tables will be created with some sample records. You could also filter the menu and submenu with the field access_right if you have some rights management in your program.

Regards,
Mark

IF NOT INDBC('menu_main','table')
 CREATE TABLE menu_main ( kod n(3), access_right n(3), menupad c(32), menu_name c(32), keyboard_shortcut c(8) DEFAULT '')
 APPEND BLANK
 replace kod WITH 1, access_right WITH 1, menupad WITH '_filemenu', menu_name WITH 'File', keyboard_shortcut WITH 'ALT+F'
 APPEND BLANK
 replace kod WITH 2, access_right WITH 1, menupad WITH '_editmenu', menu_name WITH 'Edit', keyboard_shortcut WITH 'ALT+E'
ENDIF

IF NOT INDBC('menu_sub','table')
 CREATE TABLE menu_sub ( kod n(3), fokod n(3), access_right n(3), menubar c(2), menu_name c(64), keyboard_shortcut c(8) DEFAULT '', menu_icon c(24) DEFAULT '', menu_command c(254))

 *** main menu: 1
 APPEND BLANK
 replace kod WITH 1, fokod WITH 1, access_right WITH 1, menubar WITH '1', menu_name WITH 'Open', keyboard_shortcut WITH '', menu_command WITH 'do programs\myopenproc'
 APPEND BLANK
 replace kod WITH 2, fokod WITH 1, access_right WITH 1, menubar WITH '2', menu_name WITH 'Exit', keyboard_shortcut WITH '', menu_command WITH 'quit'

 *** edit menu: 2
 APPEND BLANK
 replace kod WITH 3, fokod WITH 2, access_right WITH 1, menubar WITH '1', menu_name WITH 'Copy', keyboard_shortcut WITH 'CTRL+C', menu_command WITH 'do programs\mycopyproc', menu_icon WITH '_mfi_send'
 APPEND BLANK
 replace kod WITH 4, fokod WITH 2, access_right WITH 1, menubar WITH '2', menu_name WITH 'Paste', keyboard_shortcut WITH 'CTRL+V', menu_command WITH 'do programs\mypasteproc'
ENDIF

*** menu builder
CLOSE TABLES ALL
SET SYSMENU TO
SET SYSMENU AUTOMATIC

SELECT * from menu_main INTO CURSOR mainmenu ORDER BY kod
IF _tally=0
 IF USED('menu_main')
  SELECT menu_main
  USE
 ENDIF
 =MESSAGEBOX('The main menu is empty!',48,'')
 RETURN
ENDIF

SELECT menu_sub.*, menu_main.menupad, VAL(menu_sub.menubar) as x from menu_sub INNER JOIN menu_main ON menu_sub.fokod = menu_main.kod ORDER BY menu_sub.fokod,x INTO CURSOR submenu
IF _tally=0
 IF USED('menu_main')
  SELECT menu_main
  USE
 ENDIF
 IF USED('mainmenu')
  SELECT mainmenu
  USE
 ENDIF
 IF USED('menu_sub')
  SELECT menu_sub
  USE
 ENDIF
 =MESSAGEBOX('The sub menu is empty!',48,'')
 RETURN
ENDIF

SELECT mainmenu
GO TOP
SCAN
 IF EMPTY(keyboard_shortcut)
  s="DEFINE PAD "+ALLTRIM(menupad)+" OF _msysmenu PROMPT '"+ALLTRIM(menu_name)+"' COLOR SCHEME 3"
 ELSE
  s="DEFINE PAD "+ALLTRIM(menupad)+" OF _msysmenu PROMPT '"+ALLTRIM(menu_name)+"' COLOR SCHEME 3 KEY "+keyboard_shortcut+", ''"
 ENDIF
 &s
 s="ON PAD "+ALLTRIM(menupad)+" OF _MSYSMENU ACTIVATE POPUP p"+ALLTRIM(menupad)
 &s
 s="DEFINE POPUP p"+ALLTRIM(menupad)+" MARGIN RELATIVE SHADOW COLOR SCHEME 4"
 &s
ENDSCAN

SELECT submenu
GO TOP
SCAN
 IF EMPTY(keyboard_shortcut)
  s="DEFINE BAR "+ALLTRIM(menubar)+" OF p"+ALLTRIM(menupad)+" PROMPT '"+ALLTRIM(menu_name)+"'"
 ELSE
  s="DEFINE BAR "+ALLTRIM(menubar)+" OF p"+ALLTRIM(menupad)+" PROMPT '"+ALLTRIM(menu_name)+"' KEY "+ALLTRIM(keyboard_shortcut)+", '"+ALLTRIM(keyboard_shortcut)+"'"
 ENDIF
 IF NOT EMPTY(menu_icon)
  s=s+" PICTRES "+ALLTRIM(menu_icon)
 ENDIF
 &s
 s="ON SELECTION BAR "+ALLTRIM(menubar)+" OF p"+ALLTRIM(menupad)+" "+ALLTRIM(menu_command)
 &s
ENDSCAN

IF USED('menu_main')
 SELECT menu_main
 USE
ENDIF
IF USED('mainmenu')
 SELECT mainmenu
 USE
ENDIF
IF USED('menu_sub')
 SELECT menu_sub
 USE
ENDIF
IF USED('submenu')
 SELECT submenu
 USE
ENDIF