...using WINDOWS dialog boxes to choose COLORS
and Fonts.

This is a simple little program that shows you how to use two common dialogs built into Windows.

The first, getFontDialog(), starts up the standard Windows font selection dialog, from which you can select a font, font size, and font attributes. As used here, the return values from this dialog are used to display the font's name in the selected font, in it's selected size. Just to make it a little more useful, it also reports the actual font height. This can be used to help define your window or control height, if you actually want to use a particular font in one of these.

The call to getColorDialog(), simply changes the main window's background color, if a color has been chosen.

-- dialogs.exw

-- dialogs.exw
include win32lib.ew
include std/pretty.e
without warning

constant
Dialogs  = create( Window, "Screen Dialogs", 0, 50, 50, 520, 310, 0), 
Menu1  = create( Menu, "Dialog", Dialogs, 0, 0, 0, 0, 0), 
Menu2  = create( MenuItem, "Color", Menu1, 0, 0, 0, 0, 0), 
Menu3  = create( MenuItem, "Font", Menu1, 0, 0, 0, 0, 0), 
Menu4  = create( MenuItem, "&Exit", Menu1, 0, 0, 0, 0, 0), 
Result = create( LText, "", Dialogs, 0, 0, 512, 258, 0)


procedure get_font( integer self, integer event, sequence params )
    object font, font_style, font_height
    sequence fontsize
        
    -- call the dialog
    font = getFontDialogEx( Dialogs, w32GFD_DEFAULT_FLAGS, 0  )

    -- I'm cheating a bit here, since there
    -- are actually a few more possible return values.
    if sequence( font ) then
    
        -- we're in business!
        if equal( font[3], 3 ) then font_style = "Bold Italic"
        elsif equal( font[3], 2 ) then font_style = "Italic"
        elsif equal( font[3], 1 ) then font_style = "Bold"
        else font_style = "Regular"
        end if

        -- set the chosen font, and use it
        -- to report the results.
        setFont( Result, font[1], font[2], font[3] )
        -- font_height = getFontMetric( Result, tmHeight)    -- formally ok, but getFontMetric bombs here
        fontsize = getFontSize ( Result )                    -- try another way
        
        -- fontsize[3] is the height
        setText( Result, font[1] & ' '  &
        sprintf( "%d", {font[2]} ) & ' ' & font_style & "\nheight=" &
        sprintf( "%d", {fontsize[3]} ) )
    end if
    
end procedure

atom color   color = BrightWhite

procedure get_color( integer self, integer event, sequence params )
    atom chosencolor
    
    -- call the dialog
    chosencolor=getColorDialog( Dialogs, color )
    
    if chosencolor != -1 then
        setWindowBackColor( Result, chosencolor )
        setWindowBackColor( Dialogs, chosencolor )
    end if
    
end procedure

procedure get_lost( integer self, integer event, sequence params )
    closeWindow( Dialogs )
end procedure

setHandler( Menu2, w32HClick, routine_id( "get_color" ))
setHandler( Menu3, w32HClick, routine_id( "get_font" ))
setHandler( Menu4, w32HClick, routine_id( "get_lost" ))

WinMain( Dialogs, Normal )
--end

The variables:

atom color   color=BrightWhite

...are used as a default or 'pre-set' color choice.
If the user chooses nothing ( clicks on OK, or Cancel ), in the color dialog, this is the color that will be used to set the background.

...end...

CONTENTS