# Adding a Message Box to a Window

Now that we've introduced both simple windows and message boxes, lets put both together in this simple example.

-- example4.exw

    -- This opens a blank window,
    -- and adds an EXIT item to a menubar,
    -- then adds a message box triggered by a button control.

    include win32lib.ew

    constant SimpleWin =
        create( Window, "Tiny 3", 0, Default, Default, 200, 150, 0 ),

        MainMenu = create( Menu, "Eξt", SimpleWin, 0, 0, 0, 0, 0 ),
        ExitNow  = create( MenuItem, "Now!", MainMenu, 0 ,0 ,0 ,0 ,0 ),

    -- here's where we add a button to be shown in our main window
        AboutBox = create( PushButton, "About", SimpleWin, 10, 10, 50, 25, 0 )

    --------

    -- this procedure will be executed
    -- by Windows 'only' when we click on the button,
    procedure onButton_About( integer self, integer event, sequence params )
        object jk

        jk = message_box("...it's GREAT !", "* Euphoria *", 0 )
    end procedure

    -- this tells Windows to run the above procedure
    -- when the button is 'triggered' by the user.
    setHandler( AboutBox, w32HClick, routine_id( "onButton_About" ) )

    --------

    -- execute this when the user selects 'Now!'
    procedure onMenu_Exit( integer self, integer event, sequence params )
        closeWindow( SimpleWin )
    end procedure

    -- direct Windows to the above, 'if' it's selected.
    setHandler( ExitNow, w32HClick, routine_id("onMenu_Exit") )

    -- go ahead, run the window ! --
    WinMain( SimpleWin, Normal )
    --------------------------------
    -- end of example4.exw --

I'll try to explain exactly what's done above.

First we 'create' our main window, with "Tiny 3" in it's caption bar. The next parameter indicates it has no owner, since it's our 'main' window. The 'Default' parameters tell Windows it's in charge of the window's location on the screen. Our window has a size of 200 by 150 pixels. The last parameter tells Win32lib that it's a normal window with no special attributes.

The next line 'creates' a menubar in our window, with one menu selection, ( "Eξt" ). The '&' just before the 'x' character tells Windows this menu-item can be activated by using an ( ALT x ) key combination, as well as a mouse 'click'. The '&' character is a special 'reserved' symbol in menus for keyboard triggering, and underlines the following character in the menu text to indicate this 'feature'. The third parameter ( SimpleWin ) means that this menu-item is 'owned' by our main window. The next five ( 0's ) are not used, but are required, since all create() functions require 8 parameters.

Our second menu 'creation', is similar to the first, except that, because it is a dropdown menu item,
( ExitMenu, with a first parameter of 'MenuItem' ),
triggered by the first,
it is 'owned' by the first ( MainMenu ) menu.

The next line 'creates' a pushbutton, owned by our main ( SimpleWin ) window, with a text label of "About" . It's located at 10 by 10 pixels within our main window's 'client' area, with a size of 50 by 25 pixels, and again, no special attributes.

The 'client' area, in this case, is the area below both the caption bar, and the menu bar of our main window. The normal main window borders ( usually of 4 pixels ), are not part of the client area, and are also ignored by these 'location' parameters.

The two following procedures simply tell Windows 'what to do' if the button is 'clicked', or if the Exit / Now! menu-item is chosen.

The two 'event-handling' routine_id()'s,
( setHandler( AboutBox, ... ), and setHandler( ExitNow, ... ),
established for the two procedures, tell Windows 'where to go' in our code to handle these events.

... and last, but not least, the last line ( WinMain() ), gets the whole ball rolling, by actually handing control over to Windows, which 'runs' the above code ! ( forget this line, and nothing seems to happen... ;-) )

NOTE: The syntax for event handlers has been changed in these documents and in the demo programs to accord with the latest Win32Lib usage rather than that which prevailed when Wolfritz wrote this very good tutorial. When you want to know more about the new setHandler( … ) format have a look at html Extra_00.html.

... Whew, glad that's over with ...

Okay,

-

-

I've done all the work so far, so it's your turn.

If you haven't done so already, create a folder somewhere for your own demo programs, start up your text editor, and type in the following example.

This program isn't much more complicated than the one above. This time though, we are going to create two windows, side by side. We're also going to introduce a new control, ( CText() ), into which we can place some text. It's probably easiest if you just print out this lesson to copy from.
( It's only three pages. )

So here goes....

    -- your program --
    include win32lib.ew
    without warning

    constant
      FirstWin = create(Window,"First",0,100,100,200,150,0),
      Text1    = create(CText,"How's it going..",FirstWin,10,10,180,20,0),

      SecondWin = create(Window,"Second",FirstWin,300,100,200,150,0),
      Text2     = create(CText,"...in EUPHORIA ?",SecondWin,10,10,180,20,0)

    procedure open_second( integer self, integer event, sequence params )
      openWindow( SecondWin, Normal )
    end procedure

    setHandler( FirstWin, w32HOpen = routine_id( "open_second" ) )

    -- start it up --
    WinMain( FirstWin, Normal )
    ----- end -------

You'll notice that we specified both locations, explicitly, so the windows wouldn't overlap.

We also included another new event handler, ( setHandler( FirstWin, w32HOpen, ... ) ), which ran the open_second() procedure right after the first window 'opened'. Try commenting out the onOpen[First... line, run it again, and see what happens.

When you first start up this program, try to kill it with the [x] button in the first window, and see what happens.
Then run it again, and click the [x] button in the second window. What's different here ? This is your first 'clue' about window 'ownership'.

Since the first window 'owns' the second window, killing it kills both, however...

...and, while we're discussing them...

Unless you have special formatting requirements, you can usually 'feed' a long line to a message box. It will 'wrap' the text on words automatically, and won't ever get wider than 60 percent of the screen width.

CONTENTS

..end of lesson.