Page 1 of 1

Creazione menu a runtime

PostPosted: Wed May 21, 2014 4:38 pm
by Patrizio
Ciao a tutti,

ho riscontrato un problema costruendo un menu a runtime.

Piccolo esempio: ogni volta che cliccate su test il programma dovrebbe ricreare il menu. Se controllate con il gestione attività vedrete che il numero di GDI objects utilizzati incrementa ogni volta di 20 unità senza mai liberarsi. Togliendo il riferimento all'immagine invece rimane invariato.

Visto che uso una versione di fivewin vecchia, qualcuno può provare sull'ultima se il problema persiste?

Code: Select all  Expand view

#include "FiveWin.ch"
static oWnd
static oSubMenu
//----------------------------------------------------------------------------//

function Main()
   DEFINE WINDOW oWnd TITLE "Test" MENU BuildMenu()
   @ 2, 2 BUTTON "Test" SIZE 120, 25 OF oWnd ACTION TestMenu()
   ACTIVATE WINDOW oWnd
return nil

//----------------------------------------------------------------------------//

function BuildMenu()
   local oMenu, oItem, n
   MENU oMenu
      MENUITEM "Test"
      MENU oSubMenu
         FOR n := 1 TO 20
            MENUITEM oItem PROMPT Str(n) ACTION ( MsgInfo("Any action!") ) FILE "c:\fwh\bitmaps\16x16\bitmap.bmp" OF oSubMenu
         NEXT
      ENDMENU
   ENDMENU
return oMenu

//----------------------------------------------------------------------------//

function TestMenu()
   local oItem, n
   oSubMenu:DelItems()
   FOR n := 1 TO 20
      oSubMenu:Add(TMenuItem():New(Str(n),,,.T.,{||MsgInfo("Any action!")},"c:\fwh\bitmaps\16x16\bitmap.bmp",),.T.)
   NEXT
return

//----------------------------------------------------------------------------//
 

Re: Creazione menu a runtime

PostPosted: Wed May 21, 2014 4:44 pm
by Antonio Linares
Patrizio,

Please change Class TMenuItem Method Destroy this way:

Code: Select all  Expand view
METHOD Destroy() CLASS TMenuItem

   local nAt := AScan( ::oMenu:aItems,;
                       { | oItem | oItem:nId == ::nId } )
   local nResult                    

   if ValType( ::bAction ) == "O"
      ::bAction:End()
   endif

   if nAt != 0
      ADel( ::oMenu:aItems, nAt )
      ASize( ::oMenu:aItems, Len( ::oMenu:aItems ) - 1 )
   endif

   nResult = RemoveMenu( ::oMenu:hMenu, ::nId, MF_BYCOMMAND )
   
   if ! Empty( ::hBitmap )
      DeleteObject( ::hBitmap )
   endif
   
return nResult      
 

Re: Creazione menu a runtime

PostPosted: Wed May 21, 2014 4:57 pm
by Patrizio
Antonio, many thanks: with your change it works very well. :D

Re: Creazione menu a runtime

PostPosted: Fri Jun 06, 2014 6:58 pm
by Silvio.Falconi
scusa patrizio
mi dici per favore ( magari con un test sample) che cosa fa la modifica perchè non l'ho capita ....grazie