📄 ch01.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD><SCRIPT LANGUAGE="JavaScript"><!--function popUp(pPage) { var fullURL = document.location; var textURL = fullURL.toString(); var URLlen = textURL.length; var lenMinusPage = textURL.lastIndexOf("/"); lenMinusPage += 1; var fullPath = textURL.substring(0,lenMinusPage); popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394'); figDoc= popUpWin.document; zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>'; zhtm += '</head>'; zhtm += '<BODY bgcolor="#FFFFFF">'; zhtm += '<IMG SRC="' + fullPath + pPage + '">'; zhtm += '<P><B>' + pPage + '</B>'; zhtm += '</BODY></HTML>'; window.popUpWin.document.write(zhtm); window.popUpWin.document.close(); // Johnny Jackson 4/28/98 }//--> </SCRIPT><link rel="stylesheet" href="/includes/stylesheets/ebooks.css"> <META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1"> <TITLE>Teach Yourself Borland Delphi 4 in 21 Days -- Ch 1 -- T</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><CENTER><H1><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"></H1><H1><BR>Teach Yourself Borland Delphi 4 in 21 Days</H1></CENTER><CENTER><P><A HREF="../fm/fm.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch02/ch02.htm"><IMGSRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <HR></CENTER><CENTER><H1>- 1 -</H1></CENTER><CENTER><H1>Getting Started with Delphi</H1></CENTER><UL> <LI><A HREF="#Heading1">What Is Delphi?</A> <LI><A HREF="#Heading2">A Quick Look at the Delphi IDE</A> <UL> <LI><A HREF="#Heading3">The Object Inspector</A> <LI><A HREF="#Heading4">The Delphi Workspace</A> </UL> <LI><A HREF="#Heading5">Your First Program: Hello World</A> <UL> <LI><A HREF="#Heading6">Creating the Program</A> <LI><A HREF="#Heading7">Modifying the Program</A> <LI><A HREF="#Heading8">Closing the Program</A> </UL> <LI><A HREF="#Heading9">Your Second Program: Hello World, Part II</A> <UL> <LI><A HREF="#Heading10">Creating the Hello World II Program</A> <LI><A HREF="#Heading11">Modifying the Hello World II Program</A> </UL> <LI><A HREF="#Heading12">Object Pascal Language Overview</A> <LI><A HREF="#Heading13">In the Beginning...</A> <UL> <LI><A HREF="#Heading14">Pascal Units</A> <LI><A HREF="#Heading15">Comments in Code</A> <LI><A HREF="#Heading16">Variables</A> <LI><A HREF="#Heading17">Object Pascal Data Types</A> <LI><A HREF="#Heading18">Object Pascal Operators</A> <LI><A HREF="#Heading19">Constants</A> <LI><A HREF="#Heading20">Arrays</A> <LI><A HREF="#Heading21">Strings</A> <LI><A HREF="#Heading22">String Basics</A> </UL> <LI><A HREF="#Heading23">Summary</A> <LI><A HREF="#Heading24">Workshop</A> <UL> <LI><A HREF="#Heading25">Q&A</A> <LI><A HREF="#Heading26">Quiz</A> <LI><A HREF="#Heading27">Exercises</A> </UL></UL><P><HR SIZE="4"><CENTER><H1></H1></CENTER><P>Congratulations--you've chosen one of today's hottest programming tools! Beforeyou get started using all that Delphi has to offer, though, you first need to learna little about the Delphi IDE and about Object Pascal. In this chapter you will find</P><UL> <LI>A quick tour of Delphi <P> <LI>An introduction to the Object Pascal language <P> <LI>Facts about Pascal units, variables, and data types <P> <LI>A discussion of arrays <P> <LI>Information about strings in Pascal</UL><H2><A NAME="Heading1"></A>What Is Delphi?</H2><P>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 Windowsprograms more quickly and more easily than was ever possible before. You can createWin32 console applications or Win32 graphical user interface (GUI) programs. Whencreating Win32 GUI applications with Delphi, you have all the power of a true compiledprogramming language (Object Pascal) wrapped up in a RAD environment. What this meansis that you can create the user interface to a program (the <I>user interface</I>means the menus, dialog boxes, main window, and so on) using drag-and-drop techniquesfor true rapid application development. You can also drop ActiveX controls on formsto create specialized programs such as Web browsers in a matter of minutes. Delphigives you all this, and at virtually no cost: You don't sacrifice program executionspeed because Delphi generates fast compiled code.</P><P>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 stillhave to go to work and learn about Pascal programming. I don't want you to thinkthat 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 goodjob 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 journeysome days. The good news is that Delphi can make your trek fairly painless and evenfun. Yes, you can work and have fun doing it!</P><P>So roll up your sleeves and put on your hiking shoes. Delphi <I>is </I>a greatproduct, so have fun.</P><P><H2><A NAME="Heading2"></A>A Quick Look at the Delphi IDE</H2><P>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. Whenyou first start the program, you are presented with both a blank form and the IDE,as shown in Figure 1.1.</P><P><A HREF="javascript:popUp('28670101.gif')"><B>FIGURE 1.1.</B></A><B> </B><I>TheDelphi IDE and the initial blank form.</I></P><P>The Delphi IDE is divided into three parts. The top window can be considered themain window. It contains the toolbars and the Component palette. The Delphi toolbarsgive 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 ontoyour forms. (Components are text labels, edit controls, list boxes, buttons, andthe like.) For convenience, the components are divided into groups. Did you noticethe tabs along the top of the Component palette? Go ahead and click on the tabs toexplore 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 onyour form where you want the component to appear. Don't worry about the fact thatyou don't yet know how to use components. You'll get to that in due time. When youare done exploring, click on the tab labeled Standard, because you'll need it ina moment.</P><P><strong>New Term:</strong> A c<I>omponent</I> is a self-contained binary piece of softwarethat performs some specific predefined function, such as a text label, an edit control,or a list box.</P><P><H3><A NAME="Heading3"></A>The Object Inspector</H3><P>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 ObjectInspector has two tabs: the Properties tab and the Events tab. A component's <I>properties</I>control how the component operates. For example, changing the Color property of acomponent changes the background color of that component. The list of propertiesavailable varies from component to component, although components usually have severalcommon elements (Width and Height properties, for instance).</P><P><strong>New Term:</strong> A <I>property</I> determines the operation of a component.</P><P>The Events tab contains a list of events for a component. Events occur as theuser interacts with a component. For example, when a component is clicked, an eventis generated that tells you that the component was clicked. You can write code thatresponds to these events, performing specific actions when an event occurs. As withproperties, the events that you can respond to vary from component to component.</P><P><strong>New Term:</strong> An <I>event</I> is something that occurs as a result of acomponent's interaction with the user or with Windows.</P><P><strong>New Term:</strong> An <I>event handler</I> is a section of code that is invokedin your application in response to an event.</P><P><H3><A NAME="Heading4"></A>The Delphi Workspace</H3><P>The main part of the Delphi IDE is the workspace. The workspace initially displaysthe Form Designer. It should come as no surprise that the Form Designer enables youto create forms. In Delphi, a <I>form</I> represents a window in your program. Theform 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 formcreation process.</P><P>Hiding behind the Form Designer is the Code Editor. The Code Editor is where youtype code when writing your programs. The Object Inspector, Form Designer, Code Editor,and Component palette work interactively as you build applications.</P><P>Now that you've had a look at what makes up the Delphi IDE, let's actually dosomething.</P><P><H2><A NAME="Heading5"></A>Your First Program: Hello World</H2><P>It's tradition. Almost all programming books start you off by having you createa 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 gotsome work ahead of you in the next few chapters, so I thought I'd give you a tasteof Delphi's goodies before putting you to work learning the seemingly less glamorousbasics 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 programmingenvironment to date.</P><P><H3><A NAME="Heading6"></A>Creating the Program</H3><P>Right now you should have Delphi running, and you should be looking at a blankform. By default, the form is named Form1<I>.</I> (The form name is significant inDelphi, but I'll address that a little later.) To the left of the form, the ObjectInspector shows the properties for the form. Click on the title bar of the ObjectInspector. The Caption property is highlighted, and the cursor is sitting there waitingfor you to do something. (If the Caption property is not in view, you might haveto scroll the Object Inspector window to locate it. Properties are listed in alphabeticalorder.) Type Hello World! to change the form's caption.</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> 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. <HR></BLOCKQUOTE><P>Now click the Run button on the toolbar (the one with the green arrow). (You canalso press F9 or choose Run | Run from the main menu.) Before you even know whathas happened, Delphi has built the program. The form is displayed, and the captionshows Hello World!. In this case, the running program looks almost identical to theblank form. You might scarcely have noticed when the program was displayed becauseit is displayed in the exact location of the form in the Form Designer. (There isa difference in appearance, though, because the Form Designer displays an alignmentgrid and the running program does not.) Congratulations--you've just written yourfirst Windows program with Delphi. Wow, that was easy!</P><P>"But what is it?" you ask. It's not a lot, I agree, but it is a trueWindows program. Try it out and see. The program's main window can be moved by draggingthe title bar, it can be sized, it can be minimized, it can be maximized, and itcan be closed by clicking the Close button. You can even locate the program in WindowsExplorer (it will probably be in your \Delphi40\Bin directory as Project1.exe) anddouble-click on it to run it.</P><P><H3><A NAME="Heading7"></A>Modifying the Program</H3><P>Okay, so maybe displaying Hello World! in the caption was cheating a little. Let'sspruce it up a bit. If you still have the Hello World program running, close it byclicking the Close button in the upper-right corner of the window. The Form Designeris displayed again, and you are ready to modify the form (and, as a result, the program).</P><P>To make the program more viable, you're going to add text to the center of thewindow itself. To do this, you'll add a text label to the form:</P><DL> <DT></DT> <DD><B>1. </B>First, click on the Standard tab of the Component palette. The third component button on the palette has an <I>A</I> on it. If you put your mouse cursor over that button, the tooltip (a small pop-up window) will display Label. <P> <DT></DT> <DD><B>2. </B>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. <P> <DT></DT> <DD><B>3. </B>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. <P> <DT></DT> <DD><B>4. </B>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!. <P> <DT></DT> <DD><B>5. </B>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. <P> <DT></DT> <DD><B>6. </B>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. <P></DL><P>Because the label is probably not centered on the form, you might want to move
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -