May 06, 2024, 10:05:14 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


CreateMenu

Started by aurelCB, January 28, 2011, 03:39:12 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

aurelCB

I have some problems with command CreateMenu().
from help this must be proper way:
hMenu = CreateMenu()
hPopup = CreateMenu(1)
APPENDMENU(hMenu, "File", MF_POPUP|MF_STRING, hPopup)
'show menu
SETMENU win, hMenu


Ok this work fine.
So when i want add next menu i do this
hMenu = CreateMenu()
hPopup = CreateMenu(1)
hPopup2=CreateMenu(1)
APPENDMENU(hMenu, "File", MF_POPUP|MF_STRING, hPopup)
APPENDMENU(hMenu, "Edit", MF_POPUP|MF_STRING, hPopup2)
SETMENU win,hMenu


Is this proper way ?
Why i ask this?
Becose sometimes menu not apear?
My question will be -if i use CreateMenu() do i must first show first created menu(...with command SETMENU) then create another?
or my example is right way?

Just one thing i dont use child widow or MDI window.
I need this in first place for ABasic that i can create menus on the fly.
thanks advance...

Aurel

LarryMc

The code you show looks right to me.
Since everything is ultimately part of hmenu you only need to setmenu once to draw the entire menu (the way I understand it)
Are you executing the code in some sort of loop?

Under what conditions do they not appear?

LarryMc
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

aurelCB

Larry i create hMenu,hPopup as local variable in separate subroutines.
And as you know i use usual way to store them in linked list.
When i first time run ABasic IDE and run program menu work(appear) but then i second time run
program sometimes work and sometimes not.
What might be this?

aurelCB

January 29, 2011, 08:20:20 AM #3 Last Edit: January 29, 2011, 08:45:41 AM by aurelCB
Im not 100% sure but i think that i found what is wrong with my menus.
I found that creating popup menu with APPENDMENU need only one constant
which is MF_POPUP not MF_POPUP|MF_STRING.
For an item u must use MF_STRING.
I hope that next code will explain far better then words.
REM define a window variable
DEF w1,w2,w3:WINDOW
Pointer pw,pw2,pw3
'pw = w1
pw2=w2
pw3=w3
CONST MF_POPUP = 0x10
CONST MF_STRING = 0x0
REM open the window
OPENWINDOW w1,0,0,350,350,@MINBOX|@MAXBOX|@SIZE,0,"Simple Window",&main
REM print a message
SETWINDOWCOLOR w1,RGB(220,220,230)
Move w1,120,20:PRINT w1,"Hello World"
'create button
CONTROL w1,@SYSBUTTON,"Button1",72,44,70,20,0x50000000,1
'open new window
NewWindow1()
NewWindow2()

REM when w1 = 0 the window has been closed
'IF *pw<>0
WAITUNTIL w1 = 0
'WAITUNTIL *<window>pw2 = 0
'ENDIF
END
'---
REM every time there is a message for our window

SUB main
'IF @hitwindow=*<window>pw

IF @Message = @IDCLOSEWINDOW
        CLOSEWINDOW w1
'IF *<window>pw2<>0 THEN CLOSEWINDOW *<window>pw2
ENDIF
'ENDIF
RETURN
ENDSUB
'//////////////////////////////////////////////////
SUB window_handler
IF @Message = @IDCLOSEWINDOW
        CLOSEWINDOW *<window>@hitwindow
ENDIF

RETURN FALSE
ENDSUB
'/////////////////////////////////////////////////

SUB NewWindow1
OPENWINDOW *<window>pw2,400,0,200,200,@MINBOX,0,"Window 2",&window_handler
SETWINDOWCOLOR *<window>pw2,RGB(220,220,250)
hMenu = CreateMenu()
hPopup = CreateMenu(1)
hPopup2 = CreateMenu(1)
'set popup 1
APPENDMENU(hMenu, "File", MF_POPUP, hPopup)
'add items to popup 1
APPENDMENU(hPopup, "New", MF_STRING, 1)
APPENDMENU(hPopup, "Open", MF_STRING, 2)
APPENDMENU(hPopup, "Save", MF_STRING, 3)
'add items to popup 2
APPENDMENU(hMenu, "Edit", MF_POPUP, hPopup2)
APPENDMENU(hPopup2, "Cut", MF_STRING, 4)
APPENDMENU(hPopup2, "Copy", MF_STRING, 5)
APPENDMENU(hPopup2, "Paste", MF_STRING, 6)
'show menu
SETMENU *<window>pw2,hMenu
ENDSUB

