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

📄 ch07.htm

📁 delphi自学的好教材!特别适合刚刚起步学习delphi的人员!同样对使用者具有参考价值!
💻 HTM
📖 第 1 页 / 共 5 页
字号:
predefined colors or to create your own colors by clicking the Define Custom Colorsbutton. Figure 7.1 shows the Color dialog box after the Define Custom Colors buttonhas been clicked.</P><P><A HREF="javascript:popUp('28670701.gif')"><B>FIGURE 7.1.</B></A><B> </B><I>TheColor dialog box.</I></P><P><strong>NOTE:</strong> This is the same Color dialog box that will be displayed if youimplement the ColorDialog component in your application.</P><P>If you choose a color from the Color dialog box, you see that the value of theColor property changes to a hexadecimal string. This string represents the red, green,and blue (RGB) values that make up the color. If you know the exact RGB value ofa color, you can type it in (not likely!).</P><P>Most of the time you will probably choose a color from the list of color valuesprovided. When you click the drop-down button to display the list of possible values,you will see what essentially amounts to two groups of values. The first group ofcolors begins with clBlack and ends with clWhite. These are the Delphi predefinedcolors; this list represents the most commonly used colors. To choose one of thelisted colors, simply click the color in the list. If you can't find a color in thelist that suits your needs, you can invoke the Color dialog box as discussed.</P><P>The second group of colors in the list begins with clScrollBar. This group ofcolors represents the Windows system colors. If you use colors from this list, yourapplication will automatically adjust its colors when the user changes color schemesin Windows. If you want your application to follow the color scheme the user haschosen for his or her system, you should choose colors from this list rather thanfrom the first list.</P><P>Use of color should be carefully considered. Proper use of color provides an aestheticallypleasing environment for the user. Abuse of colors makes for an obnoxious applicationthat is annoying to use. Color is like a magnet to new programmers. It is commonto want to throw lots of colors on a form because it's fun and easy, but don't getcaught up in the fun at the expense of your users.</P><P><H3><A NAME="Heading8"></A>The Cursor Property</H3><P>The Cursor property controls the cursor that is displayed when the user movesthe mouse cursor over the component. Windows automatically changes cursors for somecomponents. For example, Windows changes the cursor to an I-beam when the cursoris moved over an Edit, Memo, or RichEdit component.</P><P>To let Windows manage the cursor, leave the Cursor property set to crDefault.If you have specialized windows (components), you can specify one of the other cursors.When the mouse is moved over that component, Windows will change the cursor to theone you specified.</P><P>Frequently, you will need to change cursors at runtime. A long process, for example,should be indicated to the user by displaying the hourglass cursor. When you resetthe cursor, you need to be sure to set the cursor back to whatever it was originally.The following code snippet illustrates this concept:</P><P><PRE>var  OldCursor : TCursor;begin  OldCursor := Screen.Cursor;  Screen.Cursor := crHourGlass;  { do some stuff which takes a long time }  Screen.Cursor := OldCursor;end;</PRE><P>This ensures that the cursor that was originally set for the application is properlyrestored.</P><P>Another cursor property, DragCursor, is used to set the cursor that is used whenthe mouse cursor is over a component that supports drag-and-drop. As with colors,you should be prudent in your use of cursors. Use custom cursors when needed, butdon't overdo it.</P><P><H3><A NAME="Heading9"></A>The Enabled Property</H3><P>Components can be enabled or disabled through the Enabled property. When a componentis disabled, it cannot accept focus (clicking on it has no effect), and usually itgives some visual cue to indicate that it is disabled. In the case of buttons, forexample, the button text is grayed out as is any bitmap on the button. Enabled isa Boolean property: Set it to True to enable the component or set it to False todisable the component. Enabling and disabling windows (remember that windowed componentsare windows, too) is a feature of Windows itself.</P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> Some components show their disabled state at design time, but most	don't. The BitBtn component is one that does show its disabled state at design time.	<HR></BLOCKQUOTE><P>The Enabled property applies mostly to windowed components, but it can apply tonon-windowed components as well. The SpeedButton component is an example of a non-windowedcomponent that can be disabled.</P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> Modifying the Enabled property for a Panel component has additional	implications. Panels are often used as containers for other controls. Therefore,	a panel becomes the parent of the controls that are placed on the panel. If you disable	a panel, the components on the panel will not show as disabled, but they will not	function because their parent (the panel) is disabled. <HR></BLOCKQUOTE><P>Although components can be disabled at design time, enabling and disabling componentsis something that is usually done at runtime. Menu items, for example, should beenabled or disabled according to whether they apply at a given time. The same istrue of buttons. There are a variety of reasons why you might want to disable othertypes of controls as well.</P><P>To disable a component at runtime, just assign False to its Enabled property,and to enable a component assign True to Enabled. The following code snippet enablesor disables a menu item based on some condition:</P><P><PRE>if CanSave then  FileSave.Enabled := Trueelse   FileSave.Enabled := False;</PRE><P>This process is often referred to as <I>command enabling</I> and is an importantpart of a professional-looking Windows program.</P><BLOCKQUOTE>	<P><HR><strong>TIP:</strong> The TActionList component can be used to enable or disable a component	or groups of components. TActionList is discussed in detail on Day 13, &quot;Beyond	the Basics,&quot; in the section &quot;Command Enabling.&quot; <HR></BLOCKQUOTE><P><H3><A NAME="Heading10"></A>The Font Property</H3><P>The Font property is a major property and therefore needs to be included here,but there is not a lot that needs to be said about it. The Font property is an instanceof the TFont class and, as such, has its own properties. You can set the Font propertiesby double-clicking on the font name in the Object Inspector (which will expand theFont node and show the Font properties) or by invoking the Font dialog box. (TheFont dialog box is discussed in more detail later in this chapter in the section&quot;The Font Dialog Box.&quot;) Figure 7.2 shows the Object Inspector with theFont property node expanded to reveal the TFont properties.</P><P><A HREF="javascript:popUp('28670702.gif')"><B>FIGURE 7.2.</B></A><B> </B><I>TheObject Inspector showing the Font property.</I></P><P>The Color property sets the color of the font, and the Name property enables youto choose the typeface for the font.</P><P>The Height and Size properties of TFont deserve special mention as well:</P><UL>	<LI>The Height property is used to specify the height of the font in pixels.	<P>	<LI>The Size property is used to specify the height of the font in points.</UL><P>When you change one of these properties, the other will change automatically.The Height is often specified as a negative number. Refer to the online help forTFont for an explanation of why this is the case.</P><P>The Pitch property is not particularly useful. I'll explain it in just a moment,but first a quick tutorial on fonts. A font can be either proportionally spaced orfixed space:</P><UL>	<LI>Most fonts are <I>proportionally spaced</I>, which means that each letter only	takes as much space as needed. For example, an uppercase M takes up much more space	than a lowercase i. Take a look at the letters in this book and you will see what	I mean. Examples of proportional fonts include Times New Roman, Arial, and Bookman.	<P>	<LI>With a <I>fixed space </I>font (typically called a fixed-pitch font), on the	other hand, all characters take exactly the same amount of space. This is convenient	for windows such as code editors (the Delphi Code Editor, for example) or any other	window where a fixed-pitch font is desired. Courier New is probably the most commonly	used fixed-pitch font, although Fixedsys is the Windows fixed-pitch font of choice	in some Windows applications. <I>Fixed space </I>fonts are harder to read, so they	aren't normally used for long blocks of text other than code.</UL><P>In theory, the Pitch property can be used to force a proportionally spaced fontto fixed space and vice versa. The problem is that Windows might perform font substitutionsto carry out the conversion. In other words, you really don't know what you mightget. It is far better to pick exactly the font you require than to rely on the Pitchproperty.</P><PRE></PRE><P>Finally, the Style property of TFont can be used to toggle bold, italic, underline,or strikethrough. These styles are not mutually exclusive, so you can mix stylesin any way you choose.</P><BLOCKQUOTE>	<P><HR><strong>TIP:</strong> Although you can use the Object Inspector to change font properties,	the Font dialog box (invoked when you click the ellipsis button next to the Font	property) has the added benefit of showing you a sample of what the font looks like	as you choose different font options. To simply change the font's Style property	or Size property, use the Object Inspector. But if you are looking for just the right	font, the Font dialog box is a better choice. <HR></BLOCKQUOTE><H3></H3><H3><A NAME="Heading11"></A>The Hint Property</H3><P>The Hint property is used to set hint text for a component. The hint text hastwo parts. The first part is sometimes called the <I>short hint</I>. This is thehint text that is displayed when the user places the cursor over the component andpauses. The pop-up window that displays the hint text is called a <I>tooltip</I>.</P><P>The second part of the hint text is sometimes called the <I>long hint</I>. Thelong hint is the optional hint text that shows in the status bar when the user movesthe mouse cursor over the component. The short and long hint texts are separatedby a pipe (|). For example, to specify both the short hint text and the long hinttext for a File Open speed button, you would enter the following for the Hint property:</P><P><PRE>File Open|Open a file for editing</PRE><P>In order for short hints to show, you must have the Application object's ShowHintproperty set to True (the default) as well as the component's ShowHint property.Displaying the long hint in the status bar requires a little more work, so I'll savethat discussion for tomorrow.</P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> You can specify the short hint text, the long hint text, or both.	You can use the pipe to tell Delphi which hint text you are supplying. If you don't	use the pipe, both the short hint and the long hint will use the same text. <HR></P>	<P><HR><strong>NOTE:</strong> Although there are no limits on the length of either the long hint	or the short hint, you should keep each hint's use in mind when you create them.	Short hints should probably be limited to 30 characters or fewer. Long hints can	be more descriptive, but keep in mind that long hints that are very long will be	truncated when displayed in the status bar. <HR></BLOCKQUOTE><H3></H3><H3><A NAME="Heading12"></A>The ParentColor, ParentCtl3D, ParentFont, and ParentShowHintProperties</H3><P>The ParentColor, ParentCtl3D, ParentFont, and ParentShowHint properties work thesame way, so I'll discuss them at the same time. When these properties are set toTrue, the component takes its Color, Ctl3D, Font, or ShowHint settings from its parent.For example, for most components the ParentFont property is set to True by default.This means the component will inherit the font that its parent is currently using.To better understand this, do this exercise:</P><DL>	<DT></DT>	<DD><B>1. </B>Create a blank form. Set the Font property's Size property to 16.	<P>	<DT></DT>	<DD><B>2. </B>Place a Label component on the form. Notice that the label automatically	uses the 16-point font.	<P>	<DT></DT>	<DD><B>3. </B>Place a Button component on the form. It also uses the 16-point font.	<P></DL><P>You can set this property to False, but by the time the component is placed itis already too late and you will have to change the font manually to the font youwant for the component.</P><P><H3><A NAME="Heading13"></A>The Tag Property</H3><P>The Tag property is nothing more than a four-byte variable set aside for youruse. You can use the Tag property to store any data that your component might need.The data stored might be a pointer to another class, an index value, or any numberof other possibilities. Using the Tag property would probably be considered an advancedprogramming technique.</P><P><H3><A NAME="Heading14"></A>Other Common Properties</H3><P>Table 7.1 lists other common properties that are frequently used. These propertiesdon't require as much explanation, so they are listed here for your reference. Notall components have each of the properties listed.</P><P><H4>TABLE 7.1. ADDITIONAL COMPONENT PROPERTIES.</H4><P><TABLE BORDER="1">	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT"><I>Property</I></TD>		<TD ALIGN="LEFT"><I>Description</I></TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">BorderStyle</TD>		<TD ALIGN="LEFT">Can be bsSingle or bsNone. Use bsNone when you want the component to blend in with			the background.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">

⌨️ 快捷键说明

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