Turbo Pascal for Windows: first steps. (Programming Power) (Column)
by Tom Campbell
I think Turbo Pascal for Windows (TPW) is the best Windows development system available for anywhere near its price, especially if you're a Pascal programmer.
Just be sure to budget something extra for CompuServe and a third-party book because the Turbo manuals don't do a great job of turning the new user into a Windows programmer. One way to remedy that situation is to buy Tom Swan's Turbo Pascal for Windows 3, which costs $29.95 from Bantam Books and is more than worth its price. Another way is to read this column.
The only thing I don't like about TPW is the documentation, which is pretty amazing considering it's a 1.0 product. There's a great deal to like about TPW.
FIrst of all, its price makes it simply the best deal you can find for developing Windows applications and DLLs. Make no mistake about it; your hundred clams (and that's list!) for TPW, plus 30 for the Swan book, gets more bang for the buck than anything else out there by a margin of at least $150.
Second, and more important, this set of robust, mature development tools is second to none. The debugger is a text-mode version of Turbo Debugger, not the more modestly endowed debugger that comes with Turbo Pascal 6 for DOS. The resource editor, which lets you create Windows dialogs, menus, and other user-interface gizmos, is Whitewater's well-respected Resource Toolkit, which itself sells for several times more than the entire Turbo Pascal for Windows package. The sample programs, in what is becoming a tradition with Borland, are numerous and of very high quality, illustrating just about every concept a Windows programmer needs to find illustrated.
The ObjectWindows Library is a sheath over Windows to ease the considerable bookwork required to get started in Windows programming, and it does a good job of it. A Help compiler is also included free ($49.95 if you buy it from Microsoft); it lets you create true Microsoft Help for your applications. Last but not least, the multiwindow development environment and editor work together in inimitable Turbo Pascal fashion, which means that TPW is intuitive and very, very fast.
Dialing for Dialogs
This month we'll take those marvelous tools and create . . . well, welll create a program that does very little. It merely displays a dialog with a couple of buttons on it. Normally I scorn do-nothing programs of this type, but I wish I'd had a template like it when I was creating my first real Windows app with Turbo Pascal (it's Windows File Finder, available on COMPUTE/NET).
The TPW example programs are all quite sophisticated--too much so for a laggard like me. What I needed to know was the bare minimum required to put up a modeless dialog with a few buttons on it and how to connect the visual entity that is a dialog with the code it's supposed to execute. You should read the TPW tutorial manual, which will make more sense after you peruse this column.
Following are the general steps. Create a dialog using some sort of resource editor or the mysterious RC (a familiar sight to old Windows hacks). The controls on the dialog must have numeric identifiers, and your program must know what those identifiers are. When the controls are activated (for example, when you click on a button), Windows sends a message to the Turbo Pascal application, which routes it through OWL. You must create a method for that control, giving the method a virtual index containing the value of the control's identifier. This means that every button, scroll bar, edit box, menu item, and so forth can potentially have a method associated with it. It also means that the routines for each button on the dialog can be in widely disparate areas in the source and that there is no intrinsic connection between the code for these controls and the fact that they appear on the same dialog.
This, then, is why Windows programs tend to be modeless and are so much easier to write in a way that keeps modes to a minimum. The usmanipulates a physical object on the screen, and that action triggers code somewhere. Your program consists of little more than subprograms (methods, technically) whose sole purpose is to react to those actions.
The program below uses a simple dialog with two buttons. Clicking on one button causes a message to be displayed. Clicking on the other causes the program to end. While you could simulate this with the Windows Message-Box routine--it is in fact used by this program--you would be limited to the Message-Box's own resources. You can easily extend the concepts used to create this dialog to edit fields, other buttons, whatever. My file finder program illustrates several of these concepts. But let's get down to specific steps.
Fire up the Whitewater Resource Toolkit. Click on the New button and enter DLG as the filename. Click on the Dialog button. The default dialog type is perfect for our purposes. You may want to double-click on the dialog and change its caption for practice; I suggest the text Click on Done to Quit for reasons that will become apparent.
Click on the Button tool and add a button. Double-click on the button to bring up its Attributes dialog. Change the text to Test (don't ever accuse me of dazzling originality) and the item ID to 102. Repeat this for a second button, but name it Done and give it the button ID 103. Place the two buttons next to each other on the dialog. Name the dialog DIALOG1.
Save this dialog resource as DLG1.RES; then start Turbo Pascal for Windows and compile the following program. Be certain all identifiers and numbers are correct because they're needed inside the source. When you run it, a dialog with two buttons appears. Click on Test, and a message box acknowledging your action appears. Click on Done, and the OWL CloseWindow method is called, returning the program's resources to Windows and exiting.
The Finished Product
The program contains extensive comments showing you how to hollow it our and replace its code with your own. It's quite simple; the code is about 30 lines long. Use it in conjunction with chapter 11 of the TPW Cookbook, and you'll be well on your way to building your own programs with custom dialogs.