Xem mẫu

II Graphical User Interfaces In this next section I’m going to talk about Graphical User Interfaces and how they are created in PureBasic. Nearly all modern operating systems have a built-in graphical user interface available, allowing the user to interact fluidly with programs that choose to use it. The operating system exposes the user interface to programs through an Application Programming Interface (or API) through which a program can tell the operating system how draw its user interface. This sounds extremely complicated but is simply and elegantly handled within PureBasic using the ‘Window’, ‘Menu’ and ‘Gadget’ libraries. PureBasic creates these graphical interfaces for your programs using the native application programming interface of the system they`ve been compiled for. In other words, when you code an interface for a program and compile it on a particular system, that program will have the correct look and feel of the operating system it has been compiled for. This is essential for all professional application development. This section begins with explaining and demonstrating programs that use a console as their user interface, which is arguably the simplest user interface of all. Later, I move on to explain how to create programs with native user interfaces and how to add menus and graphics. In the last section I give you and overview of the PureBasic Visual Form Designer. Using this tool you can design an interface visually as if you were painting it upon a program. After reading this section you should have a firm grasp on how to create graphical user interfaces for your programs and understand how to handle interaction from the user. 131 Creating User Interfaces In this chapter I’ll explain how to create graphical user interfaces for your programs. PureBasic makes this task very easy by distilling complex application programming interfaces into simple, easy to learn commands. I explain fully how to code the graphical interface, complete with menus and sometimes with graphics. I also cover how to handle events from your program’s interface, such as, when the user presses a button or selects a menu item. Hopefully after reading this chapter you will be well equipped to create user interfaces for any program you decide to develop. Console Programs To begin with, we shouldn’t run before we can walk, so first I’ll introduce to you what is known as a Console Program. Console programs, as the name suggests, are programs that use a console as their user interface. A console is a text based interface that can accept typed input and display output using text characters. On some operating systems the console can display simple graphics by substituting some of the ASCII character set for graphical symbols instead of characters. Console interfaces are usually used in programs where a fully blown user interface is not needed. These types of programs are usually command line tools that are run from other consoles or things like CGI programs that run in the background on web servers, etc. Basically, a console is used to display basic output information from the program and to accept basic text input from the user. Commands that create and work with a console interface are grouped inside the ‘Console’ library (Helpfile:Reference Manual->General Libraries->Console). This library offers PureBasic programmers various commands to print text, accept user input, clear the console and even change its colors. Here is an example of how to create a console program in PureBasic: If OpenConsole() Print("This is a test console program, press return to exit...") Input() CloseConsole() EndIf End 132 Creating User Interfaces In this example I’ve used the ‘OpenConsole()’ and ‘CloseConsole()’ commands to open and close the actual console window, these are self explanatory. The second command I’ve used is the ‘Print()’ command which accepts one String parameter that is printed to the console interface. This command is almost identical to the other console library command ‘PrintN()’. The ‘PrintN()’ will also print a line of text but it will append an end-of-line character on the end to move to a new line after the text has been printed. This is very similar behavior to the file writing commands ‘WriteString()’ and ‘WriteStringN()’ as mentioned in Chapter 7 (Handling Files). The last command used in the above example is ‘Input()’. This command halts the program’s execution until the Return key is pressed on the keyboard. This command then returns any characters (as a String) that were entered into the console before the Return key was finally pressed. Because in my example, I’m not dealing with any return value from this command, it’s used here purely to keep the console open so people can read the text I’ve printed to it. If this command was omitted, then the console would almost immediately close as soon as it had opened. Using ‘Input()’ like this, can provide a simple way to keep the console open while we read what is printed there, as long as we inform the user that to continue the program execution he or she must press Return. Reading User Input Sometimes in your console programs you may want to read user input, this could be a simple number or a String of text. Although it is pretty straightforward to gather any user input you must always remember that the ‘Input()’ command only returns Strings. This next piece of code shows this, while introducing some new console commands: If OpenConsole() EnableGraphicalConsole(#True) Repeat ConsoleColor(10, 0) PrintN("TIMES TABLES GENERATOR") PrintN("") ConsoleColor(7, 0) PrintN("Please enter a number, then press Return...") PrintN("") Number.q = ValQ(Input()) If Number = 0 ClearConsole() Continue Else Break EndIf ForEver PrintN("") Creating User Interfaces 133 For x.l = 1 To 10 PrintN(Str(x) + " x " + StrQ(Number) + " = " + StrQ(x * Number)) Next x PrintN("") Print("Press Return to exit...") Input() CloseConsole() EndIf End This example looks rather complicated but it’s not really if you read it a line at a time. The first new command I’ve used here is the ‘EnableGraphicalConsole()’ command. This enables or disables the graphical capabilities of the console program by passing either the ‘#True’ or ‘#False’ constants as a parameter. Because we later use ‘ClearConsole()’ which only works with a graphical console, we set ‘EnableGraphicalConsole()’ to true. Differences Between Graphical And Non Graphical Console Modes Using the ‘EnableGraphicalConsole()’ command you can switch between text mode and graphical mode of the current console. Here are the differences for each mode: Text Mode (Default): ASCII control characters work correctly (ASCII range ‘0’ to ‘31’). Output redirection using Pipes works correctly (Essential for CGI programs). Long Strings of printed text wrap onto a new line if they reach the end of the console window. You can read and write data to the console that is not necessarily text based. Graphical Mode (Changed using ‘EnableGraphicalConsole(#True)’): Text characters outside the ACSII range ‘33’ to ‘126’ are displayed as small simple graphics. Long Strings of printed text are truncated if they reach the end of the console window. ‘ClearConsole()’ clears the entire console of any output. ‘ConsoleLocate()’ Moves the cursor to be able to print text in any position in the console window. This list contains some advanced topics you might not be familiar with right now but I have included this list here to be used a future reference when you are more comfortable with these things. I’ve also changed the console text color in some places by using the ‘ConsoleColor()’ command. The first parameter is the text color and the second is the text background color. The parameters are numbers that range from ‘0’ to ‘15’ which represent different color presets, see the helpfile to see what color is associated with each number (Helpfile:Reference Manual->General Libraries->Console->ConsoleColor). ... - tailieumienphi.vn
nguon tai.lieu . vn