delphi - teach yourself borland delphi 4 in 21 days

691 409 0
delphi - teach yourself borland delphi 4 in 21 days

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Teach Yourself Borland Delphi 4 in 21 Days Table of Contents: ● Introduction ● Day 1 - Getting Started with Delphi ● Day 2 - More on Pascal ● Day 3 - Classes and Object-Oriented Programming ● Day 4 - The Delphi IDE Explored ● Day 5 - The Visual Component Model ● Day 6 - Working with the Form Designer and the Menu Designer ● Day 7 - VCL Components WEEK 1 - IN REVIEW ● Day 8 - Creating Applications in Delphi ● Day 9 - Projects, the Code Editor, and the Code Explorer ● Day 10 - Debugging Your Applications ● Day 11 - Delphi Tools and Options ● Day 12 - Graphics and Multimedia Programming ● Day 13 - Beyond the Basics ● Day 14 - Advanced Programming WEEK 2 - IN REVIEW ● Day 15 - COM and ActiveX ● Day 16 - Delphi Database Architecture ● Day 17 - Building Database Forms ● Day 18 - Building Database Applications ● Day 19 - Creating and Using DLLs ● Day 20 - Creating Components ● Day 21 - Delphi and C++Builder WEEK 3 - IN REVIEW APPENDIXES ● Appendix A - Answers to the Quiz Questions ● Appendix B - Delphi Internet Resources ● Bonus Day - Building Internet Applications © Copyright, Macmillan Computer Publishing. All rights reserved. Teach Yourself Borland Delphi 4 in 21 Days Introduction: You Are Here Isn't it helpful when an arrow on a map points out exactly where you are? So you are here! Maybe you are here because you have used Delphi before and you want to see what is new in Delphi 4. Maybe you are here because your boss told you to be here. Or maybe you are here as a complete beginner who would like to explore the wonderful world of Windows programming. Regardless of why you are here, welcome! I can assure you that the trip will be an interesting one. You will no doubt find it enjoyable, too. It will involve some work, but there will be some fun thrown in along the way. Believe me when I say that there's nothing quite like taking a passing thought and turning it into a working Windows program. I hope you get the fever and lose yourself in hour after hour of programming. I encourage you to experiment as you read this book. Putting the book down and playing around for a while can prove more valuable than the best teacher. Getting through this book isn't a race. The first one to reach the end doesn't receive a prize. I'd rather you spent 21 weeks learning Delphi programming than to rush through this book without taking time to apply the concepts discussed here. By the way, my experience has been that the best way to learn is to have an application in mind that you want to write and then work on that application as you work through this book. Solving real-world problems is the kind of schooling that sticks. So it doesn't really matter why you are here. What's important is that you are here. I'm glad you are here, and I hope you enjoy your Delphi experience. Relax, put your feet up, and have fun learning how to use Delphi. I know I did. About the Author KENT REISDORPH is a senior software engineer at TurboPower Software Co. He also has his own consulting business. Kent is a contributing editor for The Cobb Group's C++Builder Developer's Journal and contributes regularly to the Delphi Developer's Journal. He is also a member of TeamB, Borland's online volunteer support group. As a member of TeamB, Kent puts in many hours each week on the Borland newsgroups answering questions, primarily on C++Builder and Windows programming. He is the author of Sams Teach Yourself C++Builder in 21 Days and Sams Teach Yourself C++Builder 3 in 21 Days. Kent lives in Colorado Springs, Colorado, with his wife, Jennifer, and their six children, James, Mason, Mallory, Jenna, Marshall, and Joshua. Dedication This book is dedicated to my wife, Jennifer. I couldn't imagine dedicating it to anyone else. Thank you as always, Jen, for keeping everything going while I'm off in my own world. Acknowledgments This part of the book comes fairly easily for me. It's easy to remember those people who were instrumental in making a project like this come to completion. First I want to thank Brian Gill for his hard work on this project. I did my best to rattle Brian on one or more occasions, but he never wavered (not that I could see anyway!). I also want to thank Kezia Endsley for her work on this book. Kezia did a tremendous job as development editor. I'm certain that I have benefited from working with her. Other people at Macmillan Publishing I want to thank are Dana Lesh and Heather Urschel. There are several people at INPRISE Corporation (formerly Borland International) whom I want to thank. Although I didn't have much direct contact with Nan Borreson on this project, I know she was there behind the scenes doing her usual excellent work. I want to thank my tech editors, Bill Fisher and Ellie Peters. They both did a good job keeping me straight. I can't mention Ellie without adding that I'm glad to have Ellie as a friend as well as a tech editor. Also thanks to Steve Teixeira, Steve Trefethen, and Ryder Rishel who were quick to answer specific questions I had during this project. Last but in no way least, I want to thank my wife, Jennifer. This is the third such project I have undertaken, and Jennifer has always been way, way beyond supportive. She has grown far too accustomed to seeing me "head down and headphones on." One of these days I'll make it up to her. I promise. Tell Us What You Think! As the reader of this book, you are our most important critic and commentator. We value your opinion and want to know what we're doing right, what we could do better, what areas you'd like to see us publish in, and any other words of wisdom you're willing to pass our way. As the executive editor for the Programming team at Macmillan Computer Publishing, I welcome your comments. You can fax, email, or write me directly to let me know what you did or didn't like about this book as well as what we can do to make our books stronger. Please note that I cannot help you with technical problems related to the topic of this book, and that due to the high volume of mail I receive, I might not be able to reply to every message. When you write, please be sure to include this book's title and author as well as your name and phone or fax number. I will carefully review your comments and share them with the author and editors who worked on the book. Fax: 317-817-7070 Email: prog@mcp.com Mail: Executive Editor Programming Macmillan Computer Publishing 201 West 103rd Street Indianapolis, IN 46290 USA © Copyright, Macmillan Computer Publishing. All rights reserved. Teach Yourself Borland Delphi 4 in 21 Days - 1 - Getting Started with Delphi ● What Is Delphi? ● A Quick Look at the Delphi IDE ❍ The Object Inspector ❍ The Delphi Workspace ● Your First Program: Hello World ❍ Creating the Program ❍ Modifying the Program ❍ Closing the Program ● Your Second Program: Hello World, Part II ❍ Creating the Hello World II Program ❍ Modifying the Hello World II Program ● Object Pascal Language Overview ● In the Beginning ❍ Pascal Units ❍ Comments in Code ❍ Variables ❍ Object Pascal Data Types ❍ Object Pascal Operators ❍ Constants ❍ Arrays ❍ Strings ❍ String Basics ● Summary ● Workshop ❍ Q&A ❍ Quiz ❍ Exercises Congratulations you've chosen one of today's hottest programming tools! Before you get started using all that Delphi has to offer, though, you first need to learn a little about the Delphi IDE and about Object Pascal. In this chapter you will find ● A quick tour of Delphi ● An introduction to the Object Pascal language ● Facts about Pascal units, variables, and data types ● A discussion of arrays ● Information about strings in Pascal What Is Delphi? By now you know that Delphi is Borland's best-selling rapid application development (RAD) product for writing Windows applications. With Delphi, you can write Windows programs more quickly and more easily than was ever possible before. You can create Win32 console applications or Win32 graphical user interface (GUI) programs. When creating Win32 GUI applications with Delphi, you have all the power of a true compiled programming language (Object Pascal) wrapped up in a RAD environment. What this means is that you can create the user interface to a program (the user interface means the menus, dialog boxes, main window, and so on) using drag-and-drop techniques for true rapid application development. You can also drop ActiveX controls on forms to create specialized programs such as Web browsers in a matter of minutes. Delphi gives you all this, and at virtually no cost: You don't sacrifice program execution speed because Delphi generates fast compiled code. I can hear you saying, "This is going to be so cool!" And guess what? You're right! But before you get too excited, I need to point out that you still have to go to work and learn about Pascal programming. I don't want you to think that you can buy a program like Delphi and be a master Windows programmer overnight. It takes a great deal of work to be a good Windows programmer. Delphi does a good job of hiding some of the low-level details that make up the guts of a Windows program, but it cannot write programs for you. In the end, you must still be a programmer, and that means you have to learn programming. That can be a long, uphill journey some days. The good news is that Delphi can make your trek fairly painless and even fun. Yes, you can work and have fun doing it! So roll up your sleeves and put on your hiking shoes. Delphi is a great product, so have fun. A Quick Look at the Delphi IDE This section contains a quick look at the Delphi integrated development environment (IDE). I'll give the IDE a once-over now and examine it in more detail on Day 4, "The Delphi IDE Explored." Because you are tackling Windows programming, I'll assume you are advanced enough to have figured out how to start Delphi. When you first start the program, you are presented with both a blank form and the IDE, as shown in Figure 1.1. FIGURE 1.1. The Delphi IDE and the initial blank form. The Delphi IDE is divided into three parts. The top window can be considered the main window. It contains the toolbars and the Component palette. The Delphi toolbars give you one-click access to tasks such as opening, saving, and compiling projects. The Component palette contains a wide array of components that you can drop onto your forms. (Components are text labels, edit controls, list boxes, buttons, and the like.) For convenience, the components are divided into groups. Did you notice the tabs along the top of the Component palette? Go ahead and click on the tabs to explore the different components available to you. To place a component on your form, you simply click the component's button in the Component palette and then click on your form where you want the component to appear. Don't worry about the fact that you don't yet know how to use components. You'll get to that in due time. When you are done exploring, click on the tab labeled Standard, because you'll need it in a moment. New Term: A component is a self-contained binary piece of software that performs some specific predefined function, such as a text label, an edit control, or a list box. The Object Inspector Below the main window and on the left side of the screen is the Object Inspector. It is through the Object Inspector that you modify a component's properties and events. You will use the Object Inspector constantly as you work with Delphi. The Object Inspector has two tabs: the Properties tab and the Events tab. A component's properties control how the component operates. For example, changing the Color property of a component changes the background color of that component. The list of properties available varies from component to component, although components usually have several common elements (Width and Height properties, for instance). New Term: A property determines the operation of a component. The Events tab contains a list of events for a component. Events occur as the user interacts with a component. For example, when a component is clicked, an event is generated that tells you that the component was clicked. You can write code that responds to these events, performing specific actions when an event occurs. As with properties, the events that you can respond to vary from component to component. New Term: An event is something that occurs as a result of a component's interaction with the user or with Windows. New Term: An event handler is a section of code that is invoked in your application in response to an event. The Delphi Workspace The main part of the Delphi IDE is the workspace. The workspace initially displays the Form Designer. It should come as no surprise that the Form Designer enables you to create forms. In Delphi, a form represents a window in your program. The form might be the program's main window, a dialog box, or any other type of window. You use the Form Designer to place, move, and size components as part of the form creation process. Hiding behind the Form Designer is the Code Editor. The Code Editor is where you type code when writing your programs. The Object Inspector, Form Designer, Code Editor, and Component palette work interactively as you build applications. Now that you've had a look at what makes up the Delphi IDE, let's actually do something. Your First Program: Hello World It's tradition. Almost all programming books start you off by having you create a program that displays Hello World on the screen. I'm tempted to do something else, but tradition is not a force to be reckoned with, so Hello World it is. You've got some work ahead of you in the next few chapters, so I thought I'd give you a taste of Delphi's goodies before putting you to work learning the seemingly less glamorous basics of the Pascal language. You'll have a little fun first. Delphi (and its cousin, C++Builder) gives you possibly the quickest route to Hello World of any Windows programming environment to date. Creating the Program Right now you should have Delphi running, and you should be looking at a blank form. By default, the form is named Form1. (The form name is significant in Delphi, but I'll address that a little later.) To the left of the form, the Object Inspector shows the properties for the form. Click on the title bar of the Object Inspector. The Caption property is highlighted, and the cursor is sitting there waiting for you to do something. (If the Caption property is not in view, you might have to scroll the Object Inspector window to locate it. Properties are listed in alphabetical order.) Type Hello World! to change the form's caption. NOTE: As you modify properties, Delphi immediately displays the results of the property change when appropriate. As you type the new caption, notice that the window caption of the form is changing to reflect the text you are typing. Now click the Run button on the toolbar (the one with the green arrow). (You can also press F9 or choose Run | Run from the main menu.) Before you even know what has happened, Delphi has built the program. The form is displayed, and the caption shows Hello World!. In this case, the running program looks almost identical to the blank form. You might scarcely have noticed when the program was displayed because it is displayed in the exact location of the form in the Form Designer. (There is a difference in appearance, though, because the Form Designer displays an alignment grid and the running program does not.) Congratulations you've just written your first Windows program with Delphi. Wow, that was easy! "But what is it?" you ask. It's not a lot, I agree, but it is a true Windows program. Try it out and see. The program's main window can be moved by dragging the title bar, it can be sized, it can be minimized, it can be maximized, and it can be closed by clicking the Close button. You can even locate the program in Windows Explorer (it will probably be in your \Delphi40\Bin directory as Project1.exe) and double-click on it to run it. Modifying the Program Okay, so maybe displaying Hello World! in the caption was cheating a little. Let's spruce it up a bit. If you still have the Hello World program running, close it by clicking the Close button in the upper-right corner of the window. The Form Designer is displayed again, and you are ready to modify the form (and, as a result, the program). To make the program more viable, you're going to add text to the center of the window itself. To do this, you'll add a text label to the form: 1. First, click on the Standard tab of the Component palette. The third component button on the palette has an A on it. If you put your mouse cursor over that button, the tooltip (a small pop-up window) will display Label. 2. Click the label button and then click anywhere on the form. A label component is placed on the form with a default caption of Label1. 3. Now turn your attention to the Object Inspector. It now displays the properties for Label1 (remember that previously it was showing the properties for Form1). Again the Caption property is highlighted. 4. Click on the title bar of the Object Inspector or on the Caption property and type Hello World!. Now the label on the form shows Hello World!. 5. As long as you're at it, you can change the size of the label's text as well. Double-click on the Font property. The property will expand to show the additional font attributes below it. 6. Locate the Size property under Font and change the font size to 24 (it is currently set to 8). As soon as you press Enter or click on the form, the label instantly changes to the new size. Because the label is probably not centered on the form, you might want to move it. To move a component, simply click on it and drag it to the position you want it to occupy. When you have the label where you want it, you're ready to recompile and run the program. Click the Run button again and, after a split second, the program runs. Now you see Hello World! displayed in the center of the form as well as in the caption. Figure 1.2 shows the Hello World! program running. FIGURE 1.2. Your Hello World! program running. Closing the Program With this little taste of Delphi, you can see that writing Windows programs with Delphi is going to be a great deal more interesting than it was in the good ol' days. To prepare for what you are going to do next, you need to close the current project in the Delphi IDE. Choose File | Close All from the main menu. Click on No when prompted to save changes to Project1, or save the project if you are fond of your new creation. Your Second Program: Hello World, Part II Before you can move on to learning the Pascal language you need a little more information about how Delphi works. You'll need this information to test the various Pascal language features as you work through the next couple of days. This section will contain just a glimpse into the power of Delphi. On Days 4, 5, and 6, you get a more detailed look into how Delphi works. Creating the Hello World II Program The goal of this exercise is to have the words Hello World, Part II appear on the screen when a button is pressed. This exercise will also give you a pattern you can follow when you test various code snippets as you work through the next couple of days. Perform the following steps: 1. Choose File | New Application from the main menu to start a new application (click No if you're prompted to save the current project). 2. Click the Standard tab on the Component palette and click the icon that has an OK button on it (the Button component). 3. Place your cursor anywhere on the form and click. A button appears on the form. 4. Choose a Label component and place it near the center of the form. At this point your form should look similar to Figure 1.3. Notice that the label component has a default caption of Label1 and the button has a default caption of Button1. Modifying the Hello World II Program In the first version of Hello World, you used the Object Inspector to change the Caption property of a label. That change was applied at design time and as such was seen as soon as the program ran. In this exercise, you are going to change the caption of the label through code. FIGURE 1.3. The new form after placing the button and label components. [...]... Data Type Size in Bytes Possible Range of Values ShortInt 1 -1 28 to 127 Byte 1 0 to 255 Char 1 0 to 255 (same as Byte) WideChar 2 0 to 65,535 (same as Word) SmallInt 2 -3 2,768 to 32,767 Word 2 0 to 65,535 LongInt 4 -2 , 147 ,48 3, 648 to 2, 147 ,48 3, 647 Int 64 8 -9 ,223,372,036,8 54, 775,808 to 9,223,372,036,8 54, 775,807 Integer 4 Same as LongInt Cardinal 4 0 to 2, 147 ,48 3, 647 Single 4 1.5 ¥ 1 0 -4 5 to 3 .4 ¥ 1038 Double... string LowerCase Converts a string to lowercase Pos Returns the position of a search string within a string StringOfChar Returns a string filled with the given number of a particular character StrPas Converts a null-terminated string (PChar or array of Char) to a Pascal-style string StrPCopy Converts a Pascal-style string to a null-terminated string StrToInt Converts a string to an integer If the string... character in the string You cannot access S[0] in long strings or wide strings Control Characters in Strings Object Pascal enables you to embed control characters in strings This is useful if you need to add non-printing characters to your strings This could be as simple as starting a new line in a character string, or it could be more complex, such as embedding control characters in a string sent to... regular array Strings Strings are used heavily in programming Object Pascal has three distinct string types: long string, short string, and wide string In addition to these string types, Pascal also uses null-terminated strings I'll go over each of these types briefly, and then I'll discuss some of the string-manipulation functions Short String The short string type is a fixed-length string of characters... Consult the Delphi online help for a list of all string functions and procedures TABLE 1 .4 STRING MANIPULATION FUNCTIONS AND PROCEDURES Name Description Copy Returns a sub-string within a string Delete Deletes part of a string Format Formats and returns a string based on the format string and arguments passed Insert Inserts text into a string IntToStr Converts an integer value to a string Length Returns... 1038 Double 8 5.0 ¥ 1 0-3 24 to 1.7 ¥ 10308 Real 8 5.0 ¥ 1 0-3 24 to 1.7 ¥ 10308 (same as Double) Extended 10 3 .4 ¥ 1 0 -4 932 to 1.1 ¥ 1 049 32 Comp -9 ,223,372,036,8 54, 775,808 to 9,223,372,036,8 54, 775,807 8 Currency 8 -9 22,337,203,685 ,47 7.5808 to 922,337,203,685 ,47 7.5807 Boolean 1 True or False Variant 16 Varies Examining Table 1.1, you might notice that an Integer is the same as a LongInt So why does Object... a holdover from days gone by In a 16-bit programming environment, an Integer requires 2 bytes of storage and a LongInt requires 4 bytes of storage In a 32-bit programming environment, however, both require 4 bytes of storage and have the same range of values Delphi 4 produces only 32-bit programs, so an Integer and a LongInt are identical Most programmers use Integer rather than LongInt You might also... array of Char String Basics The Pascal string types have several elements in common The following sections describe general string operations that apply to all string types String Concatenation Using the + Operator A common programming task is that of concatenating (adding together) strings Strings can be concatenated using the + operator for example, var S1 : string; S2 : string; begin S1 := `Mallory... button The label will contain two lines of text, as shown in Figure 1.6 This code simply embeds a carriage return character (ASCII 10) in the string, thereby breaking the label into two lines FIGURE 1.6 A label with two lines Extending Strings Across Multiple Code Lines It is often necessary to break a literal string across two or more code lines to increase readability and maintainability of your code... Pascal string types is in string manipulation, you probably won't use a PChar like this very often You will typically use a PChar to convert a long string to a null-terminated string as in the previous example Note that you can pass a string literal (a string of characters within single quotes) to a Windows API function expecting a PChar Finally, you can use an array of the Char data type in place of . B - Delphi Internet Resources ● Bonus Day - Building Internet Applications © Copyright, Macmillan Computer Publishing. All rights reserved. Teach Yourself Borland Delphi 4 in 21 Days Introduction:. rights reserved. Teach Yourself Borland Delphi 4 in 21 Days - 1 - Getting Started with Delphi ● What Is Delphi? ● A Quick Look at the Delphi IDE ❍ The Object Inspector ❍ The Delphi Workspace. Teach Yourself Borland Delphi 4 in 21 Days Table of Contents: ● Introduction ● Day 1 - Getting Started with Delphi ● Day 2 - More on Pascal ● Day 3 - Classes and Object-Oriented

Ngày đăng: 16/04/2014, 11:14

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan