Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning Programming for Dummies 2004.pdf
Скачиваний:
109
Добавлен:
17.08.2013
Размер:
8.05 Mб
Скачать

Chapter 14

Creating a User Interface

In This Chapter

Creating a window

Adding menus to a window

Placing controls in a window

Using dialog boxes

As the name implies, a user interface acts as the middleman between the user and your program. A user gives commands through your program’s

user interface, which passes those commands to the part of your program that actually does the work. Then your program passes data back to the user interface to display on-screen for the user to see.

Designing a Window

The most common element of a Windows program is a window, which is nothing more than a rectangle that appears on-screen. A window has the following two purposes:

To display your program’s commands, such as pull-down menus or command buttons, so that the user can use them to make selections or input information into the program

To display information on-screen for the user to see, such as a graph of a stock price or text that the user types

Creating a new window

To create a window, you need to use the OPEN command and define the text that you want to appear in the window titlebar, as the following example shows:

192 Part III: Advanced Programming with Liberty BASIC

NOMAINWIN

OPEN “Titlebar text” FOR Window AS #1

PRINT #1, “trapclose [quit]”

WAIT

[quit]

CONFIRM “Are you sure that you want to quit?”; quit$ IF quit$ = “no” THEN WAIT

CLOSE #1 END

This program tells Liberty BASIC to do the following:

1.The first line tells Liberty BASIC not to display the main window.

2.The second line uses the OPEN command to create a new window, display the Titlebar text string in the title bar of the window, and assign the nickname #1 to represent the window that you just created.

3.The third line tells the computer to follow the instructions following the [quit] label if the user tries to close the program. This line uses the PRINT command to detect when the user tries to close the window identified by the #1 nickname, but doesn’t actually print anything on the screen.

4.The fourth line uses the WAIT command to tell your program to wait for the user to do something.

5.The fifth line is the [quit] label.

6.The sixth line displays a Confirm dialog box that asks, Are you sure that you want to quit? The CONFIRM command automatically displays a Yes and No command button in the dialog box. If the user clicks the Yes button, the program stores a value of “yes” in the quit$ variable. If the user clicks the No button, it stores a value of “no” in the quit$ variable.

7.The seventh line checks to see whether the value of the quit$ variable equals ”no.If so, it runs the WAIT command to wait for the user to do something.

8.The eighth line closes the window that the nickname #1 identifies. This line runs only if the user clicks the Yes button in the Confirm dialog box that the sixth line displays.

9.The ninth line marks the end of the program.

When you run this program, a window appears on-screen and waits for you to click its close box before the program stops running.

Chapter 14: Creating a User Interface 193

Any time that you use the OPEN command to create a new window, you must eventually use the CLOSE command to shut the window. If you don’t use the CLOSE command eventually, your program may cause the computer to crash.

Defining the size and location of a window

Each time that you create a window, Liberty BASIC may display that window on a different part of the screen. If you want to specify the exact location where you want your window to appear, you can use the UpperLeftX and UpperLeftY commands, which define how far from the left (the X coordinate) and the top edge (the Y coordinate) the upper-left corner of your window appears on-screen, as shown in Figure 14-1. If you want your window to appear exactly 225 pixels from the top and 139 pixels from the left edge of the screen, for example, you can use the following program:

NOMAINWIN

UpperLeftX = 139

UpperLeftY = 225

OPEN “Titlebar” FOR Window AS #1

PRINT #1, “trapclose [quit]”

WAIT

[quit]

CONFIRM “Are you sure you want to quit?”; quit$ IF quit$ = “no” THEN WAIT

CLOSE #1 END

The UpperLeftX and UpperLeftY commands define how far the window appears from the left and top edges, respectively. If you want to define the exact size of your window, you can use the WindowHeight and WindowWidth commands to define a size measured in pixels, as follows:

NOMAINWIN

UpperLeftX = 139

UpperLeftY = 225

WindowWidth = 550

WindowHeight = 275

