📄 apc.htm
字号:
<DT></DT> <DD><B>1. </B>Choose Component | Import ActiveX Control from the main menu. The Import ActiveX dialog box is displayed. <P> <DT></DT> <DD><B>2. </B>Locate Microsoft Internet Controls (Version 1.<I>x</I>) in the list of ActiveX controls (see Figure BD.4). If you have Internet Explorer 3 installed, the version number will be 1.0. If you have Internet Explorer 4 installed, the control will be listed as version 1.1. Notice that the Class names field shows TWebBrowser as the control contained in this file. If you have Internet Explorer 4, the Class names field will also show TWebBrowser_V1 (the original WebBrowser control) and TShellFolderViewOC. <P></DL><P><A HREF="javascript:popUp('28672204.gif')"><B>FIGURE BD.4.</B></A><B> </B><I>TheImport ActiveX dialog box.</I></P><DL> <DT><I></I></DT> <P> <DT><I></I></DT> <DD><B>3. </B>Click Install to install the control (the rest of the fields on this dialog box can be left on their default values). <P> <DT></DT> <DD><B>4. </B>The Install dialog box appears asking for a package name. Click on the Into new package tab at the top of the dialog and type IE in the File name field. (You can type a description if you want, but it's not necessary.) Click OK to create the package. <P> <DT></DT> <DD><B>5. </B>A confirmation dialog box appears asking whether you want to build and install the package. Click Yes to create the package. <P></DL><P>Delphi builds the package and then displays a dialog box saying that the TWebBrowsercontrol has been installed. Now you can try out the control:</P><DL> <DT></DT> <DD><B>1. </B>First, choose File | Close All to close all windows, and then create a new application. <P> <DT></DT> <DD><B>2. </B>Click the ActiveX tab on the Component palette. Choose the WebBrowser control and drop it on your main form. Size the control as desired, but leave room on the form for a button. <P> <DT></DT> <DD><B>3. </B>Drop a Button component on the form. Double-click the button to generate an OnClick event handler. Type the following code in the event handler (use any URL you want): <P></DL><BLOCKQUOTE> <PRE>WebBrowser1.Navigate(`http:\\www.turbopower.com', EmptyParam, EmptyParam, EmptyParam, EmptyParam););</PRE></BLOCKQUOTE><PRE></PRE><DL> <DT></DT> <DD>As you might surmise, the Navigate method loads the document in the Web browser. <P> <DT></DT> <DD><B>4. </B>Click the Run button to run the program. <P></DL><P>When the program runs, click the form's button. The Web page will load and bedisplayed in the WebBrowser control.</P><P>When you install this control, Delphi creates a unit called SHDocVw_TLB.pas. Youcan look at this unit to see what properties and methods are available for TWebBrowser.</P><BLOCKQUOTE> <P><HR><strong>TIP:</strong> You can find documentation for the WebBrowser control on Microsoft's Web site. Search the Microsoft site for the text Reusing the WebBrowser Control. <HR></BLOCKQUOTE><P>Notice that you can't redistribute the TWebBrowser control itself without obtaininga license from Microsoft. However, if you know your users have Internet Explorerinstalled on their systems, your application will work because the control is alreadyinstalled. You still have to register the control on your user's machine for yourapplication to run. See "Deploying Internet Applications" later in thischapter for more information on registering ActiveX controls.</P><P><H2><A NAME="Heading8"></A>Sending Mail</H2><P>There are many reasons you might want to send mail from one of your applications.The good news is that sending email isn't difficult at all. Maybe you want your programusers to be able to email you with any problems they encounter. In that case, yourapplication could pop up a form containing a Memo component and a Send button. Yourusers can type text in the Memo component, press the Send button, and the messageis emailed to you. You can even go so far as to attach a log file from your applicationto diagnose any problems the user is having.</P><P>The TNMSMTP control is used for sending mail through an SMTP server. SMTP is anodd protocol in that it doesn't require authenticated logon to the server (at leaston most SMTP servers). You can simply connect to any mail server, send the emailmessage, and disconnect. The Host property is used to specify the host name of themail server to which you want to connect. Most of the time, you can just use mailas the host name. Specifying mail tells the TNMSMTP control to connect to your localmail server, whether it's your ISP's mail server or your company's mail server.</P><P>If you want, you can specifically set the mail server name (such as mail.mycompany.com),but it isn't usually necessary. If you specify an incorrect mail server, you getan ESockError exception. The Port property is used to specify the port on which toconnect. The default SMTP port is port 25. The Port property defaults to a valueof 25, so you shouldn't have to change this property.</P><P>All the pertinent information for the mail message itself is contained in thePostMessage property. This property is a class that contains properties such as ToAddress,FromAddress, Subject, Body, and so on. You fill in the appropriate fields of thePostMessage property and then send the message.</P><P>Before you can send a mail message, you need to connect to the SMTP server. Thisis done with the Connect method:</P><P><PRE>SMTP.Host := `mail';SMTP.Connect;</PRE><P>After you're connected, you can send the email. The OnConnect event handler isa good place for this code because you know that you are connected to an SMTP server.For example:</P><P><PRE>procedure TMainForm.SMTPConnect(Sender: TObject);begin with SMTP.PostMessage do begin FromAddress := `bilbo@baggins.com'; ToAddress.Add(`gandolf@baggins.com'); Subject := `Test'; Body.Add(`This is a test'); end; SMTP.SendMail;end;</PRE><P>This code sets up the FromAddress, ToAddress, Subject, and Body parameters ofthe PostMessage property and then sends the message with the SendMail method. It'sas simple as that. Notice that the ToAddress and Body properties of PostMessage areTStringLists. The mail message body can contain several lines of text, so it's notsurprising that the Body property is a TStringList. The ToAddress property is alsoa TStringList, so you can specify several recipients for the message.</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> The FromAddress and ToAddress fields are required fields. All other fields (even the message body) are optional. <HR></BLOCKQUOTE><P>After you know the message has been sent successfully, you can disconnect fromthe SMTP server. The OnSuccess event is generated when the mail has been sent. YourOnSuccess event handler might be as simple as the following:</P><DL> <DD>void __fastcall tform1.smtpsuccess(tobject *sender then<BR> begin<BR> SMTP.Disconnect;<BR> end;</DL><PRE></PRE><P>You can send several messages per connection, of course. If you have several messagesto send, you don't have to disconnect from the server and reconnect for each message.Just connect once, send all your messages, and then disconnect from the server.</P><P>Your mail message might go through without incident, or it might fail in one wayor another. In either case, you must be prepared to disconnect from the server. TheOnFailure event is generated if the mail message fails to go through, so you canuse that event to disconnect from the server as well as the OnSuccess event. Thebook's code, found at http://www.mcp.com/info, contains a simple mail program thatillustrates how to send mail with TNMSMTP.</P><P><H2><A NAME="Heading9"></A>Deploying Internet Applications</H2><P>If your Internet application uses only the VCL Internet components, there is nothingspecial to do when you deploy your application unless you are using runtime packages.If you are using runtime packages, you need to ship INET40.BPL, and if you are usingthe page producer components, ship INETDB40.BPL as well.</P><P>Deploying an application that uses ActiveX controls, however, requires more work.ActiveX controls must be registered on the machine on which your application willrun. The easiest way to register ActiveX controls is with a good installation program.InstallShield Express comes with Delphi Professional and Client/Server versions,so you should try that program. Another good installation program is Wise Installfrom Great Lakes Business Solutions. The better installation programs register, aspart of the installation process, the ActiveX controls that your application uses.</P><P>If you don't use a commercial installation program, you have to manually registerany ActiveX controls your application uses. The TREGSVR.EXE utility is used to registerand unregister ActiveX and OCX controls. This utility is in your Delphi 4\Bin directory.For example, to install the EZ Web Browser application you created earlier today,you ship the following files:</P><P><PRE>HTML.OCXNMOCOD.DLLNMSCKN.DLLNWM3VWN.DLLNMORENU.DLLWEBBROWS.EXE</PRE><P>After you install all these files, you have to run TREGSVR.EXE on the HTML.OCXand NMOCOD.DLL files to register them. From the command line, you type</P><P><PRE>TREGSVR HTML.OCX</PRE><P>Do the same for NMOCOD.DLL. The THTML control is now registered on your user'smachine and your program will run as intended.</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> If you don't register your ActiveX controls properly, your users will see a message box that says, "Class not registered." when they attempt to run your program. Your application will then terminate, leaving your users wondering what went wrong. <HR></BLOCKQUOTE><P>To unregister a control, use the /u switch as follows:</P><P><PRE>TREGSVR /u HTML.OCX</PRE><P>Here again, a good installation program will have an uninstall option that takescare of this for you.</P><P>As you can see, ActiveX controls require a bit of work to install after your applicationis built. If you aren't aware of the fact that you need to register the ActiveX controls,it can lead to confusion for both you and your users. By the way, the files neededto deploy an application using the THTML control total about 900KB, so using ActiveXcontrols can be expensive in terms of disk space. I prefer to use native VCL controlswhenever possible for exactly this reason.</P><P><H2><A NAME="Heading10"></A>Summary</H2><P>Today you learned about the Internet components provided in Delphi. You builta simple but usable Web browser with the THTML control, and you learned how to sendmail using the TNMSMTP control. Internet programming is big business right now. Itcertainly can't hurt to have some Internet programming experience.</P><P><H2><A NAME="Heading11"></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 in using what youhave learned. You can find the answers to the quiz questions in Appendix A, "Answersto the Quiz Questions."</P><P><H3><A NAME="Heading12"></A>Q&A</H3><DL> <DT></DT> <DD><B>Q What components or controls should I use to create a TCP/IP client/server application?</B> <P> <DT></DT> <DD><B>A </B>Use the TClientSocket and TServerSocket components. <P> <DT></DT> <DD><B>Q Can I create Web pages from my database tables?</B> <P> <DT></DT> <DD><B>A </B>Yes. The TQueryTableProducer and TDataSetTableProducer components create an HTML document from a database table. You can learn more about these components by examining the sample projects in the Delphi 4\Demos\Webserv directory. <P> <DT></DT> <DD><B>Q The TNMSMTP control is for receiving mail and the TNMPOP3 control is for retrieving mail. Why are there two controls for email operations?</B> <P> <DT></DT> <DD><B>A </B>There are two controls because there are two separate mail protocols: one for sending mail (SMTP) and one for retrieving mail (POP3). <P> <DT></DT> <DD><B>Q When were most of the Internet protocols (SMTP, POP3, FTP, UDP, and so on) defined?</B> <P> <DT></DT> <DD><B>A </B>You might be surprised to learn that many protocols used in Internet programming have been around for 20 years or more even though the Internet is much younger than that. The Internet protocols were originally designed for the UNIX platform. <P></DL><H3><A NAME="Heading13"></A>Quiz</H3><DL> <DT></DT> <DD><B>1. </B>What control do you use to display Web pages? <P> <DT></DT> <DD><B>2. </B>What control do you use to connect to newsgroups? <P> <DT></DT> <DD><B>3. </B>What is the name of the method used to display an HTML document with the THTML control? <P> <DT></DT> <DD><B>4. </B>What event is generated when an HTML document has completed loading? <P> <DT></DT> <DD><B>5. </B>What control do you use to send email messages? <P> <DT></DT> <DD><B>6. </B>What control do you use to retrieve email messages? <P> <DT></DT> <DD><B>7. </B>What is the name of the method used to send mail with the TNMSMTP control? <P> <DT></DT> <DD><B>8. </B>Can you freely distribute the Internet Explorer ActiveX control? <P> <DT></DT> <DD><B>9. </B>What is the name of the utility used to register ActiveX controls? <P> <DT></DT> <DD><B>10. </B>What company provides the bulk of the Internet controls that come with Delphi? <P></DL><H2><A NAME="Heading14"></A>Exercises</H2><DL> <DT></DT> <DD><B>1. </B>Write an application for sending email. The application's main form should have fields for the <I>from</I> address, the <I>to</I> address, the mail subject, and the message text. <P> <DT></DT> <DD><B>2. </B>Add Forward and Back buttons to the EZ Web Browser application and make them operational. <P> <DT></DT> <DD><B>3. Extra Credit:</B> Add animation to the EZ Web Browser application so that the user can tell when the browser is loading a document.</DL><H1></H1><CENTER><P><HR><A HREF="../apb/apb.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../index.htm"><IMGSRC="../button/contents.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Contents"BORDER="0"></A></P><P>© <A HREF="../copy.htm">Copyright</A>, Macmillan Computer Publishing. Allrights reserved.</CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -