Menu Example

In the following code example, we create a dialog with a menu, outlining the concepts discussed in this chapter. Feel free to use this code as a base for your own DDT projects. This example is also available in your PowerBASIC installation, in the \PBWIN\SAMPLES\DDT\MENU folder.

 

'===========================================================

'

' Simple example of an application that has a menu and

' requires absolutely no API calls!

'

'===========================================================

 

#COMPILE EXE

 

%IDOK = 1

%IDCANCEL = 2

%IDTEXT = 100

%BN_CLICKED = 0

%BS_DEFAULT = 1

%MF_ENABLED = 0

%WM_COMMAND = &H111

 

%ID_OPEN = 401

%ID_EXIT = 402

%ID_OPTION1 = 403

%ID_OPTION2 = 404

%ID_HELP = 405

%ID_ABOUT = 406

 

'-----------------------------------------------------------

 

' ** Global variable to receive the user name

GLOBAL gsUserName AS STRING

 

'-----------------------------------------------------------

 

CALLBACK FUNCTION OkButton()

  IF CBMSG = %WM_COMMAND AND CBCTLMSG = %BN_CLICKED THEN

    CONTROL GET TEXT CBHNDL, %IDTEXT TO gsUserName

    DIALOG END CBHNDL, 1

    FUNCTION = 1

  END IF

END FUNCTION

 

'-----------------------------------------------------------

 

CALLBACK FUNCTION CancelButton()

  IF CBMSG = %WM_COMMAND AND CBCTLMSG = %BN_CLICKED THEN

    DIALOG END CBHNDL, 0

    FUNCTION = 1

    END IF

  END FUNCTION

 

'-----------------------------------------------------------

 

CALLBACK FUNCTION DlgProc()

  IF CBMSG = %WM_COMMAND THEN

    IF CBCTL => %ID_OPEN AND CBCTL <= %ID_ABOUT THEN

      MSGBOX "WM_COMMAND received from a menu item!", &H00002000& ' = %MB_TASKMODAL

      FUNCTION = 1

    END IF

  END IF

END FUNCTION

 

'-----------------------------------------------------------

 

FUNCTION PBMAIN () AS LONG

  LOCAL hDlg AS DWORD

  LOCAL lResult AS LONG

  LOCAL hMenu AS DWORD

  LOCAL hPopup1 AS DWORD

  LOCAL hPopup2 AS DWORD

 

  ' ** First create a top-level menu:

  MENU NEW BAR TO hMenu

 

  ' ** Add a top-level menu item with a popup menu:

  MENU NEW POPUP TO hPopup1

  MENU ADD POPUP, hMenu, "&File", hPopup1, %MF_ENABLED

  MENU ADD STRING, hPopup1, "&Open", %ID_OPEN, %MF_ENABLED

  MENU ADD STRING, hPopup1, "&Exit", %ID_EXIT, %MF_ENABLED

  MENU ADD STRING, hPopup1, "-", 0, 0

 

  ' ** Now we can add another item to the menu that will bring up a sub-menu.

  ' First we obtain a new popup menu handle to distinguish it from the first

  ' popup menu:

  MENU NEW POPUP TO hPopup2

 

  ' ** Now add a new menu item to the first menu.

  ' This item will bring up the sub-menu when selected:

  MENU ADD POPUP, hPopup1, "&More Options", hPopup2, %MF_ENABLED

 

  ' ** Now we will define the sub menu:

  MENU ADD STRING, hPopup2, "Option &1", %ID_OPTION1, %MF_ENABLED

  MENU ADD STRING, hPopup2, "Option &2", %ID_OPTION2, %MF_ENABLED

 

  ' ** Finally, we'll add a second top-level menu and popup.

  ' For this popup, we can reuse the first popup variable:

  MENU NEW POPUP TO hPopup1

  MENU ADD POPUP, hMenu, "&Help", hPopup1, %MF_ENABLED

  MENU ADD STRING, hPopup1, "&Help", %ID_HELP, %MF_ENABLED

  MENU ADD STRING, hPopup1, "-", 0, 0

  MENU ADD STRING, hPopup1, "&About", %ID_ABOUT, %MF_ENABLED

 

  ' ** Create a new dialog template

  DIALOG NEW 0, "What is your name?", ,, 160, 60, 0, 0 TO hDlg

 

  ' ** Add controls to it

  CONTROL ADD TEXTBOX, hDlg, %IDTEXT, "", 14, 12, 134, 12, 0

  CONTROL ADD BUTTON, hDlg, %IDOK, "OK", 34, 32, 40, 14, %BS_DEFAULT CALL OkButton

  CONTROL ADD BUTTON, hDlg, %IDCANCEL, "Cancel", 84, 32, 40, 14, 0 CALL CancelButton

 

  MENU ATTACH hMenu, hDlg

 

  ' ** Display the dialog

  DIALOG SHOW MODAL hDlg, CALL DlgProc TO lResult

 

  ' ** Check the dialog return result

  IF lResult THEN

    MSGBOX "Hello " & gsUserName

  END IF

END FUNCTION

 

'-----------------------------------------------------------

 

See Also

Menus

Menu Walkthrough

More on the Menu

Menu State