TGC Codebase Backup



Windows Style MessageBoxs and InputBoxs by Anonymous Coder

9th Jul 2005 8:43
Summary

This code contains a function "DlgBox", which will display a Windows style InputBox or a MessageBox according to the Mode parameter.



Description

This code contains a function "DlgBox", which will display a Windows style InputBox or a MessageBox according to the Mode parameter.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    
`***************************************************************************************
`           WINDOWS MESSAGE BOX AND INPUT BOX FUNCTION DEMO                            `
`         ---------------------------------------------------                          `
`  AUTHOR - HARI KRISHNAN G.  a.k.a eXeption                                           `
`  SUBMITTED TO THE GAME CREATORS CODE BASE                                            `
`***************************************************************************************

Disable EscapeKey
sync on
sync rate 0
sync

InputString as string

print "Press any key to display an Input Box, where you can enter a string..."
sync

Repeat
Until scancode() <> 0
Repeat
Until scancode() = 0

InputString = DlgBox("Sample Input Box","Enter a string which this dialog box returns","Tip : ESC - Abort , RETURN - Accept",1)
print "You entered :> """ + InputString
sync

print "Press any key to display a Message Box, which simply displays a message..."
sync
Repeat
Until scancode() <> 0
Repeat
Until scancode() = 0

InputString = DlgBox("Sample Message Box","This Message box displays a message","Tip : ESC - Abort , RETURN - Accept",0)
print "That was all... Hope you liked it!"
sync
Repeat
Until scancode() <> 0
Repeat
Until scancode() = 0

End


`***************************************************************************************
`           WINDOWS MESSAGE BOX AND INPUT BOX FUNCTION                                 `
`         ----------------------------------------------                               `
`  AUTHOR - HARI KRISHNAN G.  a.k.a eXeption                                           `
`  SUBMITTED TO THE GAME CREATORS CODE BASE                                            `
`***************************************************************************************
Function InRect(fLeft as Integer, fTop as Integer, fRight as Integer, fBottom as Integer, x as Integer, y as Integer)
    IsInside as Boolean
    IsInside = 0
    if ((x >= fLeft) and (x <= fRight)) and ((y >= fTop) and (y <= fBottom)) Then IsInside = 1
EndFunction IsInside

function colour(num)
if num=1 then ink RGB(236,233,216),0
if num=2 then ink RGB(162,160,148),0
if num=3 then ink RGB(253,250,232),0
if num=4 then ink 0,0
if num=5 then ink rgb(255,255,255),0
if num=6 then ink rgb(49,106,197),0
endfunction

Function Window3d(x1 as Integer, y1 as Integer, x2 as Integer, y2 as Integer, mode as integer)
    Colour(1) : box x1,y1,x2,y2
    select mode
        case 2
            `Light
            Colour(3) : line x1, y1, x2, y1 : line x1, y1, x1, y2
            `Shade
            Colour(2) : line x1, y2, x2+1, y2 : line x2, y1, x2, y2+1
        EndCase
        Case 3
            `Light
            Colour(2) : line x1, y1, x2, y1 : line x1, y1, x1, y2
            `Shade
            Colour(3) : line x1, y2, x2+1, y2 : line x2, y1, x2, y2+1
        EndCase
        Case Default
            `Light
            Colour(5) : line x1+1, y1+1, x2-1, y1+1 : line x1+1, y1+1, x1+1, y2-1
            Colour(3) : line x1, y1, x2, y1 : line x1, y1, x1, y2
            `Shade
            Colour(4) : line x1, y2, x2+1, y2 : line x2, y1, x2, y2+1
            Colour(2) : line x1+1, y2-1, x2, y2-1 : line x2-1, y1+1, x2-1, y2
        EndCase
    EndSelect
EndFunction

