Installing new DLL Wizards

Technically, new Wizards come in two different forms. Wizards may be part of components or packages, and in this case are installed the same way you install a component or a package. Other Wizards are distributed as stand-alone DLLs. In this case you should add the name of the DLL in the Windows Registry under the key Software Borland Delphi x.0 Experts. Simply add a new string key under this, choose a name you like (it doesn't really matter) and use as text the path and filename of the Wizard...

Owner Draw Menu Items

In Windows, the system is usually responsible for painting buttons, list boxes, edit boxes, menu items, and similar elements. Basically these controls know how to paint themselves. As an alternative, however, the system allows the owner of these controls, generally a form, to paint them. This technique, available for buttons, list boxes, combo boxes, and menu items, is called owner-draw. Actually in Delphi the situation is slightly more complex. The components can take care of painting...

Handling PopUp Menus Manually

In the Locall example, we saw how to use automatic pop-up menus. As an alternative, you can set the AutoPopup property to False or not connect the pop-up menu to any component, and use the pop-up menu's Popup method to display it on the screen. This procedure requires two parameters the x and y values of the position where the menu is going to be displayed. The problem is that you need to supply the screen coordinates of the point, not the client coordinates, which are the usual coordinates...

Adding New Form Templates to the Object Repository

Just as you can add new project templates to the Object Repository, you can also add new form templates. Simply move to the form you want to add, right-click on it, and select Add to Repository from the local menu. In the dialog box that appears (see below), you can choose which form of the current project should be added to the Repository, and set the title, description, author, page, and icon, as usual. Once you have set these elements and clicked on OK, the form is added to the proper page...

The Empty Project Template

When you start a new project, it automatically opens a blank form, too. If you want to base a new project on one of the form objects or wizards, this is not what you want. To solve this problem, you can add an Empty Project template to the Gallery. The steps required to accomplish this are simple 2 Remove its only form from the project. 3 Add this project to the templates, naming it Empty Project. When you select this project from the Object Repository, you gain two advantages. You have your...

The Phrasesl Example

If you've ever tried to learn a foreign language, you probably spent some time repeating the same silly and useless phrases over and over. Probably the most typical, when you learn English, is the infamous The book is on the table. To demonstrate radio buttons, the Phrasesl example creates a tool to build such phrases by choosing among different available options. This form is quite complex. If you rebuild it, remember that you must place the GroupBox components first and the radio buttons...

Working with the Form Designer

Designing forms is the core of visual development in the Delphi environment. Every component you place on a form and every property you set is stored in a file describing the form (a DFM file) and has some effect on the source code associated with the form (the PAS file). When you start a new, blank project, Delphi creates an empty form, and you can start working with it. You can also start with an existing form (using the various templates available), or add new forms to a project. A project...

Adding New Application Templates

Adding a new template to Delphi's Object Repository is as simple as using an existing template to build an application. When you have a working application you want to use as a starting point for further development of two or more similar programs, you can save the current status to a template, ready to use later on. Although Borland calls everything you can put in the Object Repository an object, from an object-oriented perspective this is far from true. For this reason I call the schemes you...

The Project File

In addition to the two files describing the form (PAS and DFM), a third file is vital for rebuilding the application. This is the Delphi project file (DPR). This file is built automatically, and you seldom need to change it, particularly for small programs. If you do need to change the behavior of a project, there are basically two ways to do so You can use the Delphi Project Manager and set some project options, or you can manually edit the project file directly. C Program Files Borland...

The View Menu

The View pull-down menu combines the features you usually find in View and Window menus. There is no Window menu, because the Delphi environment is not an MDI application. Most of the View commands can be used to display one of the windows of the Delphi environment, such as Project Manager, the Breakpoints list, or the Components command. Some of these windows are used during debugging others when you are writing code. Most of these windows will be described later in this chapter. It is...

Setting Project Options

From the Project Manager (or from the Project menu), you can invoke the Project Options dialog. The first page of Project Options, named Forms, lists the forms that should be created automatically at program startup (the default behavior) and the forms that are created manually by the program. You can easily move a form from one list to the other. The next page, Application, is used to set the name of the application and the name of its Help file, and to choose its icon. Other Project Options...

The Project Manager

When a project is loaded, you can choose the View Project Manager command to open a project window. The window lists all of the forms and units that make up the current project. The Project Manager's local menu allows you to perform a number of operations on the project, such as adding new or existing files, removing files, viewing a source code file or a form, and adding the project to the repository. Most of these commands are also available in the toolbar of this window. Progrann Files...

The Files Produced by the System

As you have seen, Delphi produces a number of files for each project, and you should know what they are and how they are named. There are basically two elements that have an impact on how files are named the names you give to a project and its forms, and the predefined file extensions used by Delphi for the files you write and those generated by the system. The great advantage of Delphi over other visual programming environments is that most of the source code files are plain ASCII text files....

Compiling a Project

There are several ways to compile a project. If you run it (by pressing F9 or clicking on the toolbar icon), Delphi will compile it first. When Delphi compiles a project, it compiles only the files that have changed. If you select Compile Build All, instead, every file is compiled, even if it has not changed. This second command is seldom used, since Delphi can usually determine which files have changed and compile them as required. The only exception is when you change some project options. In...

The Object Repository

Delphi has several menu commands you can use to create a new form, a new application, a new data module, a new component, and so on. These commands are located in the File menu, and also in other pull-down menus. What happens if you simply select File New Other from Delphi 6, or File New in earlier versions Delphi opens the Object Repository also called the New dialog box . The Object Repository is used to create new elements of any kind forms, applications, data modules, libraries, thread...

The Database Form Wizard

In this section, I'll show you a quick example of the use of the Database Form Wizard, but I won't describe the application we build in detail. In this example, we'll build a database program using some of the data already available in Delphi. Note that you have to create a project first, and then start the Database Form Wizard. So you usually end up with two forms, unless you remove the original form from the project. Fortunately, one of the Wizard's options, displayed at the end, lets you...

The Application Wizard

Another interesting although less powerful tool is the Application Wizard. You can activate it from the Projects page of the Object Repository. The Application Wizard allows you to create the skeleton of a number of different kinds of applications, depending on the options you select. Your application can contain a menu bar by checking one or more of the following standard Windows menus The file menu contains items such as Open, Save and Exit. I- Edit menu The Edit menu contains Undo, Cut, Copy...

Responding to Events

When you press the mouse button on a form or a component, Windows informs your application of the event by sending it a message. Delphi responds by receiving an event notification and calling the appropriate event-handler method. As a programmer, you can provide several of these methods, both for the form itself and for the components you have placed in it. Delphi defines a number of events for each kind of component. The list of events for a form is different from the list for a button, as you...

The Object Repository Options

To further customize the Repository, you can use the Tools Repository command to open the Object Repository dialog box. This dialog box is quite easy to use on the left is the list of current Repository pages and on the right the list of items in each page, including both templates and Wizards. You can use this dialog to move repository items to different pages, to add new elements, or to delete existing ones. You can use the three page-related buttons and the two buttons with arrows below the...

The Project Menu

The next pull-down menu, Project, has commands to manage a project and compile it. Add to Project and Remove from Project are used to add forms or Pascal source code files to a program and to remove them from a project. The Enterprise version of Delphi includes two more commands, Web Deploy and Web Deploy, which are not available in the other editions. These features related with ActiveX and ActiveForms, some now obsolete Microsoft web technologies. The Compile command builds or updates the...

Removing a Selected String from the Other List

Once this part of the program works, we have two more problems to solve We must remove the selected string from the other list box to avoid using the same term twice in a phrase , and we must write the code for the click event on the button. The first problem is more complex, but I'll address it immediately since the solution of the second problem will be based partially on the code we write for the first one. Our aim is to delete from a list box the item currently selected in the other list...

The Dialog Wizard

Delphi's Dialog Wizard is a simple Wizard provided mostly as a demo, with its own source code. From the code of this Wizard, in theory you should be able to learn how to build other Wizards of your own. However, you can still use the Dialog Wizard as a tool to build two kinds of dialog boxes simple dialog boxes and multiple-page dialog boxes based on the Windows PageControl component. If you choose the simple dialog box, the Wizard will jump to the third page, where you can choose the button...

Handling the Input Focus

What's important for our example is that each time a component receives or loses the input focus, it receives a corresponding event indicating that the user either has reached OnEnter or has left OnExit the component. So we can add some methods to the form to take control over the input focus and display this information in a label or a status bar. Besides three edit boxes, the form has also some labels indicating the meaning of the three edit fields First name, Last name, and Password . For...

Delphi Wizards

Besides copying or using existing code, Delphi allows you to create a new form, application, or other code files, using a Wizard. Wizards or Experts allow you to enter a number of options and produce the code corresponding to your choices. One of the most important predefined wizards is the Database Form Wizard, which you can activate using the Database Form Wizard menu item or the icon in the Forms page of the Object Repository. There are also some other simple Wizards in Delphi, such as the...

Working with a CD Drive

In addition to audio and video files, the MCI interface is generally used to operate external devices. There are many examples, but the most common MCI device connected to a PC is probably a CD-ROM drive. Most CD-ROM drives can also read audio CDs, sending the output to an external speaker or a sound card. You can use the MCI interface and the Media Player component to write applications that handle such a device. Basically, you need to set the DeviceType property to dtCDAudio, making sure no...

The Code Generated by the Menu Designer

Once you have built this menu, take a look at the list of components displayed by the Object Inspector, or open the DFM file with the textual description of the form, which will also contain a textual description of the menu structure. Here is the portion of the textual description of the form related to the menu and its items object MainMenul TMainMenu object Filel TMenuItem Caption ' amp File' object Exitl TMenuItem Caption 'E amp xit' OnClick ExitlClick object Viewl TMenuItem Caption ' amp...

The Search Menu

The Search menu has some standard commands, too, such as Search and Replace, and the Find in Files command you can see its dialog box here The Find in Files command allows you to search for a string in all of the source code files of a project, all the open files, or all the files in a directory optionally including its subdirectories , depending on the radio button you check. The result of the search will be displayed in the message area at the bottom of the editor window. You can select an...

The LabelCo Example

The first thing to do is to place a big label in the form and enter some text. Write something long. I suggest you set the WordWrap property to True, to have several lines of text, and the AutoSize property to False, to allow the label to be resized freely. It might also be a good idea to select a large font, to choose a color for the font, and to select a color for the label itself. Push the buttons below to change the color of the text or the background of this Ijffiel, and its alignment To...

The File Menu

Our starting point is the File pull-down menu. The structure of this menu has kept changing from version to version of Delphi, with menu items for handling projects moving away, and specific commands to create new designer for example data modules coming and going. Still, this menu contains commands that operate on projects and commands that operate on source code files. The File menu structure has changed in Delphi 6, with the File New submenu, and the text here has not been updated...

Creating a Rich Editor

Although you can choose a font in the Notes program, all of the text you have written will have the same font. Windows has a control that can handle the Rich Text Format RTF . A Delphi component, RichEdit, encapsulates the behavior of this standard control. You can find an example of a complete editor based on the RichEdit component among the examples that ship with Delphi. The example is named RichEdit, too . Here, we'll only change the previous program slightly by replacing the Memo component...

The Tools Menu

The Tools menu simply lists a number of external programs and tools, just to make it easier to run them. You can use the Tools command to configure and add new external tools to the pull-down. Besides simply running a program, you can pass some parameters to it. Simple parameter lists can be passed directly on the command line, while complex ones can be built by clicking the Macros button in the lower part of the Tool Properties dialog box. The Tools menu also includes a command to configure...

The Textual Description of the Form

As I've just mentioned, along with the PAS file containing the source code, there is another file describing the form, its properties, its components, and the properties of the components. This is the DFM file, a binary or text file this latter option has been introduced with Delphi 5 . Whatever the format, if you load this file in the Delphi code editor, it will be converted into a textual description. This might give the false impression that the DFM file is indeed a text file, but this can...

Using a Check ListBox Component

Checklistbox Delphi Example

A further extension to the Phrases example is the use of the CheckListBox component, a component originally introduced by Borland in Delphi 3. This is basically a list box with a custom output or an owner-draw list box, to use the proper technical term . Each item of the list is preceded by a check box. A user can select a single item of the list, but can also click on the check boxes to toggle their status. If the component has the AllowGrayed property set to True, then each check box can be...

Bitmap Menu Items

Instead of placing a bitmap close to a menu item to indicate the status of its Checked property, as we've done in the previous section, you can actually replace the text of a menu item with a bitmap. In specific cases this can make an application easier to use. BitMenu is a very simple program. I've put a shape in the middle of a form and added a menu to set the kind of shape rectangle, rounded rectangle, or ellipse , and its color. Here is the textual description of the form and its menu...

Using Radio Menu Items

In addition to using check marks, in Windows you can use radio menu items. These provide not only a different user interface, but also different behavior basically simpler code, since the system does some of the work for us . A notable example of this new user-interface feature is the View menu of the Windows Explorer. In Delphi, simply set the RadioItem property of a MenuItem component to True and you get the new check mark for the item. If you set this property for several consecutive menu...

Shortcut Keys and Hotkeys

A common feature of menu items is that they contain an underlined letter, generally called a hotkey. This letter, which is often the first letter of the text, can be used to select the menu using the keyboard. Pressing Alt plus the underlined key selects the corresponding pull-down menu. By pressing another underlined key on that menu, you issue a command. Of course, each element of the menu bar must have a different underlined character. The same is true for the menu items on a specific...

Using Components

Now it's time to start placing something useful in our Hello form. Forms can be thought of as component containers. Each form can host a number of components or controls. You can choose a component from the Components Palette above the form, in the Delphi window. There are four simple ways to place a component on a form. If you choose the Button component from the Standard page of the Components Palette, for example, you can do any of the following Click on the component, move the mouse cursor...

Customizing the Menu Check Mark

As I've just mentioned, there are a number of ways to customize a menu in Windows. In this section, I'm going to show you how you can customize the check mark used by a menu item, using two bitmaps of your own. This example, NewCheck, involves using bitmaps and calling a Windows API function. First I should explain why we need two bitmaps, not just one. If you look at a menu item, it can have either a check mark or nothing. In general, however, Windows uses two different bitmaps for the checked...

Every Box Has a Beep

To show you the capabilities of the MessageBeep API function, I've prepared a simple example, Beeps. The form of this example has a RadioGroup with some radio buttons from which the user can choose one of the five valid constants of the MessageBeep function. Here is the definition of the RadioGroup, from the textual description of the form object RadioGroupl TRadioGroup Caption 'Parameters' ItemIndex 0 Items.Strings 'mb_IconAsterisk' 'mb_IconExclamation' 'mb_IconHand' 'mb_IconQuestion' 'mb_Ok'...

The Delphi Toolbar

After you have used Delphi for a while, you'll realize that you use only a small subset of the available commands frequently. Some of these commands are probably already on the toolbar Borland's name for a toolbar some are not. If the commands you use a lot are not there, it's time to customize the toolbar so that it really helps you to use Delphi more efficiently. An alternative to using the toolbar is to use shortcut keys. Although you must remember some key combinations to use them, shortcut...

Sophisticated Input Schemes

In the last example, we saw how an Edit component can be customized for special input purposes. The components could really accept only numbers, but handling complex input schemes with a similar approach is not straightforward. For this reason, Borland has supplied a ready-to-use masked edit component, an edit component with an input mask stored in a string. For example, to handle numbers of no more than five digits, we can set the EditMask property to 99999. The character 9 stands for...

The Font Dialog

The second portion of the program involves the font. In the same way we used the standard Color dialog box in a previous example, we can use the standard Font selection dialog box provided by Windows. Just move to the Dialogs page of the Components palette and select the FontDialog component. Place it anywhere on the form, and add the following code when the user double-clicks inside the Memo procedure TNotesForm.Memo1DblClick Sender TObject begin FontDialogl.Font Memol.Font if...

Short Circuit Evaluation

The expression if not Modified or SaveChanges then requires some explanation. By default, Pascal performs what is called short-circuit evaluation of complex conditional expressions. The idea is simple if the expression not Modified is true, we are sure that the whole expression is going to be true, and we don't need to evaluate the second expression. In this particular case, the second expression is a function call, and the function is called only if Modified is True. This behavior of or and...

Donations

I'll probably set up an account on one of those donation contribution systems, to let people who have enjoyed the book and learned from it, particularly if programming is their job and not a hobby and they do it for profit, contribute to its development. No extra material is offered to those donating to the book fund, only because I want to let anyone particularly students and people leaving in poor countries benefit from the availability of this material. Information will be available on the...

Entering Numbers

We saw in the previous example that it is very easy to use an Edit component to ask the user to input some text, although it must be limited to a single line. In general, it's quite common to ask users for numeric input, too. To accomplish this, you can use the MaskEdit component in the Additional page of the Components palette or simply use an Edit component and then convert the input string into an integer, using the standard Pascal Val procedure or the Delphi IntToStr function. This sounds...

The Scroll Color Example

The ScrollC example the name stands for scroll color has a simple form with three scroll bars and three corresponding labels, a track bar with its own label, and some shape components to show the current color. Each scroll bar refers to one of the three fundamental colors, which in Windows are red, green, and blue RGB . Each label displays the name of the corresponding color and the current value. Scroll bars have a number of peculiar properties. You can use Min and Max to determine the range...

The Edit Menu

The Edit menu has some typical operations, such as Undo and Redo, and the Cut, Copy, and Paste commands, plus some specific commands for form or editor windows. The important thing to notice is that the standard features of the Edit menu and the standard Ctrl Z, Ctrl X, Ctrl C, and Ctrl V keyboard shortcuts work both with text and with form components. There are also some differences worth noting. For example, when you work with the editor, the first command of this pull-down menu is Undo when...