📄 ch01.htm
字号:
it. To move a component, simply click on it and drag it to the position you wantit to occupy. When you have the label where you want it, you're ready to recompileand run the program. Click the Run button again and, after a split second, the programruns. Now you see Hello World! displayed in the center of the form as well as inthe caption. Figure 1.2 shows the Hello World! program running.</P><P><A HREF="javascript:popUp('28670102.gif')"><B>FIGURE 1.2.</B></A><B> </B><I>Your</I>Hello World!<I> program running.</I><DL> <P></DL><P><H3><A NAME="Heading8"></A>Closing the Program</H3><P>With this little taste of Delphi, you can see that writing Windows programs withDelphi 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 projectin the Delphi IDE. Choose File | Close All from the main menu. Click on No when promptedto save changes to Project1, or save the project if you are fond of your new creation.</P><P><H2><A NAME="Heading9"></A>Your Second Program: Hello World, Part II</H2><P>Before you can move on to learning the Pascal language you need a little moreinformation about how Delphi works. You'll need this information to test the variousPascal language features as you work through the next couple of days. This sectionwill contain just a glimpse into the power of Delphi. On Days 4, 5, and 6, you geta more detailed look into how Delphi works.</P><P><H3><A NAME="Heading10"></A>Creating the Hello World II Program</H3><P>The goal of this exercise is to have the words Hello World, Part II appear onthe screen when a button is pressed. This exercise will also give you a pattern youcan follow when you test various code snippets as you work through the next coupleof days. Perform the following steps:</P><DL> <DT></DT> <DD><B>1. </B>Choose File | New Application from the main menu to start a new application (click No if you're prompted to save the current project). <P> <DT></DT> <DD><B>2. </B>Click the Standard tab on the Component palette and click the icon that has an OK button on it (the Button component). <P> <DT></DT> <DD><B>3. </B>Place your cursor anywhere on the form and click. A button appears on the form. <P> <DT></DT> <DD><B>4. </B>Choose a Label component and place it near the center of the form. <P></DL><P>At this point your form should look similar to Figure 1.3. Notice that the labelcomponent has a default caption of Label1 and the button has a default caption ofButton1.</P><P><H3><A NAME="Heading11"></A>Modifying the Hello World II Program</H3><P>In the first version of Hello World, you used the Object Inspector to change theCaption property of a label. That change was applied at design time and as such wasseen as soon as the program ran. In this exercise, you are going to change the captionof the label through code.</P><P><A HREF="javascript:popUp('286701x3.gif')"><B>FIGURE 1.3.</B></A><B> </B><I>Thenew form after placing the button and label components.</I></P><BLOCKQUOTE> <P><HR><BR> <strong>NOTE:</strong> When you change a component's properties through the Object Inspector and Form Designer, you are said to make a <I>design-time</I> change. When you modify a property through code that executes when the program runs, you are said to make a <I>runtime</I> change. <HR></BLOCKQUOTE><P>To change the Caption property at runtime, follow these steps:</P><DL> <DT></DT> <DD><B>1. </B>Double-click on the button on your form. As soon as you do, Delphi generates an event handler for the button's OnClick event. The generated code looks like this:</DL><BLOCKQUOTE> <PRE>procedure TForm1.Button1Click(Sender: TObject);beginend;</PRE></BLOCKQUOTE><PRE></PRE><DL> <DT></DT> <DD><B>2. </B>Right now you don't need to be concerned with everything you see here. You only need to understand that the OnClick event handler is a section of code that will be executed every time the button is clicked (as long as the program is running, that is). The editing cursor is placed between the begin and end statements and is waiting for you to type code. Enter this code at the cursor: <P></DL><BLOCKQUOTE> <PRE>Label1.Caption := `Hello World, Part II';</PRE></BLOCKQUOTE><PRE></PRE><DL> <DT></DT> <DD>I always indent two spaces (considered by many programmers to be proper coding practice) so my completed event handler now looks like this: <P></DL><BLOCKQUOTE> <PRE>procedure TForm1.Button1Click(Sender: TObject);begin Label1.Caption := `Hello World, Part II';end;</PRE></BLOCKQUOTE><PRE></PRE><DL> <DT></DT> <DD>This code is pretty simple. It simply assigns the value Hello World, Part II to the Caption property of the label (the Caption property is used to set the text that the label displays). <P> <DT></DT> <DD><B>3. </B>Now click on the Run button on the toolbar to run the program. When you run the program, notice that the label still has the caption Label1. Click the form's button and the label's caption changes to Hello World, Part II. Hey, how about that! Magic? No, just Delphi at work! <P></DL><P>You'll be doing many such exercises in the next few days so you'll get plentyof practice placing labels, buttons, and other components on the form. I realizethat I didn't fully explain what is going on behind the scenes here, but I don'twant to get ahead of myself so I'll save that explanation for a later time.</P><P><H2><A NAME="Heading12"></A>Object Pascal Language Overview</H2><P>Before you can learn about the RAD features of Delphi, you need to learn the basicsof the Object Pascal language. This part of the book will probably not be the mostexciting for you, but you need a basic understanding of Object Pascal before youmove on.</P><P>It would be nice if presenting the Object Pascal language could be handled sequentially.That's not the case, though, because all the features you will learn about are intertwined.I'll take the individual puzzle pieces one at a time and start fitting them together.</P><P>By the end of Day 3, you'll have a fairly complete picture of the Object Pascallanguage. Don't be concerned if you don't instantly grasp every concept that is presented.Some of what is required to fully understand Object Pascal can only come with real-worldexperience.</P><P>During the next few days, you will see short code snippets that illustrate a particularfeature of the Object Pascal language. You will also do some exercises that enableyou to test your newfound knowledge. In the first few days, you will only see yourDelphi applications in small sections. I don't want to get ahead of myself and gotoo far into the Delphi IDE or the Visual Component Library (VCL) at this early stage.You will have to settle for bits and pieces until later in the book when you startto get the complete picture. The code that you can download from the book's sitecontains complete programs for some of the exercises that you will perform over thenext several days. (Go to http://www.mcp.com/info and type 0-672-31286-7.)</P><P><H2><A NAME="Heading13"></A>In the Beginning...</H2><P>Back in 1994 or so, Borland began working on a RAD tool that it code-named Delphi.When it was decided that the component model architecture was the best way to implementRAD, it was then necessary to settle on the programming language that would be theheart of the system.</P><P>At that time, Borland was the only compiler vendor mass marketing a Pascal compiler.Borland was known as the company that produced the best Pascal tools. If you werea Pascal programmer, you probably used Borland's TurboPascal in one flavor or another.Borland more or less "owned" Pascal. Although Borland didn't own the Pascallanguage in a legal sense, it no doubt felt that because of its position in the Pascalworld, it could take considerable liberties in implementing new language featuresand enhancements. In addition, there was no Pascal standards committee, nor evena written standard defining the Pascal language. So Borland created Delphi usingPascal as the base language (the Borland internal code name stuck and became theofficial product name).</P><P>Before Delphi came into being, Borland had already modified the Pascal languagein positive ways. For example, Borland had already extended Pascal by creating anew language called Object Pascal. It can be said that Object Pascal is to Pascalwhat C++ is to C. Object Pascal added classes to Pascal, thereby hurling Pascal intothe world of object-oriented programming (OOP) languages. As Delphi was being developed,new language behavior and keywords were added to deal with the component model. Keywordssuch as published and property were added, as were others. This enabled Borland tofully implement the power of the component model. By modifying the Pascal languageto suit the component model, Borland was able to implement RAD the right way. Inessence, the Object Pascal language was modified as needed when design issues cameup during the development of the then-unknown product called Delphi. The result isa language that works seamlessly with the component model.</P><P>Although modifying the Pascal language could be considered a bold step for Borland,it was not without precedent. Previously, Microsoft had taken the BASIC languageand modified it to produce a new language called Visual Basic. This new languagewas nearly unrecognizable when compared to the original BASIC language that servedas its base.</P><P>Borland took a risk in modifying Pascal. After all, it had a loyal base of customersthat might not take kindly to enhancements to the language they had come to knowand love. Still, Borland was in a solid position in the Pascal market and went aheadwith its plans. The result was a smash hit, of course.</P><P>Make no mistake about it, Object Pascal is a powerful programming language, andI don't make that statement lightly. I have a C/C++ background and, like other C/C++programmers, I viewed Delphi with a bit of skepticism at first. I found out quickly,though, that the Object Pascal language is very capable. In fact, in the hands ofthe average programmer there is almost no difference in the two languages in termsof power. Object Pascal is unique in that it is both powerful <I>and</I> relativelyeasy to learn. I don't in any way want to leave the impression that Object Pascalis a not a full-featured programming language. Pascal has often been knocked as aless-than-serious programming language. That has never been true, and is even lesstrue with today's Object Pascal.</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> Several different terms have been adopted by Delphi programmers to describe what they do. The base language of Delphi is, of course, Object Pascal, and some folks call it exactly that. Others might say, "I program in Pascal," or even just, "I'm a Delphi programmer." In the end it's up to you to decide what terminology you will use. I'll use the terms Object Pascal and Pascal interchangeably throughout this book and will typically reserve use of the word Delphi to refer to the Delphi IDE or its tools. <HR></BLOCKQUOTE><P>Object Pascal enables you to take advantage of object-oriented programming toits fullest. OOP is not just a buzzword. It has real benefits because it enablesyou to create objects that can be used in your current program and reused in futureprograms.</P><P><strong>New Term:</strong> An <I>object</I>, like components described earlier, is abinary piece of software that performs a specific programming task. (Components areobjects, but not all objects are components. I'll explain that later.)</P><P>An object reveals to the user (the programmer using the object) only as much ofitself as needed; therefore, using the object is simplified. All internal mechanismsthat the user doesn't need to know about are hidden from sight. All this is includedin the concept of object-oriented programming. OOP enables you to take a modularapproach to programming, thus keeping you from constantly re-inventing the wheel.Delphi programs are very OOP-centric because of Delphi's heavy use of components.After a component is created (either one of your own or one of the built-in components),it can be reused in any Delphi program. A component can also be extended by inheritanceto create a new component with additional features. Best of all, components hidetheir internal details and let the programmer concentrate on getting the most outof the component. Objects and classes are discussed in detail on Day 3, "Classesand Object-Oriented Programming."</P><P><H3><A NAME="Heading14"></A>Pascal Units</H3><P>Programming is more than just typing code. Ultimately, it is the combination ofconceptualizing a programming task and then typing code to carry out that task. Thecode you type simply goes into a text file. The compiler takes that text file andcompiles it into machine code that the computer can understand. The text file thatDelphi compiles into machine code is called a unit.</P><P><strong>New Term:</strong> A <I>unit </I>is a text file that can be compiled into a moduleof code.</P><P><H4>Types of Units</H4><P>A Delphi GUI application will contain at least two units. The project source unitcontains the project source code. Project source code units have an extension ofDPR. You can view the project source unit by choosing Project | View Source fromthe main menu. It is not normally necessary to modify the project source unit. Infact, you shouldn't modify the project source unit unless you know exactly what youare doing. If you accidentally modify the project source unit in undesirable ways,you might find that your application won't compile anymore. (Certain advanced programmingtechniques require modification of the project source code, but that's not somethingyou need to be concerned with at this time.)</P><P>The second type of unit that a Delphi GUI application always has is the main form'sunit. A form unit, as its name implies, is a source code unit with an associatedform. This type of unit has a filename extension of PAS. This is the type of unityou will use most often in your Delphi programs. A Delphi GUI application will alwayshave one form unit (for the main form), but it can have one or more additional formunits as well. For example, an application that displays an About box will have themain form unit and a unit for the About box.</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> You might have noticed that I keep saying "Delphi GUI application." This is because I want to distinguish a GUI application from a console mode application. A console mode application is a 32-bit Windows application that runs in a console window (DOS box). A console application has no main form and may or may not contain other forms. A console application does, however, have one or more units. <HR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -