⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch07.htm

📁 delphi自学的好教材!特别适合刚刚起步学习delphi的人员!同样对使用者具有参考价值!
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<!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 7 -- VCL Components</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="../ch06/ch06.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch08/ch08.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>- 7 -</H1></CENTER><CENTER><H1>VCL Components</H1></CENTER><UL>	<LI><A HREF="#Heading1">A Review of Components</A>	<UL>		<LI><A HREF="#Heading2">Visual Components</A>		<LI><A HREF="#Heading3">Nonvisual Components</A>	</UL>	<LI><A HREF="#Heading4">The Name Property</A>	<LI><A HREF="#Heading5">Important Common Properties</A>	<UL>		<LI><A HREF="#Heading6">The Align Property</A>		<LI><A HREF="#Heading7">The Color Property</A>		<LI><A HREF="#Heading8">The Cursor Property</A>		<LI><A HREF="#Heading9">The Enabled Property</A>		<LI><A HREF="#Heading10">The Font Property</A>		<LI><A HREF="#Heading11">The Hint Property</A>		<LI><A HREF="#Heading12">The ParentColor, ParentCtl3D, ParentFont, and ParentShowHint		Properties</A>		<LI><A HREF="#Heading13">The Tag Property</A>		<LI><A HREF="#Heading14">Other Common Properties</A>	</UL>	<LI><A HREF="#Heading15">Primary Methods of Components</A>	<LI><A HREF="#Heading16">Common Events</A>	<LI><A HREF="#Heading17">Standard Windows Control Components</A>	<LI><A HREF="#Heading18">Edit Controls</A>	<UL>		<LI><A HREF="#Heading19">The ListBox and ComboBox Components</A>		<LI><A HREF="#Heading20">VCL Button Types</A>		<LI><A HREF="#Heading21">The Label Component</A>		<LI><A HREF="#Heading22">The ScrollBar Component</A>	</UL>	<LI><A HREF="#Heading23">The Panel Component</A>	<LI><A HREF="#Heading24">And That's Not All...</A>	<LI><A HREF="#Heading25">The Common Dialog Boxes</A>	<UL>		<LI><A HREF="#Heading26">The Execute Method</A>		<LI><A HREF="#Heading27">The File Open and File Save Dialog Boxes</A>		<LI><A HREF="#Heading28">The File Open Picture and File Save Picture Dialog Boxes</A>		<LI><A HREF="#Heading29">The Color Dialog Box</A>		<LI><A HREF="#Heading30">The Font Dialog Box</A>		<LI><A HREF="#Heading31">The Find and Replace Dialog Boxes</A>	</UL>	<LI><A HREF="#Heading32">Summary</A>	<LI><A HREF="#Heading33">Workshop</A>	<UL>		<LI><A HREF="#Heading34">Q&amp;A</A>		<LI><A HREF="#Heading35">Quiz</A>		<LI><A HREF="#Heading36">Exercises</A>	</UL>	<LI><A HREF="#InreviewA">In Review</A></UL><P><HR SIZE="4"><CENTER><H1></H1></CENTER><P>As you know by now, components are much of what gives Delphi its power. Usingthe Form Designer, you can place a component on a form and modify its design-timeproperties. In some cases, that's all you have to do. If needed, you can also manipulatethe component at runtime by changing its properties and calling its methods. Further,each component is designed to respond to certain events. I discussed properties,methods, and events on Day 5, &quot;The Visual Component Model,&quot; so I'm notgoing to go over that again.</P><P>Today you will find out more about components. You will learn about often-usedcomponents and, as a result, learn about the Visual Component Library (VCL) classesthat represent those components. As you go through this chapter, feel free to experiment.If you read something that you want to test, by all means do so. Learning by experienceis as valuable as anything you can do, so don't be afraid to experiment.</P><P><H2><A NAME="Heading1"></A>A Review of Components</H2><P>Let's review some of what you already know about components. Before doing that,though, I want to take a moment to explain the differences between a VCL componentand a Windows control. Windows controls include components such as edit controls,list boxes, combo boxes, static controls (labels), and buttons, not to mention allthe Win32 controls. Windows controls, by nature, don't have properties, methods,and events. Instead, messages are used to tell the control what to do or to get informationfrom the control. To say that dealing with controls on this level is tedious andcumbersome would be an understatement.</P><P>A VCL component is a class that encapsulates a Windows control (not all VCL componentsencapsulate controls, though). A VCL component in effect adds properties, methods,and events to a Windows control to make working with the control easier. You mightsay that VCL takes a fresh approach to working with Windows controls. It could besaid that all VCL components are controls, but not all controls are components. AVCL Edit component, for example, is a control, but a standard Windows edit controlis not a VCL component. VCL components work with Windows controls to raise the jobof dealing with those controls to a higher level.</P><P>Given that discussion, then, I will use the terms <I>control</I> and <I>component</I>interchangeably when referring to VCL components. (But I will never call a Windowscontrol a component!)</P><P><H3><A NAME="Heading2"></A>Visual Components</H3><P>Visual components include components such as edit controls, buttons, list boxes,labels, and so on. Most components you will use in a Delphi application are visualcomponents. Visual components, as much as possible, show you at design time whatthe component will look like when the program runs.</P><P><strong>New Term:</strong> Some components are visual components; others are nonvisualcomponents. A <I>visual component</I>, as its name implies, is one that can be seenby the user at design time.</P><P><H3><A NAME="Heading3"></A>Nonvisual Components</H3><P><strong>New Term:</strong> A <I>nonvisual component</I> is one that cannot be seen bythe user at design time.</P><P>Nonvisual components work behind the scenes to perform specific programming tasks.Examples include system timers, database components, and image lists. Common dialogboxes such as File Open, File Save, Font, and so on are considered nonvisual componentsas well. (They are nonvisual because they don't show themselves at design time. Atruntime, they become visible when they are invoked.) The common dialog componentsare discussed later in the section &quot;The Common Dialog Boxes.&quot;</P><P>When you place a nonvisual component on a form, Delphi displays an icon representingthe component on the form. This icon is used to access the component at design timein order to change the component's properties, but the icon does not show up whenthe program runs. Nonvisual components have properties, methods, and events justlike visual components do.</P><P>Now let's look at some of the common properties components share.</P><P><H2><A NAME="Heading4"></A>The Name Property</H2><P>The Name property serves a vital role in components. On Day 5, &quot;The VisualComponent Model,&quot; in the section &quot;VCL Explored,&quot; I discussed someof what happens when you place a component on a form. As soon as you place a componenton a form, Delphi goes to work in the background while you ponder your next move.One thing Delphi does is create a pointer to the component and assign the Name propertyas the variable name. For example, let's say you place an Edit component on a formand change the Name property to MyEdit. At that point, Delphi places the followingin the class declaration for the form (in the published section):</P><P><PRE>MyEdit: TEdit;</PRE><P>When the application runs, Delphi creates an instance of the TEdit class and assignsit to MyEdit. You can use this pointer to access the component at runtime. To setthe text for the edit control, you would use</P><P><PRE>MyEdit.Text := `Jenna Lynn';</PRE><P>Delphi also uses the Name property when creating event-handler names. Let's saythat you want to respond to the OnChange event for an Edit component. Normally, youdouble-click the Value column in the Object Inspector next to the OnChange eventto have Delphi generate an event handler for the event. Delphi creates a defaultfunction name based on the Name property of the component and the event being handled.In this case, Delphi would generate a function called MyEditChange.</P><P>You can change the Name property at any time provided that you change it <I>only</I>via the Object Inspector. When you change a component's Name property at design time,Delphi goes through all the code that it previously generated and changes the nameof the pointer and all event-handling functions.</P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> Delphi will change all the code that it generated to reflect the	new value of the component's Name property, but it will not modify any code you wrote.	In other words, Delphi will take care of modifying the code it wrote, but it is up	to you to update and maintain the code you wrote. Generally speaking, you should	set the Name property when you initially place the component on the form and leave	it alone after that. There's no problem with changing the name at a later time, but	it might lead to more work. <HR></BLOCKQUOTE><P>Continuing with this example, if you change the Name property of the edit controlfrom MyEdit to FirstName, Delphi will change the pointer name to FirstName and theOnChange handler name to FirstNameChange. It's all done automatically; you don'thave to do anything but change the Name property and trust that Delphi will do therest of the work.</P><BLOCKQUOTE>	<P><HR><strong>CAUTION:</strong> Never change the Name property at runtime. Never manually change	a component's name (the name that Delphi assigned to the component's pointer) or	event-handler names in the Code Editor. If you perform either of these actions, Delphi	loses track of components and the results are not good, to say the least. You might	even lose the ability to load your form. The only safe way to change the Name property	of a component is through the Object Inspector. <HR></BLOCKQUOTE><P>Delphi assigns a default value to the Name property for all components placedon a form. If you place an Edit component, for example, Delphi assigns Edit1 to theName property. If you place a second Edit component on the form, Delphi will assignEdit2 to that component's Name property, and so on. You should give your componentsmeaningful names as soon as possible to avoid confusion and extra work later on.</P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> You can leave the default names for components that will never be	referenced in code. For example, if you have several label components that contain	static (unchanging) text, you can leave the default names because you won't be accessing	the components at runtime. <HR></BLOCKQUOTE><H2></H2><H2><A NAME="Heading5"></A>Important Common Properties</H2><P>All components have certain properties in common. For example, all visual componentshave Left and Top properties that determine where the component is placed on theform. Properties such as Left, Top, Height, and Width are self-explanatory, so Iwon't go over them here. A few of the common properties, however, warrant a closerlook.</P><P><H3><A NAME="Heading6"></A>The Align Property</H3><P>On Day 6, &quot;Working with the Form Designer and the Menu Designer,&quot; Idiscussed the Align and Alignment properties, so I won't go over those again in detail.Refer to Day 6 for complete information on Align. It should be noted here, however,that not all components expose the Align property at design time. A single-line editcontrol, for example, should occupy a standard height, so the features of the Alignproperty do not make sense for that type of component. As you gain experience withDelphi (and depending on the type of applications you write), you will probably relyheavily on the Align property.</P><P><H3><A NAME="Heading7"></A>The Color Property</H3><P>The Color property sets the background color for the component. (The text coloris set through the Font property.) Although the Color property is simple to use,there are a few aspects of component colors that should be addressed.</P><P>The way the Color property is handled in the Object Inspector is somewhat unique.If you click the Value column, you see the drop-down arrow button indicating thatyou can choose from a list of color values. That is certainly the case, but there'smore to it than that. If you double-click the Value column, the Color dialog boxwill be displayed. This dialog box enables you to choose a color from one of the

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -