delphi - delphi borland in 14 days

536 304 0
delphi - delphi borland in 14 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

1 At A Glance P2/V5/Swg2 TY C++ Builder in 14 Days 31051-1 Lenser 1.14.97 At a GlanceWK1 LP#3 1 WEEK 5 4 3 2 1 6 7 At a Glance In Week 1 you will get a start on learning how to write Windows programs in C++. The C++ language is not an easy language to learn. It is, however, the standard programming language in many corporations and governments around the world. Learning C++ might not be the easiest task you could attempt to tackle, but it should be very rewarding, both intellectually, and, eventually, monetarily. Your first four days will be spent learning about the basics of the C++ language. As you work through the first four chapters, you will write simple test programs, each of which will help you solidify a particular feature of the C++ language. I warn you, though, that these programs will probably not be the type of program that you purchased C++Builder to write. The test programs for the first four days will be console applications. These programs work just like DOS programs. They won’t Untitled-16 1/19/99, 1:01 PM1 2 Week 1 P2/V5/Swg2 TY C++ Builder in 14 Days 31051-1 Lenser 1.14.97 At a GlanceWK1 LP#3 have any flash or glitter. You probably won’t be terribly impressed. These programs will, however, help to teach you the basics of C++, and that is what the first four days of this book are about. Starting on Day 5 you’ll begin to learn about some of the things that make the visual programming aspect of C++Builder the great tool that it is. We will talk about frameworks and what a framework means to you as a Windows programmer. On Day 5 you will build a simple test program using C++Builder’s visual programming tools. After that we will spend a couple days going over the C++Builder IDE so that you can become familiar with how the entire C++Builder IDE works together to make your programming tasks easier. This is where things start to get more interesting. You will get an opportunity to write some working Windows programs in the last part of this first week. So, with that in mind, let’s get to it. Untitled-16 1/19/99, 1:01 PM2 3 Getting Your Feet Wet P2/V5/swg2 TY C++ Builder in 14 Days 31051-1Lenser 1.15.97 CH01 LP#4 1 Day 1 1 Week Getting Your Feet Wet by Kent Reisdorph Congratulations—you’ve chosen one of today’s hottest new programming tools! Before you can jump into using all of what C++Builder has to offer, though, you’ll need to learn a little about C++ first. In this chapter you will find ■■ A quick tour of C++Builder ■■ Information about how to write a Win32 console-mode application ■■ An introduction to the C++ language ■■ Facts about C++ variables and data types ■■ Information about functions in C++ (including the main() function) ■■ A discussion of arrays Untitled-10 1/19/99, 12:48 PM3 4 Day 1 P2/V5/swg2 TY C++ Builder in 14 Days 31051-1 Lenser 1.15.97 CH01 LP#4 What Is C++Builder? By now you know that C++Builder is Borland’s hot new rapid application development (RAD) product for writing C++ applications. With C++Builder you can write C++ Windows programs more quickly and more easily than was ever possible before. You can create Win32 console applications or Win32 GUI (graphical user interface) programs. When creating Win32 GUI applications with C++Builder, you have all the power of C++ 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 OCX controls on forms to create specialized programs such as Web browsers in a matter of minutes. C++Builder gives you all of this, but you don’t sacrifice program execution speed because you still have the power that the C++ language offers you. I can hear you saying, “This is going to be so cool!” And guess what? You’re right! But before you go slobbering all over yourself with anticipation, I also need to point out that the C++ language is not an easy one to master. I don’t want you to think that you can buy a program like C++Builder and be a master Windows programmer overnight. It takes a great deal of work to be a good Windows programmer. C++Builder does a great 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 C++Builder can make your trek fairly painless and even fun. Yes, you can work and have fun doing it! So roll up your sleeves and get your hiking shoes on. C++Builder is cool, so have fun. A Quick Look at the C++Builder IDE This section contains a quick look at the C++Builder IDE. We’ll give the IDE a once-over here, and we’ll examine it in more detail on Day 6, “The C++Builder IDE Explored: Projects and Forms.” Because you are tackling Windows programming, I’ll assume you are advanced enough to have figured out how to start C++Builder. When you first start the program, you are presented with both a blank form and the IDE, as shown in Figure 1.1. The C++Builder IDE (which stands for integrated development environment) is divided into three parts. The top window might be considered the main window. It contains the speedbar on the left and the Component Palette on the right. The speedbar gives you one-click access to tasks like opening, saving, and compiling projects. The Component Palette contains a wide array of components that you can drop onto your forms. (Components are things like text labels, edit controls, list boxes, buttons, and the like.) For convenience, the components Untitled-10 1/19/99, 12:48 PM4 5 Getting Your Feet Wet P2/V5/swg2 TY C++ Builder in 14 Days 31051-1Lenser 1.15.97 CH01 LP#4 1 A component is a self-contained piece of binary software that performs some specific predefined task, such as a text label, an edit control, or a list box. Below the speedbar and Component Palette and glued to 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 C++Builder. The Object Inspector has one or two tabs, depending on the component currently selected. It always has a Properties tab. A component’s properties control how the component operates. For example, changing the Color property of a component will change the background color of that component. The list of available properties varies from component to component, although components usually have several common elements ( Width and Height properties, for instance). N EW T ERM Figure 1.1. The C++Builder IDE and the initial blank form. 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. We’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. Untitled-10 1/19/99, 12:48 PM5 6 Day 1 P2/V5/swg2 TY C++ Builder in 14 Days 31051-1 Lenser 1.15.97 CH01 LP#4 A property determines the operation of a component. Usually the Object Inspector has an Events tab in addition to the Properties tab. Events occur as the user interacts with a component. For example, when a component is clicked, an event fires and tells Windows that the component was clicked. You can write code that responds to those events, performing specific actions when an event occurs. As with properties, the events that you can respond to vary from component to component. An event is a method that is invoked in a component as a result of that component’s interaction with the user. To the right of the Object Inspector is the C++Builder workspace. The workspace initially displays the Form Editor. It should come as no surprise that the Form Editor allows you to create forms. In C++Builder 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 Editor to place, move, and size components as part of the form creation process. Hiding behind the Form Editor is the Code Editor. The Code Editor is where you type code when writing your programs. The Object Inspector, Form Editor, Code Editor, and Component Palette work interactively as you build applications. Now that you’ve had a look at what makes up the C++Builder IDE, let’s actually do something. 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 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 C++Builder’s goodies before putting you to work on learning the seemingly less-glamorous basics of C++. You’ll have a little fun before you have to go on the chain gang. C++Builder (and its cousin, Delphi) possibly allow you the quickest route to Hello World of any Windows programming environment to date. Right now you should have C++Builder running, and you should be looking at a blank form. By default, the form is named Form1. (The form name is significant in C++Builder, 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. N EW T ERM N EW T ERM Untitled-10 1/19/99, 12:48 PM6 7 Getting Your Feet Wet P2/V5/swg2 TY C++ Builder in 14 Days 31051-1Lenser 1.15.97 CH01 LP#4 1 As you modify properties, C++Builder will immediately display 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 speedbar (the one with the green arrow). (You could also press F9 or choose Run | Run from the main menu.) C++Builder begins to build the program. The compiler status dialog box, shown in Figure 1.2, is displayed, and you can watch as C++Builder whips through the files necessary to build your program. After a brief wait, the compiler status box disappears, the form is displayed, and the caption shows Hello World!. In this case, the running program looks almost identical to the blank form. You may scarcely have noticed when the program was displayed because it is displayed in the exact location of the form in the Form Editor. (There is a difference, though, because the Form Editor displays an alignment grid and the running program does not.) Congratulations— you’ve just written your first C++ Windows program with C++Builder. Wow, that was easy! “But what is it?” you ask. It’s not a lot, I agree, but it is a true Windows program. It 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. N OTE Figure 1.2. The compiler status dialog box. Okay, so maybe displaying Hello World! just 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 Editor is displayed again, and you are ready to modify the form (and, as a result, the program). To make the program more viable, we’re going to add text to the center of the window itself. To do this, we’ll add a text label to the form. 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 tool tip will display Label. Click the label button and then click anywhere on the form. A label component is placed on the form. Now turn your attention to the Object Inspector. It now displays the properties for Label1 (remember that before it was showing the properties for Form1). Again the Caption property is highlighted. Untitled-10 1/19/99, 12:48 PM7 8 Day 1 P2/V5/swg2 TY C++ Builder in 14 Days 31051-1 Lenser 1.15.97 CH01 LP#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!. As long as we’re at it, let’s 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. Locate the Size property under Font and change the font size to 24 (it is currently set to 8). As soon as you press the Enter key or click on the form, the label instantly changes to the new size. Because the label is probably not centered on the form, you may want to move it. To move a component, simply click on it and drag it to the position you want it to occupy. Once you have the label where you want it, you’re ready to recompile and run the program. Click the Run button again. C++Builder compiles the program again and, after a moment (shorter this time), the program runs. Now you see Hello World! displayed in the center of the form as well as in the caption. Figure 1.3 shows the Hello World program running. Figure 1.3. The Hello World program running. With this little taste of C++Builder, you can see that writing C++ Windows programs with C++Builder 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 C++Builder IDE. Choose File | Close All from the main menu. Click No when prompted to save changes to Project1, or save the project as HelloWorld if you are fond of your new creation. Hello World, Part II—A Win32 Console Application In the next couple chapters you are going to learn the basics of the C++ language. Along the way you will write some simple test programs. These test programs will work best as console applications. For all intents and purposes, these programs look like DOS programs when they run. There are some major differences between a Win32 console app and a DOS program, Untitled-10 1/19/99, 12:48 PM8 9 Getting Your Feet Wet P2/V5/swg2 TY C++ Builder in 14 Days 31051-1Lenser 1.15.97 CH01 LP#4 1 but you need not be concerned about that right now. So, without further ado, let’s create Hello World as a Win32 console program with C++Builder. A Win32 console application is a 32-bit program that runs in a DOS box under Windows 95 or Windows NT. From the main menu, choose File | New. C++Builder displays the Object Repository. Curiously enough, the Object Repository’s title bar says New Items, but don’t be thrown by that. The Object Repository contains predefined projects, forms, dialog boxes, and other objects you can add to your applications or use to begin a new project. I will discuss the Object Repository in detail on Day 9, “Creating Applications in C++Builder.” For now, click on the New tab in the Object Repository and double-click Console App to start a new console application project. C++Builder creates the project and displays the Code Editor so that you can enter code for the program. Figure 1.4 shows the Code Editor as it appears when starting a new console-mode application. N EW T ERM Figure 1.4. The C++Builder Code Editor window. You will notice a couple of differences between the C++Builder IDE now and how it looked earlier when we created a GUI application. First, there is no Form Editor. That’s because a console application can’t display forms (well, that’s not completely true, but it’s accurate enough for this discussion). Also notice that the Object Inspector is blank. You can only place components on a form, so the Object Inspector is useless in a console application. When writing console applications, you can close the Object Inspector to make more room for the Code Editor window. Close the Object Inspector by clicking the Close button on the Object Inspector’s title bar. To bring back the Object Inspector, press F11 or choose View | Object Inspector from the main menu. T IP Untitled-10 1/19/99, 12:48 PM9 10 Day 1 P2/V5/swg2 TY C++ Builder in 14 Days 31051-1 Lenser 1.15.97 CH01 LP#4 When you examine the Code Editor, you should see the following text displayed in the editor window: // #include <condefs.h> #include <stdio.h> #include <stdlib.h> #pragma hdrstop // int main(int argc, char **argv) { return 0; } // This is a do-nothing C++ program, but a valid C++ program nonetheless. We’ll modify the code in just a moment to make this program actually do something, but first I want you to notice the lines that begin with //. These are comment lines that, in this program, serve no purpose other than to divide the program’s code visually. (You will normally use comment lines to document your code.) C++Builder adds these comment lines automatically when a new console application is first created. (In future code listings I will eliminate the comment lines to save space.) Notice also that the single statement in this code ends in a semicolon. (I know it doesn’t make sense right now, but there is only one actual executable statement in this program.) The semicolon is used at the end of each statement in a C++ program. Very early in the process of learning the C and C++ languages, the budding programmer must learn the difference between an expression and a statement. The “official” definition of a statement is “an expression that is followed by a semicolon.” The semicolon closes an expression and makes it a kind of single-line block of code. I’ll get into the code block soon, but for now you should realize that an expression is a unit of code that evaluates to some quantity. A statement is an expression that is closed. For example, consider the following statement: c = a + b; In this example, the portion to the right of the equal sign, a + b, is an expression. The entire line is a statement. I know this may be a bit confusing at the moment, but it should become clearer as we go along. I’ll try to be very careful when I use these two terms. For now, though, just remember that a statement is followed by a semicolon and is a closed expression. Also notice the opening and closing braces in the program. In C++, a block of code begins with the opening brace ( {) and ends with the closing brace (}). The braces are used to delineate the beginning and end of code blocks associated with loops, functions, if statements, and in other cases as well. In this program there is only one set of braces because it is a simple program. A NALYSIS Untitled-10 1/19/99, 12:48 PM10 [...]... passed in the command line This will always be at least 1 because the program name counts as a parameter The variable argv is an array of pointers to strings This array will contain each string passed in the command line For this code example, the following are true: contains 4 contains c:\bc5\bin\grep.com contains WM_KILLFOCUS contains -d contains -i argv argc[0] argc[1] argc[2] argc[3] Untitled-10 26... program in the editor window until it looks like Listing 1.2 You don’t need to add the comment lines if you don’t want to Remember to skip the line numbers ANALYSIS Listing 1.2 HELLO.CPP (revised) 1: 2: 3: 4: 5: 6: 7: 8: #include #include #include #include #include #pragma hdrstop // add this line int main(int argc, char **argv) continues Untitled-10... Untitled-10 27 P2/V5/swg2 TY C++ Builder in 14 Days 1/19/99, 12:48 PM 3105 1-1 Lenser 1.15.97 CH01 LP#4 Day 1 28 In most programs the value returned from main() is irrelevant because the return value is not typically used In fact, you don’t need your main() function to return a value at all There is more than one form of main() The following all represent valid declarations: main(); int main(); int main(int... two functions used in Windows programs wsprintf() is functionally the same as sprintf(), with one major difference: It does not allow you to put floating-point Untitled-10 34 P2/V5/swg2 TY C++ Builder in 14 Days 1/19/99, 12:48 PM 3105 1-1 Lenser 1.15.97 CH01 LP#4 Getting Your Feet Wet 35 numbers in the formatted string You can use either function in your C++Builder programs, but sprintf() is preferred... in C++ (32-bit programs) Data Type char unsigned char short unsigned short long Size in Bytes 1 1 2 2 4 Possible Range of Values -1 28 0 to 126 to 255 -3 2,768 0 to 32,767 to 65,535 -2 ,147 ,483,648 to 2 ,147 ,483,648 unsigned long int unsigned int float double bool Untitled-10 4 4 4 4 8 1 17 P2/V5/swg2 TY C++ Builder in 14 Days to 4,294,967,295 Same as long Same as unsigned long 1.2E-38 to 3.4E381 2.2E-308... that you have a Win32 console application that you execute from a DOS prompt with the following command line: grep WM_KILLFOCUS -d -i In this case you are starting a program called grep with command-line arguments of WM_KILLFOCUS, -d, and -i Given that example, let me show you how that translates to argc and argv inside the main() function First of all, the integer variable argc will contain the number... myArray[4] = = = = = -2 00; -1 00; 0; 100; 200; Later in your program you can access the individual elements of the array again using the subscript operator: int result = myArray[3] + myArray[4]; Untitled-10 28 P2/V5/swg2 TY C++ Builder in 14 Days // result will be 300 1/19/99, 12:48 PM 3105 1-1 Lenser 1.15.97 CH01 LP#4 Getting Your Feet Wet 29 There is a shortcut method to declaring and filling an array all... the string Converts all characters in a string to uppercase Builds a string based on a variable number of parameters strupr() sprintf() NOTE The string operations discussed here are how strings are handled in C Most C++ compilers provide a cstring class that simplifies the difficulties inherent in the C way of handling strings (C++Builder’s Visual Component Library contains a class called Strings that... less, you’ll be safe In reality, anything more than about 20 characters is too long to be useful anyway C++ Data Types NEW TERM Untitled-10 In C++ a data type defines the way the compiler stores information in memory 16 P2/V5/swg2 TY C++ Builder in 14 Days 1/19/99, 12:48 PM 3105 1-1 Lenser 1.15.97 CH01 LP#4 Getting Your Feet Wet 17 In some programming languages you can get by with assigning any type of value... P2/V5/swg2 TY C++ Builder in 14 Days 1/19/99, 12:48 PM 3105 1-1 Lenser 1.15.97 CH01 LP#4 Getting Your Feet Wet 27 Let’s prove that this works with a little sample program Create a new console application in C++Builder and enter the program shown in Listing 1.5 1 Listing 1.5 ARGSTEST.CPP 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: #include #include #pragma hdrstop int main(int argc, char **argv) . be used in your current program and reused in future programs. Listing 1.2. continued A NALYSIS Untitled-10 1/19/99, 12:48 PM14 15 Getting Your Feet Wet P2/V5/swg2 TY C++ Builder in 14 Days 3105 1-1 Lenser. the opening and closing braces in the program. In C++, a block of code begins with the opening brace ( {) and ends with the closing brace (}). The braces are used to delineate the beginning and. displayed in the editor window: // #include <condefs.h> #include <stdio.h> #include <stdlib.h> #pragma hdrstop // int main(int argc, char **argv) { return 0; } // This is a do-nothing

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

Mục lục

  • Week 1 At a Glance

    • Day 1 Getting Your Feet Wet

    • Day 2 Wading In Deeper

    • Day 3 Up to your Neck in C++

    • Day 4 Totally Immersed: C++ Classes and Object-Oriented Programming

    • Day 5 C++ Class Frameworks and the Visual Component Model

    • Day 6 The C++Builder IDE Explored: Projects and Forms

    • Day 7 Working with the Form Designer and the Menu Designer

    • Week 2 At a Glance

      • Day 8 VCL Components

      • Day 9 Creating Applications in C++Builder

      • Day 10 More on Projects

      • Day 11 Using the Debugger

      • Day 12 C++Builder Database Architecture

      • Day 13 Building Internet Applications

      • Day 14 C++Builder Extensions to C++

      • Appendix A Answers to Quiz Questions

      • Add to Your Sams Library

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

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

Tài liệu liên quan