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

📄 ch21.htm

📁 delphi自学的好教材!特别适合刚刚起步学习delphi的人员!同样对使用者具有参考价值!
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<P><H2><A NAME="Heading10"></A>Code Editor</H2><P>The Delphi 4 Code Editor has many features not found in C++Builder. In particular,the following features are found in Delphi 4:</P><UL>	<LI>Module navigation	<P>	<LI>Code completion	<P>	<LI>Class completion	<P>	<LI>Code parameters</UL><P>Module navigation is a feature specific to the Object Pascal language, so it'sno surprise that C++Builder doesn't have module navigation--but why doesn't C++Builderhave code completion and code parameters? Delphi compiles the current unit each timethe code completion or code parameters features are invoked. This is possible dueto the incredible speed of the Object Pascal compiler. C++ code takes much longerto compile, so it is not feasible to implement code completion and code parametersin C++Builder using this same design. There are alternative means of implementingthese features, so future versions of C++Builder might very well have code completionand code parameters.</P><P>C++Builder does not have class completion because it is a feature new to Delphi4. The next version of C++Builder will likely have class completion.</P><P><H3><A NAME="Heading11"></A>Code Explorer</H3><P>The Code Explorer is also new to Delphi 4, so it is not found in any version ofC++Builder. This is also a feature I expect to see in future versions of C++Builder.</P><P><H2><A NAME="Heading12"></A>VCL Enhancements</H2><P>The Delphi 4 VCL includes a few enhancements to VCL not yet found in C++Builder.In particular, these classes are new to VCL in Delphi 4:</P><UL>	<LI>TActionList	<P>	<LI>TControlBar	<P>	<LI>TDateTimePicker	<P>	<LI>TPageScroller	<P>	<LI>TSimpleMail	<P>	<LI>Additional QuickReports components: TQRTextFilter, TQRCSVFilter, TQRHTMLFilter	<P>	<LI>Additional client/server MIDAS components for DCOM connections, CORBA connections,	OLE Enterprise connections, and others</UL><P>As with many of the additions to Delphi 4, these new VCL components will almostcertainly be added to the next version of C++Builder.</P><P><H3><A NAME="Heading13"></A>C++Builder Can Compile Pascal Units</H3><P>C++Builder can compile Pascal units just as easily as it can compile C++ units.This means that you can add a Pascal source file directly to a C++Builder projectand C++Builder will compile the unit and link that unit's code into the project'sEXE. You can add a Delphi form and unit to a C++Builder project and that form willbe called just like a native C++Builder form. C++Builder has the capability to compilePascal units, but Delphi can't compile C++Builder source files. Delphi 4 can, however,create C++ object files (.obj) from Object Pascal source.</P><PRE></PRE><H3><A NAME="Heading14"></A>ActiveX Support</H3><P>Support for creating ActiveX controls is slightly different in C++Builder thanin Delphi. Primarily, C++Builder ActiveX controls make use of the ActiveX TemplateLibrary (ATL), a C++ framework for ActiveX creation.</P><P><H3><A NAME="Heading15"></A>Delphi Compiles Faster and Produces Smaller EXEs</H3><P>Delphi programs will always compile faster than C++Builder programs. Pascal compilesmuch faster than C++ because it isn't as complex. C++Builder's use of pre-compiledheaders and the incremental linker helps reduce the effects by speeding up compileand link times, but Delphi programs still compile much faster than C++Builder programs.</P><P>Along the same lines, Delphi programs are always smaller than C++Builder programs.This is because of a number of factors, but primarily because the VCL is writtenin Object Pascal. Because the VCL is written in Object Pascal, a C++Builder programhas to include both the C++ Runtime Library and the Object Pascal Runtime Library.Both C++ and Pascal use different exception handling and runtime type informationcode, which results in extra code in a C++Builder application.</P><P><H2><A NAME="Heading16"></A>Converting from Delphi to C++Builder</H2><P>At some point you might need to convert an application from Delphi to C++Builderor from C++Builder to Delphi. I have done dozens of such conversions and, althoughtime-consuming, they are not difficult. (By the way, when converting a project, Irun Delphi and C++Builder simultaneously.) In the following section I discuss convertingonly from Delphi to C++Builder. Converting the other way is simply a variation ofthe same concepts.</P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> Why discuss converting a Delphi application to C++Builder and not	the other way around? Primarily because Delphi has a larger installed base (it's	been on the market longer) and the most likely conversion is from Delphi to C++Builder.	That does not necessarily mean that developers are switching to C++Builder from Delphi.	I have done nearly 200 such conversions for TurboPower Software (my employer). In	that case, I was converting our products' example programs, which were written in	Delphi, to C++Builder. This was necessary when we began supporting our components	for use in C++Builder. <HR></BLOCKQUOTE><P>Converting a project from Delphi to C++Builder involves two basic steps. The firststep is to copy the Delphi application's forms to the C++Builder project. The secondstep is to modify the Delphi code. Let's look at these two steps in more detail.</P><PRE></PRE><H3><A NAME="Heading17"></A>Copying the Delphi Forms</H3><P>The first thing you need to do is copy the Delphi forms to the C++Builder application.A C++Builder form and a Delphi form use the same basic format, but there is at leastone significant difference that you should be aware of.</P><P>It is obvious that a form file contains the size and position of the form itselfand of every component on the form. What might not be obvious, however, is that theform file also contains information about events and event handlers. Specifically,the form file includes a description of any event handlers that have been createdfor the form and for any components on the form. In a Delphi form file, the eventscontain references to event handlers in Pascal.</P><P>In a C++Builder form file, the events point to C++ event handlers. Naturally,you will have to remove the Pascal references in order to use the form in C++Builder.It isn't important that you understand this in detail, but it is a factor you willhave to deal with when converting a Delphi form to C++Builder.</P><P>Here are the steps required to copy a Delphi main form:</P><DL>	<DT></DT>	<DD><B>1. </B>Open the project in Delphi and make note of the main form's filename	and its Name property.	<P>	<DT></DT>	<DD><B>2. </B>Switch to C++Builder and select the main form. Change the Name property	to the same name as the Delphi project's main form.	<P>	<DT></DT>	<DD><B>3. </B>Save the C++Builder project. Save the main form's unit with the same	filename the form had in Delphi (minus the .PAS extension, of course). Save the project	with the same project name as the Delphi project.	<P>	<DT></DT>	<DD><B>4. </B>Switch to Windows Explorer. Copy the main form's form file (the .DFM	file) from the Delphi project's directory to the C++Builder project's directory.	Make sure that you copy the file, not move it. Explorer will warn you that a file	with the same filename exists in the destination directory. Click Yes to overwrite	the form file in the C++Builder directory. If you don't get this warning, you have	done something wrong in saving the C++Builder unit.	<P>	<DT></DT>	<DD><B>5. </B>Switch back to C++Builder. A dialog box will be displayed that says	Module XXX.CPP's time/date has changed. Reload?. Click Yes to reload the form. When	the form reloads, it will contain the components found on the Delphi form.	<P>	<DT></DT>	<DD><B>6. </B>Make sure that the form is selected and then choose Edit|Select All	from the C++Builder main menu to select all the form's components. Now choose Edit|Cut	from the main menu, immediately followed by Edit|Paste. This step ensures that all	the declarations for the individual components are placed in the main form's header	file.	<P></DL><P>At this point you need to be certain that all references to the Delphi event handlersare removed from the C++Builder form file. Doing this is simple, as follows:</P><DL>	<DD><B>1. </B>Choose File|Save from the C++Builder main menu or click the Save File	button on the toolbar.	<DT></DT>	<DD><B>2. </B>C++Builder will display a message box for each event handler associated	with the form, as shown in Figure 21.3. Click Yes each time the dialog box is displayed	to remove all event handlers. Be aware that you might have to click Yes dozens of	times to remove all the event handlers. (I've had to remove as many as 100 event	handlers for a single form!)	<P></DL><P><A HREF="javascript:popUp('28672103.gif')"><B>FIGURE 21.3.</B></A><B> </B><I>Removingevent handlers from the form.</I></P><P>At this point, you are done copying the form itself. Now you can move on to convertingthe code.</P><P><strong>NOTE:</strong> You need to repeat this form-copying process for every form inthe Delphi application.</P><P><H3><A NAME="Heading18"></A>Converting the Code</H3><P>Converting the code from Delphi to C++Builder is much more difficult than copyingthe form. There are many ways to proceed, but I'll explain the method that I use.First, look for any variables and methods that were created by the programmer andnot by Delphi, as follows:</P><DL>	<DT></DT>	<DD><B>1. </B>Locate the main form's class declaration in the Delphi unit.	<P>	<DT></DT>	<DD><B>2. </B>Make note of any variables or methods declared in the private or public	sections.	<P>	<DT></DT>	<DD><B>3. </B>Copy any such declarations to the Clipboard. For example, part of the	Delphi class declaration might look like this:	<P></DL><BLOCKQUOTE>	<PRE>private  { Private declarations }   DrawColor : TColor;  procedure DoDrawing;  function  SetDrawColor : Integer;</PRE></BLOCKQUOTE><PRE></PRE><DL>	<DD>In this case you would copy the declarations for DrawColor, DoDrawing, and SetDrawColor.	<DT></DT>	<DD><B>4. </B>Switch back to C++Builder. Display the main form's header in the Code	Editor. Locate the private section and paste the code from the Clipboard.	<P>	<DT></DT>	<DD><B>5. </B>Convert the declarations you pasted to C++ syntax. For example, the	declarations in the example in step 3 would look like this after being converted:	<P></DL><BLOCKQUOTE>	<PRE>private:  // User declarations  TColor DrawColor;  void DoDrawing();  int SetDrawColor();</PRE></BLOCKQUOTE><PRE></PRE><DL>	<DT></DT>	<DD><B>6. </B>Switch back to Delphi. In the implementation section of the Delphi	unit, locate any methods declared in the class declaration (DoDrawing and SetDrawColor	in this example). Copy the methods in the Delphi unit to the Clipboard.	<P>	<DD><B>7. </B>Switch to C++Builder. Paste the methods copied in step 6 from the Clipboard	to the form's source unit. Convert the first line of the methods (the method header)	from Pascal syntax to C++ syntax. Don't worry about the function bodies or the begin	and end statements. You'll fix that later.</DL><P><H4>Copying the Event Handlers</H4><P>The next step is to copy the code for the event handlers in the Delphi unit tothe C++Builder unit. The best way I have found to do this is to start at the topof the implementation section in the Delphi unit and work down. Here are the stepsto take when you encounter an event handler in the Delphi code:</P><DL>	<DT></DT>	<DD><B>1. </B>Make note of the event handler's name. If the name is Button1Click,	you know that the event handler is for the OnClick event of the component named Button1.	<P>	<DT></DT>	<DD><B>2. </B>Copy the code inside the event handler to the Clipboard.	<P>	<DT></DT>	<DD><B>3. </B>Switch to C++Builder. Generate an event handler that matches the event	handler from which you copied the code.	<P>	<DT></DT>	<DD><B>4. </B>Paste the Delphi code from the Clipboard into the event handler.	<P></DL><P>Repeat steps 1 through 4 for every event handler in the Delphi unit. When youare done, you will have several event handlers in your C++Builder project.</P><P><H4>Using the Replace Text Dialog Box</H4><P>The event handlers in the Delphi form file contain Pascal code, so you need toconvert that code to C++. Fortunately, you can do a lot of the conversion with C++Builder'sReplace Text dialog box. Table 21.2 lists the Pascal syntax you are searching forand the C++ syntax that is the replacement text. Spaces in the search or replacetext are denoted by a small dot. In most cases, you will want to perform the Findand Replace operations in the order listed in Table 21.2.</P><PRE></PRE><H4>TABLE 21.2. FIND AND REPLACE TEXT WHEN CONVERTING FROM DELPHI TO C++BUILDER.</H4><P><TABLE BORDER="1">	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT"><I>Description</I></TD>		<TD ALIGN="LEFT"><I>Find Text</I></TD>		<TD ALIGN="LEFT"><I>Replace Text</I></TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Equality operator</TD>		<TD ALIGN="LEFT">[lozenge]=[lozenge]</TD>		<TD ALIGN="LEFT">[lozenge]==[lozenge]</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Assignment operator</TD>		<TD ALIGN="LEFT">:=</TD>		<TD ALIGN="LEFT">=</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Inequality operator</TD>		<TD ALIGN="LEFT">&lt;&gt;</TD>		<TD ALIGN="LEFT">!=</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Membership operator</TD>		<TD ALIGN="LEFT">.</TD>		<TD ALIGN="LEFT">-&gt;</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">String quote</TD>		<TD ALIGN="LEFT">`</TD>		<TD ALIGN="LEFT">&quot;</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Begin comment</TD>		<TD ALIGN="LEFT">{</TD>		<TD ALIGN="LEFT">//</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">End comment</TD>		<TD ALIGN="LEFT">}</TD>		<TD ALIGN="LEFT">(Nothing)</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Pascal True keyword</TD>		<TD ALIGN="LEFT">True</TD>		<TD ALIGN="LEFT">true</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">

⌨️ 快捷键说明

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