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

📄 apc.htm

📁 delphi自学的好教材!特别适合刚刚起步学习delphi的人员!同样对使用者具有参考价值!
💻 HTM
📖 第 1 页 / 共 3 页
字号:
	<P>	<DT></DT>	<DD><B>8. </B>Now compile and run the program. Type an URL in the combo box and press	Enter. If you typed in a valid URL, the page will load in the HTML control.	<P></DL><P>Wow! A Web browser in 15 minutes! Notice that the browser acts like any otherWeb browser...well, sort of. You need to add a lot of functionality, but it's a start.</P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> If you are fortunate enough to have a full-time Internet connection,	your new browser will work immediately. If you are using dial-up networking with	auto dial enabled, the dialer will start automatically and connect to your Internet	service provider (ISP). If you don't have dial-up networking installed, you have	to connect to the Internet manually before running the program. <HR></BLOCKQUOTE><H3></H3><H3><A NAME="Heading5"></A>Adding a Progress Indicator</H3><P>You now have a good start on your Web browser. One of the features you are missingis some status information on each page as it loads. What you'll do is add a routinethat updates the status bar as a page loads. You make use of the THTML control'sOnUpdateRetrieval and OnEndRetrieval events to obtain periodic status updates. Youwill use the GetBytesTotal and GetBytesDone methods to calculate a percentage andthen display the percentage loaded in the status bar. Ready?</P><DL>	<DT></DT>	<DD><B>1. </B>Click on the HTML control on your form. Generate an event handler for	the OnUpdateRetrieval event. Add code to the event handler so that it looks like	this:	<P></DL><BLOCKQUOTE>	<PRE>procedure TWebMain.HTMLUpdateRetrieval(Sender: TObject);var  Total   : Integer;  Done    : Integer;  Percent : Integer;begin  Total := HTML.RetrieveBytesTotal;  Done  := HTML.RetrieveBytesDone;  if (Total = 0) or (Done = 0) then    Percent := 0  else    Percent := ((Done * 100) div Total);  StatusBar.SimpleText := Format(    `Getting Document: %d%% of %dK', [Percent, Total div 1024]);end;</PRE></BLOCKQUOTE><PRE></PRE><DL>	<DT></DT>	<DD><B>2. </B>Now generate an event handler for the OnEndRetrieval event. Type this	code in the event handler:	<P></DL><BLOCKQUOTE>	<PRE>StatusBar.SimpleText := `Done';</PRE></BLOCKQUOTE><PRE></PRE><P>Take a closer look at the code in step 1. There isn't very much to it. The GetBytesTotalmethod tells you how many bytes are in the document or the embedded object currentlybeing loaded (objects include images). The GetBytesDone method gives the number ofbytes that have been retrieved for the page or object up to this point. From thereit's a simple matter to calculate the percentage of the object that has been retrieved.Finally, you format a string with the information obtained from the HTML controland send it to the status bar. The code in step 2 simply updates the status bar afterthe entire document has been loaded.</P><P>Run the program again and watch what happens when you load a page. The statusbar shows the percentage loaded for the page and for any embedded objects.</P><P><H3><A NAME="Heading6"></A>Some Finishing Touches</H3><P>Now for some finishing touches. First, you'll add some buttons beneath the URLcombo box. (For a peek at the finished product, see Figure BD.2.) Here goes:</P><DL>	<DT></DT>	<DD><B>1. </B>Place a button on the panel beneath the URL combo box. Make its Name	property GoBtn and change its Caption to Go!.	<P>	<DT></DT>	<DD><B>2. </B>Generate an event handler for the OnClick event for the new button.	Enter the following code in the event handler:	<P></DL><BLOCKQUOTE>	<PRE>URLComboBoxClick(Self);</PRE></BLOCKQUOTE><PRE></PRE><DL>	<DT></DT>	<DD><B>3. </B>Place another button on the panel, just to the right of the first button.	Change the Name to StopBtn and the Caption to Stop.	<P>	<DT></DT>	<DD><B>4. </B>Generate an event handler for the OnClick event for this button and	type the following code in the event handler:	<P></DL><BLOCKQUOTE>	<P>HTML.Cancel(0);</P>	<P>	<PRE>StatusBar.SimpleText := `Done';</PRE></BLOCKQUOTE><PRE></PRE><DL>	<DT></DT>	<DD><B>5. </B>Place a third button on the panel to the right of the other two buttons.	Change the Name property to ReloadBtn. Change the Caption property to Reload.	<P>	<DT></DT>	<DD><B>6. </B>Create an event handler for the OnClick event for this button and enter	the same code as in step 2:	<P></DL><BLOCKQUOTE>	<PRE>URLComboBoxClick(Self);</PRE></BLOCKQUOTE><PRE></PRE><DL>	<DT></DT>	<DD><B>7. </B>Place a fourth (and final) button on the panel. Change the Name to	SourceBtn and change the Caption to View Source.	<P>	<DT></DT>	<DD><B>8. </B>Create an event handler for the OnClick event and enter this code:	<P></DL><BLOCKQUOTE>	<PRE>HTML.ViewSource := not HTML.ViewSource;if HTML.ViewSource then  SourceBtn.Caption := `View Document'else  SourceBtn.Caption := `View Source';</PRE></BLOCKQUOTE><PRE></PRE><P>Your form should now look like the one shown in Figure BD.2.</P><P><A HREF="javascript:popUp('28672202.gif')"><B>FIGURE BD.2.</B></A><B> </B><I>TheEZ Web Browser with buttons in place.</I></P><P>These steps introduce a couple of new THTML elements. The Cancel method stopsthe process of loading a document. The ViewSource property is used to toggle betweenviewing the document as HTML source or as a regular HTML document.</P><P>Now run the program again. Check out the new buttons and see what they do. Inparticular, give the View Source button a try.</P><P>Okay, you're almost done with your Web browser. Let's add a couple of more features.You are going to respond to two more events of the THTML control in order to providemore status information.</P><DL>	<DT></DT>	<DD><B>1. </B>Generate an event handler for THTML's OnDoRequestDoc event. Type this	code in the event handler:	<P></DL><BLOCKQUOTE>	<PRE>StatusBar.SimpleText := `Connecting to ` + URL + `...';</PRE></BLOCKQUOTE><PRE></PRE><DL>	<DT></DT>	<DD><B>2. </B>Now create an event handler for the OnBeginRetrieval event. When the	event handler appears, type this code:	<P></DL><BLOCKQUOTE>	<PRE>StatusBar.SimpleText := `Connected...';URLComboBox.Items.Insert(0, URLComboBox.Text);</PRE></BLOCKQUOTE><PRE></PRE><P>In this sequence, step 1 makes use of the OnDoRequestDoc event, which is generatedwhen a document is requested. The URL parameter of the DoRequestDoc event handleris the URL of the site to which you are connected. As long as the URL is provided,you can use it to build a string to display in the status bar. Step 2 adds a littlemore status information when the document actually starts to load. It also takesthe URL and adds it to the URL combo box's list. You need to make sure that you haveconnected to a site before adding the URL to the list of visited sites.</P><P>Congratulations, you have finished (or nearly finished) your first Internet application.Figure BD.3 shows the EZ Web Browser in operation.</P><P><A HREF="javascript:popUp('28672203.gif')"><B>FIGURE BD.3.</B></A><B> </B><I>Thefinished EZ Web Browser displaying a page.</I></P><P>Hey, that's good work! There are some things that your Web browser doesn't do,but it does a lot, so you can be proud. Stand back and admire your work. You cantake your new creation and add some new features of your own. One feature you mightwant to add is a list of URLs that can be used to implement browse buttons (backand next). You could also replace the standard buttons with a toolbar and add glyphsto the toolbar's buttons. If you really want to add the ultimate touch, provide ananimation while the document is loading so that your users can tell when your browseris doing something. You can do that most easily with a TImageList component, althoughthe TAnimate component would work, too.</P><P>The THTML control has several properties that I didn't cover. Most of these propertieshave to do with user preferences such as background color, the color of links, thecolor of visited links, the various fonts used for each heading size, and so on.I'm not going to explain those properties because they are easy to figure out. Forthe most part you can just accept the default values for these properties. If youwant to customize your browser further, though, you can certainly spend some timereviewing the properties list for the THTML control.</P><P>Listing BD.1 lists the browser program's main unit.</P><P><H4>LISTING BD.1. WebBrwsU.pas.</H4><PRE>unit WebBrwsU;interfaceuses  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,  StdCtrls, ExtCtrls, OleCtrls, NMHTML, ComCtrls;type  TWebMain = class(TForm)    Panel1: TPanel;    URLComboBox: TComboBox;    StatusBar: TStatusBar;    HTML: THTML;    GoBtn: TButton;    StopBtn: TButton;    ReloadBtn: TButton;    SourceBtn: TButton;    procedure URLComboBoxClick(Sender: TObject);    procedure URLComboBoxKeyPress(Sender: TObject; var Key: Char);    procedure HTMLUpdateRetrieval(Sender: TObject);    procedure HTMLEndRetrieval(Sender: TObject);    procedure Gobtnclick(Sender: Tobject);    procedure StopBtnClick(Sender: TObject);    procedure ReloadBtnClick(Sender: TObject);    procedure SourceBtnClick(Sender: TObject);    procedure HTMLDoRequestDoc(Sender: TObject; const URL: WideString;      Element: HTMLElement; DocInput: DocInput;      var EnableDefault: WordBool);    procedure HTMLBeginRetrieval(Sender: TObject);  private    { Private declarations }  public    { Public declarations }  end;var  WebMain: TWebMain;implementation{$R *.DFM}procedure TWebMain.URLComboBoxClick(Sender: TObject);begin  if URLComboBox.Text &lt;&gt; `' then    HTML.RequestDoc(URLComboBox.Text);end;procedure TWebMain.URLComboBoxKeyPress(Sender: TObject; var Key: Char);begin  if Key = Char(VK_RETURN) then begin    Key := #0;    if URLComboBox.Text = `' then      Exit;    URLComboBoxClick(Sender);  end;end;procedure TWebMain.HTMLUpdateRetrieval(Sender: TObject); var  Total   : Integer;  Done    : Integer;  Percent : Integer;begin  Total := HTML.RetrieveBytesTotal;  Done  := HTML.RetrieveBytesDone;  if (Total = 0) or (Done = 0) then    Percent := 0  else    Percent := ((Done * 100) div Total);  StatusBar.SimpleText := Format(    `Getting Document: %d%% of %dK', [Percent, Total div 1024]);end;procedure TWebMain.HTMLEndRetrieval(Sender: TObject);begin  StatusBar.SimpleText := `Done';end;procedure TWebMain.GoBtnClick(Sender: TObject);begin  URLComboBoxClick(Self);end;procedure TWebMain.StopBtnClick(Sender: TObject);begin  HTML.Cancel(0);  StatusBar.SimpleText := `Done';end;procedure TWebMain.ReloadBtnClick(Sender: TObject);begin  URLComboBoxClick(Self);end;procedure TWebMain.SourceBtnClick(Sender: TObject);begin  HTML.ViewSource := not HTML.ViewSource;  if HTML.ViewSource then    SourceBtn.Caption := `View Document'  else    SourceBtn.Caption := `View Source';end;procedure TWebMain.HTMLDoRequestDoc(Sender: TObject; const URL: WideString;  Element: HTMLElement; DocInput: DocInput; var EnableDefault: WordBool);begin  StatusBar.SimpleText := `Connecting to ` + URL + `...';end;procedure TWebMain.HTMLBeginRetrieval(Sender: TObject);begin  StatusBar.SimpleText := `Connected...';  URLComboBox.Items.Insert(0, URLComboBox.Text);end;</PRE><PRE>end.</PRE><P><H2><A NAME="Heading7"></A>Using Internet Explorer as an ActiveX Control</H2><P>If you have Microsoft Internet Explorer installed on your system, you can useit as an ActiveX control. The first thing you need to do is import the control intoDelphi's Component palette. After that, you can place it on a form just as you wouldany control. First, let me show you how to import Internet Explorer. (I showed youhow to import ActiveX controls on Day 15, &quot;COM and ActiveX,&quot; but it doesn'thurt to review.) Here are the steps:</P><DL>

⌨️ 快捷键说明

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