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

📄 ch21.htm

📁 delphi自学的好教材!特别适合刚刚起步学习delphi的人员!同样对使用者具有参考价值!
💻 HTM
📖 第 1 页 / 共 3 页
字号:
		<TD ALIGN="LEFT">Pascal False keyword</TD>		<TD ALIGN="LEFT">False</TD>		<TD ALIGN="LEFT">false</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">if statement</TD>		<TD ALIGN="LEFT">if[lozenge]</TD>		<TD ALIGN="LEFT">if[lozenge](</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Start of block</TD>		<TD ALIGN="LEFT">begin</TD>		<TD ALIGN="LEFT">{</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">End of block</TD>		<TD ALIGN="LEFT">end;</TD>		<TD ALIGN="LEFT">}</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">End of block (form 2)</TD>		<TD ALIGN="LEFT">end</TD>		<TD ALIGN="LEFT">}</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Pascal then statement</TD>		<TD ALIGN="LEFT">then</TD>		<TD ALIGN="LEFT">)[lozenge]</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Pascal do statement</TD>		<TD ALIGN="LEFT">[lozenge]do[lozenge]</TD>		<TD ALIGN="LEFT">(Nothing)</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Pascal not statement</TD>		<TD ALIGN="LEFT">not[lozenge]</TD>		<TD ALIGN="LEFT">!</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Pascal nil keyword</TD>		<TD ALIGN="LEFT">nil</TD>		<TD ALIGN="LEFT">NULL</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Pascal case statement</TD>		<TD ALIGN="LEFT">case[lozenge]</TD>		<TD ALIGN="LEFT">switch[lozenge](</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Pascal case statement</TD>		<TD ALIGN="LEFT">[lozenge]of[lozenge]</TD>		<TD ALIGN="LEFT">)[lozenge]{</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Pascal Self keyword</TD>		<TD ALIGN="LEFT">Self</TD>		<TD ALIGN="LEFT">this</TD>	</TR></TABLE></P><P>When performing the Find and Replace, you should use the Replace All option, butyou need to take care when doing so. For example, you don't want to replace all occurrencesof a period with -&gt; starting at the top of the file because the first severallines of every C++Builder source unit contain include statements with filenames.</P><P>Make sure that if you replace Pascal comments (which begin with { and end with}) with C++ comments, you do so <I>before </I>replacing the begin and end statements.Also, when replacing words such as end, you should turn on the Whole Words Only optionin the Replace Text dialog box. This ensures that you don't accidentally replaceindividual characters within longer words. Be aware that some of your Find and Replaceoperations could have undesirable side effects (such as replacing the period thatseparates a filename and its extension with -&gt;).</P><BLOCKQUOTE>	<P><HR><strong>TIP:</strong> Consider writing a Microsoft Word macro that performs the Find and	Replace operations shown in Table 21.2. It can save you a great deal of time if you	have a lot of units to convert. <HR></BLOCKQUOTE><PRE>After you have performed the Find and Replace operations, you will have a file that is a mixture of Pascal and C++. The easy part is finished, and now you must go to work converting the rest of the file by hand. You have to know enough about each language to know how to convert Pascal syntax to C++ syntax. You're on your own from this point on, but I can point out a few issues to be aware of as you convert the rest of the file.</PRE><H4>The Pascal with Statement</H4><P>First, there is no C++ equivalent to the Pascal with statement. Take this code,for example:</P><P><PRE>with MyForm do begin  Width   := 200;  Height  := 500;  Caption := `Hello there';end;</PRE><P>When you convert this code to C++Builder, you have to specifically reference eachproperty:</P><P><PRE>MyForm-&gt;Width = 200;MyForm-&gt;Height = 500;MyForm-&gt;Caption = &quot;Hello there&quot;;</PRE><H3><A NAME="Heading19"></A>The Pascal as Statement</H3><P>Another Pascal statement that requires some work to convert is the as statement.You frequently see code like this in Delphi programs:</P><P><PRE>with Sender as TButton do  Click;</PRE><P>In C++Builder, this code is translated as follows:</P><P><PRE>TButton* button = dynamic_cast&lt;TButton*&gt;(Sender);if (button)  button-&gt;Click();</PRE><H4>Dealing with Strings</H4><P>Another area that requires special attention is string handling. Pascal has stringmanipulation functions that operate on the string data type. C++Builder, on the otherhand, has the AnsiString class, which has its own string manipulation functions.Take, for example, this Pascal code:</P><P><H4>X := STRTOINT(EDIT.TEXT);</H4><P>This code is translated to C++Builder code like this:</P><P><PRE>X = Edit-&gt;Text.ToInt();</PRE><H4>Converting Sets</H4><P>As with strings, the C++Builder equivalent to Pascal sets is a C++ class. In thecase of sets, the C++ class is called Set. The following code examples illustrateconversion of Pascal set syntax to C++Builder's Set class. I used the Font property'sStyle member to illustrate. First look at the Pascal code:</P><P><PRE>{Clear the set.} Font.Style := [];{ Add the bold and italics styles. } Font.Style := Font.Style + [fsBold, fsItalic];{ Remove the italics style. } Font.Style := Font.Style - [fsItalic];{ See if the set contains the fsBold style. } if fsBold in Font.Style then   { Code here if the font is bold. } </PRE><P>Here's the C++Builder equivalent to this code:</P><P><PRE>// Clear the set.</PRE><PRE>Font-&gt;Style.Clear();// Add the bold and italics styles.Font-&gt;Style = Font-&gt;Style &lt;&lt; fsBold &lt;&lt; fsItalic;// Remove the italics style.Font-&gt;Style = Font-&gt;Style &gt;&gt; fsItalic;// See if the set contains the fsBold style.if (Font-&gt;Style.Contains(fsBold))  // Code here if the font is bold.</PRE><P>I can't explain every difference between Object Pascal and C++ in this chapter,but these few examples should be enough to get you started. If you are using C++Builder,you should check out my C++Builder book, <I>Sams Teach Yourself Borland C++Builder3 in 21 Days</I> (ISBN 0-672-31266-2). That book explains some of the differencesbetween Object Pascal and C++ in detail.</P><P><H3><A NAME="Heading20"></A>Reusing Forms</H3><P>You don't have to convert Delphi forms to C++ at all if you don't want to. Youcan use Delphi forms in C++Builder just as they are. Simply add the .PAS file forthe form directly to your C++Builder project. C++Builder will create a header forthe Delphi unit that you can use in any C++Builder units that reference the Delphiform.</P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> Although you can add a Delphi form to a C++Builder project, you cannot	edit the form with the C++Builder Form Designer. Any modifications that you want	to make to the form visually must be made from the Delphi IDE. You can, however,	edit the form as text from within the C++Builder IDE. Choose the View As Text option	from the C++Builder Form Designer's context menu to edit the form in text format.	<HR></BLOCKQUOTE><H2><A NAME="Heading21"></A>Summary</H2><PRE>Delphi and C++Builder are not so much competing products as they are complementary products. If you know how to program with C++Builder, learning Delphi is relatively easy. Moving from Delphi to C++Builder isn't quite as easy because of the complexity of the C++ language, but if you decide to move from Delphi to C++Builder, you can at least be assured that you won't have to learn a new framework. Without question, being proficient in both Delphi and C++Builder makes you a more valuable programmer.</PRE><H2><A NAME="Heading22"></A>Workshop</H2><P>The Workshop contains quiz questions to help you solidify your understanding ofthe material covered and exercises to provide you with experience using what youhave learned. You can find the answers in Appendix A, &quot;Answers to the Quiz Questions.&quot;</P><P><H3><A NAME="Heading23"></A>Q&amp;A</H3><DL>	<DT></DT>	<DD><B>Q Can I use Pascal units in a C++Builder project?</B>	<P>	<DT></DT>	<DD><B>A</B> Yes. Add the Pascal unit to your project just as you would add a C++	unit. Be sure to put the Pascal units above any C++ units that reference code in	the Pascal units in the Project Manager.	<P>	<DT></DT>	<DD><B>Q Can I use C++ units in my Delphi projects?</B>	<P>	<DT></DT>	<DD><B>A</B> No. You can use Pascal units in C++Builder but not the other way around.	<P>	<DT></DT>	<DD><B>Q As a programmer, I'm curious about something: Are the Delphi and C++Builder	IDEs built from the same code base?</B>	<P>	<DT></DT>	<DD><B>A</B> Yes. Although Delphi and C++Builder have obvious differences, they have	many similarities, so Borland uses a single code base for both IDEs.	<P>	<DT></DT>	<DD><B>Q Why do my Delphi projects compile so much faster than my C++Builder projects?</B>	<P>	<DT></DT>	<DD><B>A</B> Because Object Pascal is less complex than C++ and thus compiles faster.	<P>	<DT></DT>	<DD><B>Q I have heard that if I know C++Builder, learning Delphi is simple. Is that	true?</B>	<P>	<DT></DT>	<DD><B>A</B> Not exactly, no. Despite stereotypes regarding Pascal, Delphi's Object	Pascal is relatively complex. Although going from C++Builder to Delphi is easier	than the reverse, it is still not something that should be approached lightly.	<P></DL><H2><A NAME="Heading24"></A>Quiz</H2><DL>	<DT></DT>	<DD><B>1. </B>Do Delphi and C++Builder project files have the same filename extension?	<P>	<DT></DT>	<DD><B>2. </B>Do Delphi and C++Builder form files have the same filename extension?	<P>	<DT></DT>	<DD><B>3. </B>Can you use packages from third-party component vendors in both Delphi	and C++Builder?	<P>	<DT></DT>	<DD><B>4. </B>Can you open a Delphi form file in C++Builder?	<P>	<DT></DT>	<DD><B>5. </B>Can you edit a Delphi form file using the C++Builder Form Designer?	<P>	<DT></DT>	<DD><B>6. </B>Can you use a C++Builder source unit in Delphi?	<P>	<DT></DT>	<DD><B>7. </B>Which is better, Delphi or C++Builder?	<P></DL><H3><A NAME="Heading25"></A>Exercises</H3><DL>	<DT></DT>	<DD><B>1. </B>If you have C++Builder, take an example from the Delphi Demos directory	and convert it to C++Builder.	<P>	<DT></DT>	<DD><B>2. </B>Take an example in the C++Builder Examples directory and convert it	to Delphi.	<P>	<DD><B>3. </B>Take a break, you've finished your 21st day!</DL><H1><A NAME="InReview"></A>In Review</H1><P>This was a pretty productive week, wasn't it? Unlike Visual Component Library(VCL) components, COM and ActiveX controls can be used by a wide variety of programmingenvironments. The good news is that Delphi gives you the fastest route to ActiveXcreation of any programming environment available today.</P><P>You probably had no idea that database operations could be so easy. Easy is arelative thing. I wouldn't go so far as to say that database programming is eversimple, but Delphi certainly makes it easier than it would be in other programmingenvironments.</P><P>On Day 19, you learned about dynamic link libraries (DLLs). Regardless of whetheryou decide to use DLLs right away, you can at least see their benefits and can makean informed decision regarding their use in your programs. If you plan on callingDelphi forms from programs not written in Delphi, you will have to use DLLs. DLLsare not difficult, but here again, a little experience goes a long way.</P><P>On Day 20, you learned about creating your own components. You either loved itor you were left scratching your head. If the latter is the case, don't worry aboutit. You might never have to write your own components. There are plenty of sourcesfor good, commercial-quality components. (See Appendix B, &quot;Delphi Internet Resources,&quot;for a few.) Plus, you can always go back and tackle Day 20 again after you've accumulatedsome time in your Delphi log book. If you enjoyed learning about writing components,it was probably difficult for you to read the last chapter! I'd be willing to betthat at some time during Day 20 you could have been overheard saying, &quot;Wow!&quot;Writing your own components is rewarding--no question. I had time only to scratchthe surface of covering how to write components. There is a lot more to learn, andmuch of that can be learned by experience only. This chapter gives you enough toget you started.</P><P>Finally, you learned about Delphi and C++Builder and how they can work together.Delphi and C++Builder are two great products. If you know how to use one of theseprogramming environments, learning the other is very easy. The more you know, themore valuable you are as a programmer.</P><DL>	<DD></DL><H1></H1><CENTER><P><HR><A HREF="../ch20/ch20.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../apa/apa.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></P><P>&#169; <A HREF="../copy.htm">Copyright</A>, Macmillan Computer Publishing. Allrights reserved.</CENTER></BODY></HTML>

⌨️ 快捷键说明

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