User Tools

Site Tools


Creating a menu from a resource

by Jon Ripley, June 2006

The normal method of creating a menu is to use the Windows API functions “CreateMenu”, “CreatePopupMenu” and “AppendMenu” to create a menu structure:

        SYS "CreatePopupMenu" TO m_file%
        SYS "AppendMenu", m_file%, 0, 100, "&New"+CHR$9+"Ctrl+N"
        SYS "AppendMenu", m_file%, 0, 101, "&Open"+CHR$9+"Ctrl+O"
        SYS "AppendMenu", m_file%, 0, 102, "&Save"+CHR$9+"Ctrl+S"
        SYS "AppendMenu", m_file%, 0, 103, "Save &As"+CHR$9+"Ctrl+A"
        SYS "AppendMenu", m_file%, 2048, 104, " "
        SYS "AppendMenu", m_file%, 0, 105, "E&xit"+CHR$9+"Alt+F4"
        SYS "CreateMenu" TO m_main%
        SYS "AppendMenu", m_main%, 16, m_file%, "&File"
        SYS "SetMenu", @hwnd%, m_main%
        SYS "DrawMenuBar", @hwnd%

However there may be circumstances when the menu has already been designed and its template is contained as a resource within a DLL or EXE file. In that case you can create the menu in your program using the resource data. The code example below creates a menu from the resource data for the BB4W IDE menu (in BBCWIN.EXE or BBCWDEM.EXE):

        REM Acquire handle to BBCWIN.EXE or BBCWDEM.EXE
        SYS "GetModuleHandle", 0 TO hmod%
        REM Load menu template into memory
        SYS "LoadMenu", hmod%, "BBCWIN" TO hmenu%
        SYS "LockResource", hmenu%
        REM Attach menu to main window
        SYS "SetMenu", @hwnd%, hmenu%
        REM Draw the menu
        SYS "DrawMenuBar", @hwnd%
        REM Your program goes here...
        REM Destroy the menu and free used memory
        SYS "DestroyMenu", hmenu% TO ret%

Naturally this code will only run within the BB4W IDE, because if compiled to an executable file the menu resource data will not be present.

To load the menu resource data from a different file (for example a DLL) then replace the call to “GetModuleHandle” with a call to “LoadLibrary” as follows:

        SYS "LoadLibrary", "EXAMPLE.DLL" TO hmod%
This website uses cookies for visitor traffic analysis. By using the website, you agree with storing the cookies on your computer.More information
creating_20a_20menu_20from_20a_20resource.txt · Last modified: 2018/04/16 15:20 by richardrussell