News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Using menus

Started by jj2007, September 25, 2023, 08:36:35 AM

Previous topic - Next topic

jj2007

Below a simple reader for rtf and asm files:

------------------------------------------------------------
GuiParas equ "Hello World", w500, h400, b LiteBlueGreen, icon Printer    ; width+height, background colour
GuiMenu equ @File, &Open, &Save, &Print, -, E&xit, @Edit, Undo, Copy, Paste

include \masm32\MasmBasic\Res\MbGui.asm
  GuiControl MyEdit, "RichEdit", wCL$(), bcol LiteYellow, fcol Black, font -14    ; load file in commandline
  GuiControl MyStatus, "statusbar", wRec$("Started "+fTime$())

Event Menu
  Switch_ MenuID    ; 0...n: see the order of the GuiMenu line above
  Case_ 0
  .if FileOpen$("Rich sources=*.asc|All sauces=*.as?;*.inc|All files=*.*")
          SetWin$ hMyEdit=FileRead$(FileOpen$())
  .endif
  Case_ 1: MsgBox 0, "Saving not implemented, sorry", "Hi", MB_OK
  Case_ 2: <void PrintRtf(hMyEdit)>        ; print selection (if any) or whole document
  Case_ 3: invoke SendMessage, hGui, WM_CLOSE, 0, 0
  Case_ 4: invoke SendMessage, hMyEdit, WM_UNDO, 0, 0
  Endsw_
GuiEnd
Rsrc
#include "resource.h"
01 RT_MANIFEST    "\\Masm32\\MasmBasic\\Res\\XpManifest.xml"
Rsrc    OPT_Arg1    ?:\Masm32\MasmBasic\Res\SkelGuiMini.asc
------------------------------------------------------------

With the GuiMenu equ @File, ... line you define the menu. Both GuiParas and GuiMenu must come before the include line.

Afterwards, you can react in GuiEvent to messages using a simple Switch_ , where 0 is the first menu item, 1 the second etc, not counting the @titles.

The OPT_Arg1 ?:\Masm32\MasmBasic\Res\SkelGuiMini.asc passes a filename via the commandline when building the source. It is not needed for the release version, but it does no harm either ;-)

Note that bare metal fans can also use the traditional method with resources. To do so, use e.g. the following:
Rsrc
#include "resource.h"
IDI_APPLICATION ICON    "\\Masm32\\MasmBasic\\icons\\Smiley.ico"
01 RT_MANIFEST    "\\Masm32\\MasmBasic\\Res\\XpManifest.xml"
#define IDC_MENU    100
#define IDM_SAYHELLO    101
#define IDM_GETTEXT    102
#define IDM_CLEAR    103
#define IDM_EXIT    104
IDC_MENU  MENU
BEGIN
    POPUP "The menu"
    BEGIN
        MENUITEM "Say Hello", IDM_SAYHELLO
        MENUITEM "Get Text", IDM_GETTEXT
        MENUITEM "Clear Edit Box", IDM_CLEAR
        MENUITEM "", , MFT_SEPARATOR
        MENUITEM "E&xit", IDM_EXIT
    END
END
Rsrc

Afterwards, select in RichMasm the #define IDC_... lines and press Ctrl N. Paste the result in the main code somewhere above the Event Menu, and change the Switch_ accordingly. Type IDC_MENU=100 before the include line, and you are done. It's slightly more complicated than the GuiMenu line but it works (you may need the latest version) :thumbsup:

P.S.: Case_ 2, PrintRtf works fine, but I recommend nonetheless to test it with PrintToPdf or similar in order not to waste paper.