SUB NewWindow2
OPENWINDOW *<window>pw3,620,0,150,200,@MINBOX,0,"Window 3",&window_handler
ENDSUB


all best... :)
Aurel

LarryMc

January 29, 2011, 09:42:16 AM #4 Last Edit: January 29, 2011, 09:43:58 AM by LarryMc
QuoteI found that creating popup menu with APPENDMENU need only one constant
which is MF_POPUP not MF_POPUP|MF_STRING.

CONST MF_POPUP = 0x10
CONST MF_STRING = 0x0
MF_POPUP = MF_POPUP|MF_STRING
0x10 = 0x10 | 0x0

So having MF_POPUP|MF_STRING when you only need MF_POPUP doesn't hurt.

Having MF_POPUP|MF_STRING  when you only need MF_STRING won't work.

what you posted here:
SUB NewWindow1
OPENWINDOW *<window>pw2,400,0,200,200,@MINBOX,0,"Window 2",&window_handler
SETWINDOWCOLOR *<window>pw2,RGB(220,220,250)
hMenu = CreateMenu()
hPopup = CreateMenu(1)
hPopup2 = CreateMenu(1)
'set popup 1
APPENDMENU(hMenu, "File", MF_POPUP, hPopup)
'add items to popup 1
....
'add items to popup 2
APPENDMENU(hMenu, "Edit", MF_POPUP, hPopup2)
.....
'show menu
SETMENU *<window>pw2,hMenu
ENDSUB

is really the same as you posted in the beginning:
hMenu = CreateMenu()
hPopup = CreateMenu(1)
hPopup2=CreateMenu(1)
APPENDMENU(hMenu, "File", MF_POPUP|MF_STRING, hPopup)
APPENDMENU(hMenu, "Edit", MF_POPUP|MF_STRING, hPopup2)
SETMENU win,hMenu

because
MF_POPUP|MF_STRING =0x10
MF_POPUP = 0x10

LarryMc


LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

aurelCB

I understand your point Larry and your answer is logical.
I think that i must test this more....

aurelCB