Function DlgBox(Title as string, Prompt as string, Prompt2 as string, mode as integer )
    ReturnStr$ as String
    txt as string
    Cv as Boolean
    Csr as string

    Wx1 as Integer
    Wy1 as Integer
    Wx2 as Integer
    Wy2 as Integer

    Bx1 as Integer
    By1 as Integer
    Bx2 as Integer
    By2 as Integer

    Cx1 as Integer
    Cy1 as Integer
    Cx2 as Integer
    Cy2 as Integer

    Tx1 as Integer
    Ty1 as Integer
    Tx2 as Integer
    Ty2 as Integer

    ReturnStr$ = ""

    Wx1 = (Screen Width() - 300)/2
    Wy1 = (Screen Height() - 150)/2
    Wx2 = Wx1 + 300
    Wy2 = Wy1 + 150

    Bx1 = Wx2 - 2 * 60 - 20
    By1 = Wy2 - 20 - 10
    Bx2 = Bx1 + 60
    By2 = By1 + 20

    Cx1 = Bx2 + 10
    Cy1 = By1
    Cx2 = Cx1 + 60
    Cy2 = By2

    Tx1 = Wx1 + 10
    Ty1 = By1 - 30
    Tx2 = Wx2 - 10
    Ty2 = Ty1 + 20
    txt = ""
    Csr = " "
    Cv = 0

    Set text font "Arial"
    Set Text Size 14

    `The Window
    Window3d(Wx1,Wy1,Wx2,Wy2,0)

    `The button
    Window3d(Bx1,By1,Bx2,By2,2) `yes
    Window3d(Cx1,Cy1,Cx2,Cy2,2) `no
    Colour(4)
    set text to Normal
    Center text (Bx1 + Bx2) /2 , By1 + 3 , "Yes"
    Center text (Cx1 + Cx2) /2 , Cy1 + 3 , "No"

    Colour(6)
    set text to bold
    box Wx1+2,Wy1+2,Wx2-2,Wy1 + Text Height(Title) + 4
    Colour(5)
    text Wx1 + 10, Wy1 + 2, Title

    Colour(4)
    set text to Normal
    text Wx1 + 20, Wy1 + 30, Prompt
    text Wx1 + 20, Wy1 + 30 + text Height(Prompt), Prompt2

    if Mode = 1
        Window3d(Tx1,Ty1,Tx2,Ty2,3)
        Ink RGB(255,255,255),0
        Box Tx1+1,Ty1+1,Tx2-1,Ty2-1
    Endif

    If mode = 1
        Hide Mouse
        set text opaque
        do
            repeat
                Ink RGB(255,255,255),0
                Box Tx1+1,Ty1+1,Tx2-1,Ty2-1
                Ink 0, RGB(255,255,255)
                text Tx1 + 2 ,Ty1+2,txt

                text Tx1 + 2 + Text Width(txt),Ty1+2,csr

                if (i >= 20)
                    if (Cv = 0)
                        Cv = 1
                        csr = "|"
                    else
                        Cv = 0
                        csr = " "
                    EndIF
                    i=0
                EndIF
                i=i+1

                sync
            Until (InKey$() <> "")

            if EscapeKey() Then txt="" : Exit
            if ReturnKey() Then Exit

            `Backspace
            if Scancode() = 14 Then txt = Left$(txt, Len(txt)-1) Else txt=txt + InKey$()

            Sync

            repeat
            Until (scancode() = 0)
        Loop
        set text transparent
        ReturnStr$ = txt
    EndIF

    Show Mouse
    do
        if (inRect(Bx1,By1,Bx2,By2,MouseX(),MouseY()) = 1) and (MouseClick() = 1) then Exit
        if (inRect(Cx1,Cy1,Cx2,Cy2,MouseX(),MouseY()) = 1) and (MouseClick() = 1) then ReturnStr$="" : Exit
        if ReturnKey() Then Exit
        if EscapeKey() Then ReturnStr$="" : Exit
        Sync
    Loop

    wait 100
EndFunction ReturnStr$