OPEN “Titlebar” FOR Window AS #1

PRINT #1, “trapclose [quit]”

WAIT

[quit]

CONFIRM “Are you sure you want to quit?”; quit$ IF quit$ = “no” THEN WAIT

CLOSE #1 END

194 Part III: Advanced Programming with Liberty BASIC

Title bar

Figure 14-1:

After you create a window, you can define text to appear in that window’s title bar, location on-screen, and the

window size.

You must define the size and location of a window before using the OPEN command that actually creates the window on-screen.

Adding color to a window

Liberty BASIC also enables you to choose the background color for your window by using the BackgroundColor$ command and to set its value to one of the following colors:

White

Darkblue

Black

Red

Lightgray (or Palegray)

Darkred

Darkgray

Pink

Yellow

Darkpink

Brown

Green

Blue

Darkgreen

Cyan

Darkcyan

Chapter 14: Creating a User Interface 195

If you want to create a window with a pink background, you can use the following commands:

NOMAINWIN

UpperLeftX = 139

UpperLeftY = 225

WindowWidth = 550

WindowHeight = 275

BackgroundColor$ = “Pink”

OPEN “Titlebar” FOR Window AS #1

PRINT #1, “trapclose [quit]”

WAIT

[quit]

CONFIRM “Are you sure you want to quit?”; quit$ IF quit$ = “no” THEN WAIT

CLOSE #1 END

Putting Pull-Down Menus in a Window

Most windows provide pull-down menus so that users can pick a command. To create a pull-down menu, you need to use the MENU command to define the following:

Menu title (typical menu titles are File, Edit, and Help)

Menu commands that appear on the menu, under the menu title (such as Edit, Print, or Cut)

A label to tell your program which instructions to follow after a user chooses a particular menu command

A typical menu command may look as follows:

MENU #WindowHandle, “Menu Title”, “Command1”, [command1]

In this example, #WindowHandle refers to the nickname of the window where you want the menu to appear, Menu Title appears at the top of the window, and Command1 appears after the user clicks the title of the pull-down menu.

To show you a real-life example, take a look at the following program:

NOMAINWIN

MENU #1, “&File”, “&Open”, [asOpen], “&Print”, [asPrint], “E&xit”, [quit]

OPEN “Menu Example” FOR Window AS #1

PRINT #1, “trapclose [quit]”

WAIT

[quit]

196 Part III: Advanced Programming with Liberty BASIC

CONFIRM “Are you sure that you want to quit?”; quit$

IF quit$ = “no” THEN WAIT

CLOSE #1

END

[asOpen]

NOTICE “Open command chosen”

WAIT

[asPrint]

NOTICE “Print command chosen” WAIT

This program works as follows:

1.The first line tells Liberty BASIC not to display the main window.

2.The second line defines a pull-down menu that appears in the window that the file handle #1 designates. The menu title is File and the commands that appear underneath are Open, Print, and Exit.

If you want a letter in a menu title or command to appear underlined, put an ampersand character (&) in front of that letter, such as &Print. Any underlined letter acts as a shortcut or accelerator key so that the user can choose that menu title by pressing Alt plus the underlined letter, such as Alt+F for the File menu. After selecting a menu, the user can then type the underlined letter in a command, such as X, to choose that menu command — for example, Exit.

3.The third line tells the computer, “Create a window with the text Menu Example in the title bar and identify this window by its file handle of #1.”

4.The fourth line tells the computer to follow the instructions following the [quit] label if the user tries to close the program.

5.The fifth line tells the computer to wait for the user to choose a command.

6.The sixth line is the [quit] label.

7.The seventh line displays a Confirm dialog box that asks, “Are you sure that you want to quit?” If the user clicks the Yes button, the program stores a value of “yes” in the quit$ variable. If the user clicks the No button, it stores a value of “no” in the quit$ variable.

8.The eighth line checks to see whether the value of the quit$ variable equals “no”. If so, it runs the WAIT command to wait for the user to do something.

