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

📄 ch21.htm

📁 VC 21天 学习VC 的好东西
💻 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"><IMG
SRC="../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 to
make 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 client
isn't really a practical integration. However, making it easy for the users of development
tools to build Internet-enabled applications is a very practical feature. And this
is exactly what Microsoft did.</P>
<P>One of the capabilities that Microsoft made available to its application development
tools is using Internet Explorer as an integrated part of any application. This means
that you can include Internet Explorer, and all its associated components, in your
own applications. The possibilities extend far beyond providing your users Web browsing
capability; your applications can also house, and interact with, Java applets. You
can 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 Explorer
to support the use of ActiveX controls. Well, the developers looked at what they
would need to do, and what was possible, and decided to make Internet Explorer a
lot more than just a Web browser.</P>
<P>The first thing that Microsoft did was separate the Web browser from the ActiveX
objects that perform all the work. As a result, it ended up with the Internet Explorer
application, which is little more than an ActiveX document container, and the Internet
Explorer HTML viewer control, which ran as an ActiveX document server inside the
application. This meant that the Internet Explorer application could host more than
just Web pages; it could also be used to host Word documents, Excel spreadsheets,
PowerPoint presentations, and any other ActiveX document that had an ActiveX document
server installed on the same computer, as shown in Figure 21.1.</P>
<P>Within the HTML viewer component, Microsoft added the capability to host other
controls, including scripting engines and ActiveX controls, as shown in Figure 21.2.
This gave Microsoft the flexibility to add more scripting languages to Internet Explorer
as they were requested and created. This also enabled Internet Explorer to host any
ActiveX controls that developers might want to create.</P>
<P>In designing Internet Explorer this way, Microsoft not only gave itself a lot
of flexibility for future expansion of the functionality supported by Internet Explorer,
but it also made the entire workings of Internet Explorer available to any developer
that wants to take advantage of it and integrate Internet Explorer into his or her
applications.</P>
<P><A HREF="javascript:popUp('21fig01.gif')"><B>FIGURE 21.1.</B></A><B> </B><I>The
Internet Explorer ActiveX document model.</I></P>
<P><A HREF="javascript:popUp('21fig02.gif')"><B>FIGURE 21.2.</B></A><B> </B><I>The
Internet 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 used
as the base class for the view class in your Single Document Interface (SDI) or Multiple
Document Interface (MDI) applications. You can easily create applications that have
built-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. There
are functions for returning the browser to the starting page of the user or for taking
the user to an Internet search page. There are also functions for taking the user
to the previous or next page or even to a remote Web page. All these functions are
members of the CHtmlView class and thus are member functions of your application
view 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 same
function as their toolbar equivalents in Internet Explorer. The last function does
take 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 functions
for controlling the browser. Two of these functions are Refresh(), which makes the
HTML viewer control reload the current Web page, and Stop(), which halts a download
in progress. As with most of the navigation functions, these functions do not take
any 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 informational
in nature. You can use these functions to get information about the current state
of 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 you
want 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 only
work 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 component
into your own applications, you will build a simple Web browser application. You
will create an SDI application using the CHtmlView class as the base for your own
view class. You'll add a menu with functions for the back and forward navigation
options. You'll also add a dialog for getting from the user a URL that you will use
to 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 application
shell. The only other thing that you need to ensure is that Internet Explorer is
installed 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 to
install the latest version of Internet Explorer. On any computers where you run your
application, however, you might need to make sure that Internet Explorer is installed
or install it yourself.</P>
<P>To create the shell of the application that you will build today, start a new
project using the MFC AppWizard to create the application shell. Give the project
a 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 application
as you can create an SDI application. For the purposes of the sample application
that you are building today, go ahead and specify that the application is a Single
document (SDI) application. You can accept the default settings for the rest of the
AppWizard; for this example, however, choose the Internet Explorer ReBars for the
toolbar appearance on the fourth AppWizard step.</P>
<P>Finally, on the sixth step, specify the CHtmlView class as the base class for
your view class. This causes your application to be created using the Internet Explorer
Web browser control as the main application view.</P>
<P>Once you finish generating the shell for your application, if you compile and
run it while connected to the Internet, you'll find that you already have a working
Web browser, as shown in Figure 21.3. However, you do not have the ability to specify
where 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>The
initial 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 control
where it takes you. What you need to add is an edit control where the user can enter
a URL. Looking at the toolbar of the running application, you notice there's a place
to put this control.</P>
<P>
<H4>Specifying a URL</H4>
<P>You probably noticed when you ran your application that the second toolbar had
some static text telling you to lay out the dialog bar. The dialog bar is different
from 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 dialog
bar in the resource tab, you won't find it in the toolbar folder; it's in the dialogs
folder.</P>
<P>If you open the dialogs folder and double-click the IDR_MAINFRAME dialog to open
it 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 bar
and add an edit box, as shown in Figure 21.4. Specify an ID for the edit box; for
this example, use the ID IDC_EADDRESS.</P>
<P>Before you open the Class Wizard to begin adding variables and event functions
to the dialog bar, be aware that the dialog bar will automatically send its events
to the main frame class in your application. When you open the Class Wizard, it assumes
that you need to associate the dialog bar with a class and prompts you to create
a new class. This association is not necessary because you can map all its events
through 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>The
dialog 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 + -