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

📄 ch21.htm

📁 visual c plus plus its a book for how to learn c plus plus in just 21 days its an easy way to lea
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD>	<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1"><SCRIPT LANGUAGE="JavaScript"><!--function popUp(pPage) { var fullURL = document.location; var textURL = fullURL.toString(); var URLlen = textURL.length; var lenMinusPage = textURL.lastIndexOf("/"); lenMinusPage += 1; var fullPath = textURL.substring(0,lenMinusPage); popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394'); figDoc= popUpWin.document; zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>'; zhtm += '</head>'; zhtm += '<BODY bgcolor="#FFFFFF">'; zhtm += '<IMG SRC="' + fullPath + pPage + '">'; zhtm += '<P><B>' + pPage + '</B>'; zhtm += '</BODY></HTML>'; window.popUpWin.document.write(zhtm); window.popUpWin.document.close(); // Johnny Jackson 4/28/98 }//-->                                                                </SCRIPT><link rel="stylesheet" href="/includes/stylesheets/ebooks.css">	<META NAME="GENERATOR" Content="Symantec Visual Page Mac 1.1.1">	<TITLE>Teach Yourself Visual C++ 6 in 21 Days -- Ch 21 -- Adding Web Browsing Functionality to Your Applications</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><H1 ALIGN="CENTER"><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM"BORDER="0"><BR>Teach Yourself Visual C++ 6 in 21 Days</H1><CENTER><P><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> <HR></CENTER><H1 ALIGN="CENTER">- 21 -<BR>Adding Web Browsing Functionality to Your Applications</H1><H1></H1><UL>	<LI><A HREF="#Heading1">The Internet Explorer ActiveX Model</A>	<LI><A HREF="#Heading2">The CHtmlView Class</A>	<UL>		<LI><A HREF="#Heading3">Navigating the Web</A>		<LI><A HREF="#Heading4">Controlling the Browser</A>		<LI><A HREF="#Heading5">Getting the Browser Status</A>	</UL>	<LI><A HREF="#Heading6">Building a Web-Browsing Application</A>	<UL>		<LI><A HREF="#Heading7">Creating the Application Shell</A>		<LI><A HREF="#Heading8">Adding Navigation Functionality</A>	</UL>	<LI><A HREF="#Heading9">Summary</A>	<LI><A HREF="#Heading10">Q&amp;A</A>	<LI><A HREF="#Heading11">Workshop</A>	<UL>		<LI><A HREF="#Heading12">Quiz</A>		<LI><A HREF="#Heading13">Exercises</A>	</UL>	<LI><A HREF="#Heading14">In Review</A></UL><P><HR SIZE="4"><BR>When Microsoft made the decision a few years ago to make all its applications Internet-enabled,it wasn't just talking about making Word read and write HTML pages. It wanted tomake the Internet an integrated part of every application, in some way or another.Well, when it comes to development tools, making the editor double as an email clientisn't really a practical integration. However, making it easy for the users of developmenttools to build Internet-enabled applications is a very practical feature. And thisis exactly what Microsoft did.</P><P>One of the capabilities that Microsoft made available to its application developmenttools is using Internet Explorer as an integrated part of any application. This meansthat you can include Internet Explorer, and all its associated components, in yourown applications. The possibilities extend far beyond providing your users Web browsingcapability; your applications can also house, and interact with, Java applets. Youcan provide your users with not one, but two macro languages, VBScript and JScript(Microsoft's version of JavaScript).</P><P>Today, you will learn</P><UL>	<LI>How the Internet Explorer ActiveX Object Model enables you to integrate all the	components into your applications.	<P>	<LI>How the CHtmlView view class encapsulates most of the Internet Explorer functionality	in a ready-made class.	<P>	<LI>How to build a simple Web browser using the CHtmlView class and Internet Explorer.</UL><H2><A NAME="Heading1"></A>The Internet Explorer ActiveX Model</H2><P>When Microsoft came up with the idea of integrating ActiveX with its Web browser,Internet Explorer, it realized that it would need to reengineer Internet Explorerto support the use of ActiveX controls. Well, the developers looked at what theywould need to do, and what was possible, and decided to make Internet Explorer alot more than just a Web browser.</P><P>The first thing that Microsoft did was separate the Web browser from the ActiveXobjects that perform all the work. As a result, it ended up with the Internet Explorerapplication, which is little more than an ActiveX document container, and the InternetExplorer HTML viewer control, which ran as an ActiveX document server inside theapplication. This meant that the Internet Explorer application could host more thanjust Web pages; it could also be used to host Word documents, Excel spreadsheets,PowerPoint presentations, and any other ActiveX document that had an ActiveX documentserver installed on the same computer, as shown in Figure 21.1.</P><P>Within the HTML viewer component, Microsoft added the capability to host othercontrols, including scripting engines and ActiveX controls, as shown in Figure 21.2.This gave Microsoft the flexibility to add more scripting languages to Internet Exploreras they were requested and created. This also enabled Internet Explorer to host anyActiveX controls that developers might want to create.</P><P>In designing Internet Explorer this way, Microsoft not only gave itself a lotof flexibility for future expansion of the functionality supported by Internet Explorer,but it also made the entire workings of Internet Explorer available to any developerthat wants to take advantage of it and integrate Internet Explorer into his or herapplications.</P><P><A HREF="javascript:popUp('21fig01.gif')"><B>FIGURE 21.1.</B></A><B> </B><I>TheInternet Explorer ActiveX document model.</I></P><P><A HREF="javascript:popUp('21fig02.gif')"><B>FIGURE 21.2.</B></A><B> </B><I>TheInternet Explorer HTML viewer ActiveX object model.</I></P><P><H2><A NAME="Heading2"></A>The CHtmlView Class</H2><P>To make it easy to incorporate the Internet Explorer HTML viewer into Visual C++applications, Microsoft wrapped it in the CHtmlView class. This class can be usedas the base class for the view class in your Single Document Interface (SDI) or MultipleDocument Interface (MDI) applications. You can easily create applications that havebuilt-in Web browsing capabilities.</P><P><H3><A NAME="Heading3"></A>Navigating the Web</H3><P>Several functions available in the CHtmlView class cover navigating the Web. Thereare functions for returning the browser to the starting page of the user or for takingthe user to an Internet search page. There are also functions for taking the userto the previous or next page or even to a remote Web page. All these functions aremembers of the CHtmlView class and thus are member functions of your applicationview class (when using the CHtmlView class as the base class for your view class).</P><P>The navigation functions for the CHtmlView class are listed in Table 21.1.</P><P><H4>TABLE 21.1. CHtmlView NAVIGATION FUNCTIONS.</H4><P><TABLE BORDER="1">	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT"><I>Function Definition</I></TD>		<TD ALIGN="LEFT"><I>Description</I></TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">GoBack()</TD>		<TD ALIGN="LEFT">Takes the user to the previous Web page.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">GoForward()</TD>		<TD ALIGN="LEFT">Takes the user to the next Web page. (This assumes that the user has backed up from			at least one Web page.)</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">GoHome()</TD>		<TD ALIGN="LEFT">Takes the user to the start page for Internet Explorer.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">GoSearch()</TD>		<TD ALIGN="LEFT">Takes the user to an Internet search page.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Navigate(LPCTSTR URL)</TD>		<TD ALIGN="LEFT">Takes the user to the Web page specified in the URL variable.</TD>	</TR></TABLE></P><P>The first four functions do not take any arguments and perform the exact samefunction as their toolbar equivalents in Internet Explorer. The last function doestake arguments; the only required argument is the URL of the Web page to display.</P><P><H3><A NAME="Heading4"></A>Controlling the Browser</H3><P>Along with the functions for navigating around the Web, you use some functionsfor controlling the browser. Two of these functions are Refresh(), which makes theHTML viewer control reload the current Web page, and Stop(), which halts a downloadin progress. As with most of the navigation functions, these functions do not takeany arguments and work just like their equivalent toolbar buttons in Internet Explorer.</P><P><H3><A NAME="Heading5"></A>Getting the Browser Status</H3><P>Another category of functions that are available in the CHtmlView class is informationalin nature. You can use these functions to get information about the current stateof the browser. For instance, if you want to get the current Web page in the browser,you can call GetLocationURL(), which returns a CString containing the URL. If youwant to determine if the browser is busy with a download, you can call GetBusy(),which returns a boolean value specifying whether the browser is busy.</P><P>Many more functions are available in the CHtmlView class, and some of them onlywork on Internet Explorer itself, not on the browser control.</P><P><H2><A NAME="Heading6"></A>Building a Web-Browsing Application</H2><P>For an example of how you can integrate the Internet Explorer Web browser componentinto your own applications, you will build a simple Web browser application. Youwill create an SDI application using the CHtmlView class as the base for your ownview class. You'll add a menu with functions for the back and forward navigationoptions. You'll also add a dialog for getting from the user a URL that you will useto navigate the browser to the specified Web page.</P><P><H3><A NAME="Heading7"></A>Creating the Application Shell</H3><P>To create a Web browser application, you can create a standard SDI or MDI applicationshell. The only other thing that you need to ensure is that Internet Explorer isinstalled on the computer where your application will run. For your development computer,this is not a problem because the Visual C++ installation probably required you toinstall the latest version of Internet Explorer. On any computers where you run yourapplication, however, you might need to make sure that Internet Explorer is installedor install it yourself.</P><P>To create the shell of the application that you will build today, start a newproject using the MFC AppWizard to create the application shell. Give the projecta suitable name, such as WebBrowse, and click the OK button to start the AppWizard.</P><P>In the AppWizard, you can just as easily create an MDI Web browsing applicationas you can create an SDI application. For the purposes of the sample applicationthat you are building today, go ahead and specify that the application is a Singledocument (SDI) application. You can accept the default settings for the rest of theAppWizard; for this example, however, choose the Internet Explorer ReBars for thetoolbar appearance on the fourth AppWizard step.</P><P>Finally, on the sixth step, specify the CHtmlView class as the base class foryour view class. This causes your application to be created using the Internet ExplorerWeb browser control as the main application view.</P><P>Once you finish generating the shell for your application, if you compile andrun it while connected to the Internet, you'll find that you already have a workingWeb browser, as shown in Figure 21.3. However, you do not have the ability to specifywhere your browser will take you, other than clicking links in the Web pages displayed.</P><P><A HREF="javascript:popUp('21fig03.gif')"><B>FIGURE 21.3.</B></A><B> </B><I>Theinitial Web browsing application.</I></P><P><H3><A NAME="Heading8"></A>Adding Navigation Functionality</H3><P>Now that you've got a working Web browser, it would be nice if you could controlwhere it takes you. What you need to add is an edit control where the user can entera URL. Looking at the toolbar of the running application, you notice there's a placeto put this control.</P><P><H4>Specifying a URL</H4><P>You probably noticed when you ran your application that the second toolbar hadsome static text telling you to lay out the dialog bar. The dialog bar is differentfrom what you have worked with before. It is a toolbar with dialog controls on it.You even design the bar in the dialog layout designer. When you look for this dialogbar in the resource tab, you won't find it in the toolbar folder; it's in the dialogsfolder.</P><P>If you open the dialogs folder and double-click the IDR_MAINFRAME dialog to openit in the dialog designer, you'll see that it's the second toolbar in your application.You can place edit boxes, buttons, combo boxes, and check boxes on this toolbar.You can place any control that you can use on a dialog window on this toolbar.</P><P>For your Web browser, modify the static text control already on the dialog barand add an edit box, as shown in Figure 21.4. Specify an ID for the edit box; forthis example, use the ID IDC_EADDRESS.</P><P>Before you open the Class Wizard to begin adding variables and event functionsto the dialog bar, be aware that the dialog bar will automatically send its eventsto the main frame class in your application. When you open the Class Wizard, it assumesthat you need to associate the dialog bar with a class and prompts you to createa new class. This association is not necessary because you can map all its eventsthrough the frame and from there feed them to the view or document classes.</P><P><A HREF="javascript:popUp('21fig04.gif')"><B>FIGURE 21.4.</B></A><B> </B><I>Thedialog bar layout.</I></P><P>For this example, you don't even need to use the Class Wizard to add any event

⌨️ 快捷键说明

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