9.The ninth line closes the window that the nickname #1 identifies. This line runs only if the user clicks the Yes button in the Confirm dialog box that the seventh line displays.

Chapter 14: Creating a User Interface 197

10.The tenth line marks the end of the program.

11.The eleventh line identifies the label [asOpen]. After the user chooses the Open command from the File menu, the computer jumps to this line to look for further instructions to follow.

12.The twelfth and thirteenth lines tell the computer, “Display a Notice dialog box with the message, Open command chosen. Then wait for the user to do something else.”

13.The fourteenth line identifies the label [asPrint]. After the user chooses the Print command from the File menu, the computer jumps to this line to look for further instructions to follow.

14.The fifteenth and sixteenth lines tell the computer, “Display a Notice dialog box with the message, Print command chosen. Then wait for the user to do something else.”

To define additional menus for a window, as shown in Figure 14-2, just use multiple MENU commands, as in the following example:

NOMAINWIN

MENU #1, “&File”, “&Open”, [asOpen], “&Print”, [asPrint], “E&xit”, [quit]

MENU #1, “&Help”, “&Contents”, [asContents], “&About”, [asAbout]

OPEN “Menu Example” FOR Window AS #1 PRINT #1, “trapclose [quit]”

WAIT

[quit]

CONFIRM “Are you sure that you want to quit?”; quit$ IF quit$ = “no” THEN WAIT

CLOSE #1 END

[asOpen]

NOTICE “Open command chosen” WAIT

[asPrint]

NOTICE “Print command chosen” WAIT

[asContents]

NOTICE “Contents command chosen” WAIT

[asAbout]

NOTICE “About command chosen” WAIT

198 Part III: Advanced Programming with Liberty BASIC

Each time that you create additional menu commands, you need to create instructions (which a [label] identifies) for the computer to follow whenever the user chooses those commands.

Figure 14-2:

Liberty

BASIC can create pull-down menus.

Making Pop-Up Menus

In addition to pull-down menus, many programs also offer pop-up menus that appear next to the mouse pointer when you right-click the mouse. To create a pop-up menu in Liberty BASIC, you need to use the POPUPMENU command, as follows:

POPUPMENU “command1”, [label1], “command2”, [label2]

To make the POPUPMENU easier to understand, place each command and label on a separate line and use the underscore character (_) at the end of each line. The underscore character tells Liberty BASIC that additional instructions appear on the following line. You can use the underscore character to divide long lines of commands into shorter, separate lines, such as you see in the following example:

POPUPMENU _ “command1”, [label1], _

“command2”, [label2], _ “command3”, [label3]

Chapter 14: Creating a User Interface 199

Here’s what the preceding commands do:

1.The POPUPMENU command tells the computer, “Create a pop-up menu next to the current position of the mouse pointer.”

2.The “command1” and “command2“ text are the actual commands that appear on the pop-up menu. You can add additional commands so that your pop-up menus can display 5, 7, or 15 different commands.

3.The [label1] and [label2] labels identify instructions for the computer to follow if the user chooses a particular menu command. So if the user chose “command1”, the computer immediately follows the instructions that the [label1] label identifies.

If you want to add a horizontal line to separate commands on your pop-up menu, just use the vertical line character (|) in place of a command and omit any label following the | character, as shown in the example below.

POPUPMENU _ “command1”, [label1], _

|, _

“command3”, [label3]

To see how pop-up menus work, try the following program, as shown in Figure 14-3. Notice how the vertical line character creates a horizontal line to separate commands on the pop-up menu.

NOMAINWIN

POPUPMENU _

“Destroy the planet”, [one], _ “Poison the environment”, [two], _ | ,_

“Get elected to political office”, [three]

NOTICE “Nothing selected.”

END

[one]

NOTICE “Launching missiles now.” END

[two]

NOTICE “Spilling oil into the oceans.” END

[three]

NOTICE “Fooled the public once more.” END