I spend my free time today looking into how properly crete menus and i was
shocked.It looks that IWB dont like creating menus on runtime especialy not insude subroutines with local
variables.I dont know how explain such a weird behavier of program.
Sometimes menu appear properly ,sometimes not,sometimes not items showed under menu...
this really going me creazy >:(

Another weird stuff is that CreateMenu and SetMenu commands are in conflict with API called same command
even you use aliases.
Totataly weird is command SetMenu from api which you cannot force to work,why?
simply becose he need differnt type of variable then SetMenu which is build into language.
I dont know what to say more...

LarryMc

January 30, 2011, 05:32:15 PM #7 Last Edit: January 30, 2011, 05:35:59 PM by LarryMc
Don't know why you are having so many problems

Here is some source code where IWB is creating a menu internally:

DECLARE IMPORT,CreateMenuA ALIAS CreateMenu(),UINT
DECLARE IMPORT,AppendMenuA(hMenu as UINT,uFlags as UINT,uIDNewItem as UINT,lpNewItem as STRING)
DECLARE IMPORT,SetMenuA alias SetMenu(hwnd as UINT,hMenu as UINT),INT
hmenu = CreateMenuA()
hsubmenu = CreateMenuA()
AppendMenuA(hsubmenu,0,IB_WM_CASCADE,"&Cascade")
AppendMenuA(hsubmenu,0,IB_WM_TILE,"&Tile")
AppendMenuA(hsubmenu,0,IB_WM_ARRANGE,"&Arrange Icons")
AppendMenuA(hmenu,MF_POPUP,hsubmenu,"&Window")
SetMenuA(win.hwnd,hmenu)


The three IWB commands use the aliased API functions aboue to create menus.

Can you build me a demo program that has menus that don't show so I can compile it on my computer?

Quote from: AurelTotataly weird is command SetMenu from api which you cannot force to work,why?
if you use the API setmenu (you have to alias it) you will also need to use
DrawMenuBar(win.hwnd) to redraw the menu and pick up any changes.

LarryMc
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

aurelCB

Larry i will try one more time with DrawMenuBar.
Yes i can build demo that you can compile this on your computer.
Most unusual thing is that sometimes work as i espected and sometimes not
which i dont see with any other programs before ???
Thank you Larry ....i will try more.

LarryMc

A problem I had one time with things acting erratic and seeming to disappear (not menus though) was due to the way way I was working on my project.

1st - it was a multi source file project
2nd - I was using PROJECTGLOBAL in  in an included globals.eba file
3rd - I changed my globals.eba file to support additional global variables shared between two source files.
4th - I recompiled the 2 source files and the globals.eba file and relinked the project.
5th - things started acting erratic.

When I finally recompiled the entire project and relinked everything started working correctly.

Are you doing anything like that?

LarryMc
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

aurelCB

No Larry ABasic is only one source file (currently about 8000 lines of code).
New window have global scope.
All variables for menus are defined as local same as for other controls.
One maby thing is that i dont use autodefine off - i dont use this command.
But as i say before why sometimes work and sometimes not is totaly weird.
I even defragment disk,clean registry etc ..etc...
and same thing.
I also try with Beginmenu but this option i cannot use .

LarryMc

do you have one or more real busy loops in your program?

LarryMc
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

aurelCB

Yes Larry i have few loops which are started if is something clicked .
Main Loop read main source code and when he match keyword WAIT he is stoped ....
I think but im not sure, look like this:

FOR......
....
....
    "WAIT"
      Return
...
...
NEXT
so i probably here made mistake,do you think that i must add BREAKFOR after RETURN that i exit from loop.
Until now everything work fine.
Another thing which i probably made wrong is scaning.
Under button RUN i have this:
IF (run1=0)&(error=0) THEN Gosub Runscript
'------------------------------------------------------------------------
/*If (stop1=1)&(error=0)
linenum = linenum + 1
GOSUB timersubpos  'this scan position in code for timer command "SUBTIMER"-timer event
GOSUB subbpos   ' this scan position in code for controlID event "SUBID"
GOSUB submpos  'this scan position in code for menunum event "SUBMENUID"

Endif

I hope that you have picture how executing work.
So under new window SUB main i have this loop which scan which menu item is clicked:
'for menu ID-s
IF @class = @IDMENUPICK
For m=1 TO 100
IF @menunum = m
IF lsubmenu[m]=0 then BREAKFOR
linenum = lsubmenu[m] + 1
if stop1 = 1 then gosub runscript
'BREAKFOR
ENDIF
Next m
ENDIF


lsubmenu[m] array hold positions of each 'SUBMENUID' command in ABasic source code.
So when i click for example item num 2 loop find position end start executing code after command 'SUBMENUID'.
I will return to first point in this post about scaning.
So i made changes in scaning and code under RUN button is this:
subbpos()
submpos()
------------------------------------------------------------------------
IF (run1=0)&(error=0) THEN Gosub Runscript


As you see i first scan code for subrutines and store positions in arrays and try again code for menu.
And now seems to me that work properly.I try same code 20 times...or maby more.
This time i think that i dont have multiple rescaning and it looks that i dont execute mulitple times same command.
Im not sure that is all OK but looks that work.
Example ABasic code which i run is:
'test Menu
DEFNUM MainMenu
DEFNUM menu_1
DEFNUM c
WIN 0 0 400 260 WS_CAPTION "Test Menu..."
MainColor 220 220 230

CreateMenu MainMenu
CreatePopupMenu menu_1
AddMenu MainMenu menu_1 File
AddMenuItem menu_1 1 New
AddMenuItem menu_1 2 Open
AddMenuItem menu_1 3 Save
SetMenu MainMenu

WAIT

SUBMENUID 3
MessageBox "Save" & "Clicked"
ENDSUB

SUBMENUID 1
MessageBox "New" & "Clicked"
ENDSUB


Again i apologize if i say something wrong becose im mad on myself in first place....

all best..
Aurel

aurelCB

Just one thing...
Im not sure or i constanly mix things like RETURN and ENDSUB.
So which one of this end subroutine and back to next line after subrutine is called.
I know why i mix this things - probably becose im still in mind in CBasic ::)
And maby i becose of this have weird errors.
One small example :

mysubrutine()  ----->
LABEL continue
.....
.....
....
SUB mysubrutine
....
....
....
...
RETURN ----> return back or...
....
...
ENDSUB ----> return to LABEL continue

I know that my question might look stupid and if made to many mistakes i wonder myself how
my ABasic even work.... ???

LarryMc

when you call a sub the endsub means the end of the subroutine so exit it
return means exit the subroutine right now.
in either case the next statement to be executed is the one following the statement that called the routine

you don't need a label

LarryMc
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

aurelCB

Label is just for orientacion nothing else.