Re: Centering Text in Win32

new topic     » goto parent     » topic index » view thread      » older message » newer message

Rev. Ferlin Scarborough asked:

> I need a little help with a procedure to
> Center Text on a line in a Window, I am
> using win32lib and the Window is Re-sizeable.

I've included the code. Note that if you are not calling repaintRect(), the
(x2,y2) parameter of onPaint will be equal to the size of the window. The
getFontSize() routine returns the *average* size of a character in the
current font.

Hope this helps!

-- David Cuny


-- Begin Code: Center Text Demo --

include win32lib.ew

constant Win =
    create( Window, "Centered Text Demo", 0, Default, Default, 200, 100, 0 )

-- setFont( Win, "Courier New", 12, 0 )

procedure Paint( integer x1, integer y1, integer x2, integer y2 )

    integer textWide, winWide, atX
    sequence fontSize, winSize, text

    -- set text
    text = "Hello, World!"

    -- get size of font
    fontSize = getFontSize( Win )

    -- text width
    textWide = fontSize[1] * length( text )

    -- get size of window
    winSize = getWindowExtent( Win )
    winWide = winSize[1]


    -- calculate start of text
    atX = floor( (winWide/2) - (textWide/2) )

    -- display, if there is room
    if atX > 0 then
        setPosition( Win, atX, 0 )
        wPuts( Win, "Hello, World!" )
    end if

end procedure

-- tell Windows when to do the action
    onPaint[Win] = routine_id( "Paint" )

-- hand control over to Windows
WinMain( Win )

-- End Code: Center Text Demo --

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu