 A Delphi Tutorial                                     A WindoWatch Feature


                   Building a Small Delphi Application

                      copyright 1995 by Herb Chong

 Borland's Delphi is one of the Windows programming tools being marketed
 as a Rapid Application Development (RAD) toolkit. You are supposed to be
 able to build applications faster with RAD systems than with traditional
 development tools like C and C++,  even with their spiffy new interfaces
 and IDEs.  What distinguishes a RAD tool from other quick, easy to use
 tool building environments like Visual Basic is that a RAD tool has much
 more power built into it from the beginning.  However, it's not enough to
 just be able to put together applications quickly. You have to be able to
 build applications that can solve tough, real-world problems quickly and
 efficiently. Visual Basic is great for prototyping look and feel, but as
 soon as you start to use it on larger projects, its limitations become
 painfully obvious.

 Code reuse is difficult, and speed just isn't there unless you write lots
 of DLLs or VBXs, defeating the whole point of going with Visual Basic in
 the first place. Visual Basic 4.0 promises to help with some of the group
 programming problems, but fundamentally, Visual Basic is not an object
 oriented programming language and that means you have to use components
 that you can't modify or derive from.

 In this tutorial, we're going to build a small Delphi application. It
 wont do much, mostly because if you need to work through tutorials,
 youre not ready to do much yet. The program is going to copy files from
 one place to another. That doesn't sound very hard, and it isnt. Writing
 this article took about four times as long as it did to write the
 program. But thats not the point. The point is to get you started with a
 small program that is just a little beyond the introductory tutorials in
 the Delphi manuals and into areas they don't cover.

 Getting Started

 Like all GUI tool building, when you program in Delphi, you need to think
 about what kind of interface you want for your program. Once you have
 your user interface worked out, much of the rest of the programming falls
 into place. It doesn't make the details any easier, but at least you have
 a better idea of where to start. Object oriented programming in general
 requires that you think much harder about how your application needs to
 work before you can begin coding.

 The interface I chose for this tutorial program is simple. The user sees
 either list and copy from one list to another. Each file list can be from
 files in the same directory or in different directories. For simplicity
 of programming, they can select only one file at a time. Figure 1 shows a
 screen shot of the finished program. (Note: all the screen shots for this
 article were done on Windows 95.) Well spend most of the time building
 this user interface appearance before actually writing any code. The
 finished program is only 129 lines of Object Pascal code, and most of it
 is generated by Delphi itself. With Delphi, you spend more time thinking
 about the problem than writing code.

 To get started, you need to create a directory for your new project. I
 like to keep all the work I do in separate directories under the \DELPHI
 subdirectory. I have one called \DELPHI\PER-SONAL and within it, I keep
 all my projects. So, create a directory where you want to keep this
 tutorial program and launch Delphi. If you have used Delphi before,
 youll come up with the last project you loaded. Since we are beginning a
 new project, click on File|New Project. It shows the Object Inspector, which
 lets you set object properties, and the main form for the program. Underneath
 it, just barely visible, is the Object Pascal code for the form. Now, save
 the project using File|Save Project As. Specify the name you want use for
 the first forms files. If you want to, give the project file a name too,
 like "tutorial.dpr".

 Before we do anything else, run the program to see what a completely
 empty form looks like. Press the F9 key, or use Run|Run to compile the
 code and launch the program and you get a completely empty default form.

 Although it is hard to tell because of the default color scheme in
 Windows 95, there are several undesirable characteristics of this form.
 The first is that there is a maximize button. This sample application
 doesnt resize its controls, so allowing the user to click on maximize is
 not a good idea. The other thing is that resizing borders are on the form
 too. They also allow the user to change the size of the form window. We
 need to get rid of both. You do this changing the form's properties using
 Object Inspector.

 The top drop-down listbox shows which component you have selected. In
 this case, it's Form1 and it is of type Tform1. We need to change the
 border icons and the border style flags. If you click on BorderStyle,
 youll see that there is a drop-down listbox of possible settings. Change
 the BorderStyle property to bsSingle. The   +  character next to the
 BorderIcons property signals that there are several settings grouped
 together under this property. If you double-click on the property name,
 you will see that there are three sub-properties.

 The system menu is the icon shape on the left of the title-bar (this is
 new in Windows 95). The minimize and maximize buttons are on the right,
 next to the close button new in Windows 95. Its OK to minimize this
 application, but not to maximize it.  Click on the biMaximize property
 and change its value to False. The remaining thing to do is to change the
 form Caption property.

 This is the text that will appear in the title bar of your form. Once you
 do this, run the program again and see what it looks like.

 Well, there you have it, a complete Delphi application that does nearly
 nothing. You havent entered or changed any code, and you have a full
 fledged application. What you have done is change properties for a form
 so that it runs the way we need this application to run. It prevents the
 user from doing things that it's not prepared to handle. The next step is
 to put the controls onto the form. Getting the Visual Appearance Right

 Once the basic form properties have been set, its time to draw the
 controls on the form itself. No code need be attached at first. The basic
 visual appearance needs to be right first. To add controls to a form, you
 need to use the Delphi main Menu bar. The three groups of buttons on the
 left allow you to manage Delphi files, projects, and debugging using the
 integrated debugger. In the middle and right portions of the window are
 the Visual Component Library components, arranged into groups by tabs. The
 controls we need at first are from the Standard tab, which is shown high-
 lighted in gray. If you hold your mouse pointer over anyone of the icons
 in the tabbed bar, you will see the name of the component.

 Find and click on the Panel component. The icon indents. This tells you
 that when you drag the mouse cursor on the form, you will be creating a
 panel where you drag. Make sure the size is approximately what is shown
 in the screen shot as we need to fit some buttons and another one of these
 later.

 Although this is a fine looking panel, it's not got the look we want.
 Click once on the panel. The Object Inspector now changes to show the
 properties for the panel. The panel is colored such that it appears to be
 popped out from the form. This isnt what we want. Instead, we want the
 panel border to look like an indentation into the form with the surface
 of the panel the same height as the rest of the form itself. To make
 these changes you need to change the BevelInner and BevelOuter
 properties. BevelInner needs to be bvRaised and BevelOuter needs to be
 bvLowered.

 In our application, each part of the file selector has a way to select
 the drive, directory, and file that the user wants to copy. We need to
 put these controls into the panel we just created. These components are
 located under the System tab.

 We will need the DriveComboBox, DirectoryListBox, and FileListBox
 components. They also have to be grouped together with the panel that we
 just created. Make sure that the panel is selected. Then select each type
 of component and create them inside the panel.

 Select the panel and drag it left and right to make sure that the other
 three components are enclosed properly by the panel control. If any are
 not, moving the panel will leave them behind. If this happens, cut the
 component using Edit|Cut, select the panel, and then Edit|Paste it. This
 makes sure that the component is contained within the panel. Make sure to
 size the FileListBox component slightly taller than half way so that the
 panel's caption is hidden. The other thing to do is to change the panels
 caption to an empty string.

 Once you have all the components arranged properly, select the panel and
 do Edit|Copy. Then click on the form so that it is selected, and the do
 Edit|Paste. Youll end up with another panel and set of controls pasted
 on top of your other panel and controls. See Figure 12. Click on the
 group of controls and drag it into position on the right hand side of the
 form.

 Click on the drop down listbox in Object Inspector and scroll thru the
 list. You should see two of everything except the form itself in the
 list. These are all the components in your project. Their component names
 are based on the type of component they are. For a project of this size,
 it makes little difference what you name your components because there
 are so few of them. For a larger project, you will give them more
 descriptive names that identify the purpose of the control better. For
 this project, we will leave the control names alone. After we add four
 buttons we are done with the visual appearance of the program. But-tons
 are located on the Standard tab of the Visual Component Library tab bar.
 Select it and add four buttons from top to bottom, arranged as in Figure 1.
 The easiest thing to do is to create one button of the right size and clone
 it three times by copying the first and pasting it three times.

 The button captions arent right, so youll need to edit them. The top
 button is named About..., the next one is Copy >>, the next Copy <<,
 and the final one Exit!. Make these changes and save your work.
 Click on File|Save Project and make sure that the project is saved into
 your working directory. Then press F9 and run your program. If you did
 everything as you should have, the application should pop up with a
 window.


 Experiment with the program as it's running. You'll see that you can
 click on all the buttons and controls and some things happen. What
 happens isnt what needs to happen yet, but you can scroll the listboxes,
 select things, and see buttons go down when you click on them. The visual
 interface is done. The only visual aspect left to do is to not highlight
 the Copy buttons unless there is a file selected in the corresponding
 FileListBox controls. To do that requires writing Object Pascal Code.
 That's what we will do next.

 Making It Work for Real

 All the visual interface design that you did before was fairly easy and
 frequently represents most of the design time in a complicated
 application. For this application, it probably represents about half the
 time if you are familiar with Delphi. The other half of the time will be
 spent putting in the right code to make the application work. The first
 thing well do is to make the Exit! button work. You can do this by
 double-clicking on the Exit! button. Up comes the code editor window.
 Once it appears, you can edit the code. The code for the Exit! button
 consists of one statement, Close;.

 In the same way, you will add the code for the About button. Bring the
 form design window to the front again and double click on it. Add the
 statement to implement the About dialog. Save the project and run the
 program again. You should be able to click on the About button and see
 the message box. You should also be able to click on the Exit! button to
 exit the program.

 Well! It's getting closer to being something real. The next thing to do
 is to make the drive, directory, and file components work together. If
 you tried them in the version of the program you have now, you will see
 that changing the drive letters or directories did not have any effect on
 each other nor the file listbox. Were going to add some code to make
 them work together. Double click respectively on the left DriveComboBox,
 and DirectoryListBox and add the lines of code to your project.

 Do the same for the DriveComboBox and DirectoryListBox on the right. You
 should be adding the lines of code as shown in Figure 19. Save the
 project and run the program again. This time, when you change drives or
 select a new directory, the file listbox contents should change. Hard to
 believe it, but we are almost done.

 Look at Figure 1 again, where we show the finished application. Notice
 that the Copy buttons are both grayed out. This happens whenever there
 arent any files selected in either file listbox. If a file is selected,
 the appropriate Copy button is supposed to become enabled. If a file is
 selected in each listbox at the same time, both buttons are supposed to
 be enabled. To do this, your application has to both initialize the
 buttons to be both grayed, but also to enable and disable the buttons as
 appropriate.

 First, lets get the button states started properly. To do this, you need
 to modify one of the forms events, the OnCreate event. Select the form
 and bring the Object Inspector window to the front. You will see all the
 forms properties by default. If you click on the Events tab at the
 bottom of the Object Inspector window, you will see the events list.
 Note that no events have event handlers assigned to them. This
 means that every event that the form receives is handled by a default
 event handler.

 Double click on the empty value next to the OnCreate event. You will
 bring up a code editor window with a new method. Insert the lines of code
 shown in Figure 21. Once again, save the project and run it. You should
 see both the Copy buttons disabled.

 So, now we have disabled the buttons at startup. How do we enable them?
 Well, the buttons should be enabled when a file is selected in the
 correct file listbox window. Click on the left file listbox to select it.
 Now, in Object Inspector, double click on the OnClick event for the
 component. Add the code for FileListBox1as and do the same for the right
 file listbox. Save the project and run the program again. The Copy buttons
 should enable themselves when you selected a file in their connected file
 listbox. In Figure 1, each file listbox has the ShowGlyph property set to
 True so that an icon appears next to each file name identifying the file
 type. Can do that at this point in the program. Note that when you make this
 change to the properties for each listbox, the contents change right away
 so that you can see the effect of the change.

 The only thing left to do now is to make the copy buttons actually do
 something. Once again, you will be modifying the default OnClick
 handlers, but for the two Copy buttons. Double click on the event
 handlers list to create and add code shown to the code in the edit window.

 Since the copy operation is the same for both buttons, but the strings
 used to decide which to copy where are different, Ive decided to use a
 simple helper function to do the real copying operation. CopyFile takes
 two string parameters as input, the file to copy and the directory to
 copy it into. Scroll upwards in the code edit window until you get to the
 section of code for private declarations. Add the function forward
 declaration.

 Now you have to add the code to actually do the copying. Now is where we
 do something sneaky. Object Pascal doesnt have a function that copies a
 file from one place to another, and neither does the Windows API,
 strictly speaking. However, there is an obscure function seldom used by
 most people which does do file copying, and has a bonus function, it
 uncompresses files from normal Windows setup disks! Its called LZCopy,
 and you need to add LZExpand to your uses clause in the form.

 Because it is a Windows API function, it cant use Object Pascal strings
 directly. Well have to convert the strings to Windows API null
 terminated strings. Delphi has functions to do this. Add the appropriate
 procedures shown to your code. The code allocates several structures
 and variables needed to interface with the Windows API. It then converts
 the name of the input file and opens it using the LZOpen API call.. Next,
 it builds the output file name and opens it too. Then it calls LZCopy to
 do the real file copying. Finally, it closes the files and exits. Thats
 all there is to it.

 Summary

 Although it took me about an hour to research, write, and debug the
 program, it took more than 4 hours to write this article. Most of it was
 spent getting the screen shots right and really describing all that I
 did. If I was writing this program without the need to prepare for a
 tutorial, it would have taken less than an hour to have a completely
 working implementation. I'm not a Delphi expert by any means. I am a good
 Windows programmer and know what things are supposed to do. I probably
 spent less time designing the program than most people would because I
 have designed many things like this before. Delphi is extremely easy to
 use for people who are familiar with Visual Basic. Its a bigger change
 for people who have been using Borland Pascal or any of the C++
 programming tools. Even, so, with only a few hours experimentation, you
 can become very productive in Delphi.

 For Further Development

 The program we built was a small one. It and this tutorial supplements
 the introductory material included with Delphi. If you want to extend
 this program to make it into a truly useful utility, you need to make the
 window and controls resizable, and perhaps even include multicolumn
 listboxes for the file list display. You can also put in checks for
 copying over the same file name, renaming on conflicts, selecting
 multiple files, and so on. There are dozens of things that could be done
 to enhance it. I'll leave most of them up to you. This small program is
 the beginnings of a file manager/viewer application, much like what ended
 up being called File Manager in Windows.

 Herb Chong has many credits in his vita. It's always a treat to carry his
 work because he understands what is needed to communicate and teach. His
 many contributions include articles in Windows Sources, The Cobb Group's
 Inside Microsoft Windows and of course, WindoWatch. Herb is the WindoWatch
 Contributing Editor.




                                        ww

