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

📄 ch21.htm

📁 VC 21天 学习VC 的好东西
💻 HTM
📖 第 1 页 / 共 3 页
字号:
handlers to the dialog bar. You need to trigger an action when the user finishes
entering a URL into the edit box. The closest event available to you through the
Class Wizard is the EN_CHANGED event, which will trigger for each letter the user
types. What you need is an event that will trigger when the user presses the Enter
key. Fortunately, when the user types in the edit box on the dialog bar and presses
the Enter key, the IDOK command ID is sent to the frame class. What you can do is
add a command handler in the message map to call a function on the IDOK command.</P>
<P>In your command handler, you need to get the window text from the edit box on
the dialog bar. You can pass this string to the Navigate function in the view class,
making the browser go to the page specified by the user.</P>
<P>To add this functionality to your application, add a new member function to the
CMainFrame class. Specify the function type as void, the function declaration as
OnNewAddress, and the access as public. Edit the new function, adding the code in
Listing 21.1.</P>
<P>
<H4>LISTING 21.1. THE CMainFrame OnNewAddress FUNCTION.</H4>
<PRE>1: void CMainFrame::OnNewAddress()
2: {
3:     CString sAddress;
4:
5:     // Get the new URL
6:     m_wndDlgBar.GetDlgItem(IDC_EADDRESS)-&gt;GetWindowText(sAddress);
7:     // Navigate to the new URL
8:     ((CWebBrowseView*)GetActiveView())-&gt;Navigate(sAddress);
9: }</PRE>
<P>In this function, line 6 got the text in the edit box using the GetWindowText
function, placing the text into the m_sAddress variable. The dialog bar was declared
in the CMainFrame class as the m_wndDlgBar variable, so you were able to use the
GetDlgItem function on the dialog bar variable to get a pointer to the edit box.</P>
<P>In line 8, you cast the return pointer from the GetActiveView function as a pointer
to the CWebBrowseView class. This allowed you to call the Navigate function on the
view class, passing it the URL that was entered into the edit box.</P>
<P>Now that you are able to take the URL that the user entered and tell the browser
component to go to that Web page, how do you trigger this function? You have to add
the message-map entry by hand because this is one that the Class Wizard isn't able
to add. In the message map, after the closing marker of the AFX_MSG_MAP (the section
maintained by the Class Wizard), add the ON_COMMAND macro, specifying the IDOK command
and your new function as the handler to be called, as in Listing 21.2. You can also
add this entry before the Class Wizard section as long as it's on either side and
not inside the section maintained by the Class Wizard.</P>
<P>
<H4>LISTING 21.2. THE CMainFrame MESSAGE MAP.</H4>
<PRE>1: BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
2:     //{{AFX_MSG_MAP(CMainFrame)
3:         // NOTE - the ClassWizard will add and remove mapping macros             &Acirc;here.
4:         //    DO NOT EDIT what you see in these blocks of generated             &Acirc;code !
5:     ON_WM_CREATE()
6:     //}}AFX_MSG_MAP
7:     ON_COMMAND(IDOK, OnNewAddress)
8: END_MESSAGE_MAP()</PRE>
<P>If you compile and run your application, you can enter a URL into the edit box
on the toolbar and press the Enter key, and your application should browse to the
Web page you specified, as in Figure 21.5.</P>
<P><A HREF="javascript:popUp('21fig05.gif')"><B>FIGURE 21.5.</B></A><B> </B><I>Browsing
to a specified URL.</I></P>
<P>
<H4>Displaying the Current URL</H4>
<P>When surfing the Web, you often follow links on Web pages that take you to other
Web sites. When you do this, you wouldn't know what Web site you accessed if your
browser didn't place the URL into the address box, indicating the site where you
are and providing the opportunity to copy or modify the URL to find another page
on the same site.</P>
<P>Getting the current URL from the browser is a simple matter of calling the GetLocationURL
function and passing the result to the dialog bar. The problem is when to get the
URL. It turns out that some event functions in the CHtmlView class can be overridden
in your class. These functions will be triggered on various events that are triggered
by the browser control. There are event functions for starting the navigation, beginning
a download, monitoring a download's progress, and, most important for our needs,
indicating a download has finished. None of these event handler functions can be
added to your view class through the Class Wizard. You have to add them all.</P>
<P>To add the download-complete event handler to your application, add a new member
function to the view class of your application. Specify the function type as void,
the function declaration as OnDocumentComplete(LPCTSTR lpszUrl), and the access as
public. Edit the function, adding the code in Listing 21.3.</P>
<P>
<H4>LISTING 21.3. THE CWEBBROWSEVIEW ONDOCUMENTCOMPLETE FUNCTION.</H4>
<PRE>1: void CWebBrowseView::OnDocumentComplete(LPCTSTR lpszUrl)
2: {
3:     // Pass the new URL to the address bar
4:     ((CMainFrame*)GetParentFrame())-&gt;SetAddress(lpszUrl);
5: }</PRE>
<P>You'll notice in this function that you didn't need to call the GetLocationURL
function after all. The URL that is downloaded is passed as an argument to this function.
This allows you to pass the URL along to the frame, where you'll add another function
to populate the edit box on the dialog bar with the URL.</P>
<P>To add the function to populate the dialog bar with the new URL, add a member
function to the main frame class, CMainFrame. Specify the function type as void,
the function declaration as SetAddress(LPCTSTR lpszURL), and the access as public.
Edit the function, adding the code in Listing 21.4.</P>
<P>
<H4>LISTING 21.4. THE CMainFrame SetAddress FUNCTION.</H4>
<PRE>1: void CMainFrame::SetAddress(LPCTSTR lpszURL)
2: {
3:     // Set the new URL in the address edit control
4:     m_wndDlgBar.GetDlgItem(IDC_EADDRESS)-&gt;SetWindowText(lpszURL);
5: }</PRE>
<P>In this function, you took the opposite path from the one you used to get the
text from the edit box. You used the SetWindowText to change the text in the edit
box to the URL that you are passing in. When you run your application, you should
be able to see the URL address on the dialog bar change to reflect the Web page that
you are viewing.</P>
<P>
<H4>Back and Forth</H4>
<P>Now that you can enter a URL into the dialog bar and have your application go
to that Web site, and you can see the address of any Web sites that you view, it'd
be nice if you could back up from where you might have gone. This is a simple matter
of calling the GoBack and GoForward functions on the view class in your application.
You can call these functions from menu entries, which also allows you to attach toolbar
buttons to perform the same calls.</P>
<P>To add this functionality, open the main menu in the Menu Designer. You can delete
the Edit menu from the bar, and all of the entries below it, because they are of
no use in the application that you are building today. Grab the blank menu entry
on the bar, and drag it to the left of the Help menu. Open the properties dialog
for this menu entry and give it a caption of &amp;Go. This is the menu where all
navigation functions will be located.</P>
<P>To provide the back-and-forth functionality, you need to add two menu entries,
one for the GoBack function and one for the GoForward function. Specify the properties
for these two menu entries as shown in Table 21.2.</P>
<P>
<H4>TABLE 21.2. MENU PROPERTY SETTINGS.</H4>
<P>
<TABLE BORDER="1">
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT"><I>Object</I></TD>
		<TD ALIGN="LEFT"><I>Property</I></TD>
		<TD ALIGN="LEFT"><I>Setting</I></TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">Menu Entry</TD>
		<TD ALIGN="LEFT">ID</TD>
		<TD ALIGN="LEFT">IDM_GO_BACK</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT"></TD>
		<TD ALIGN="LEFT">Caption</TD>
		<TD ALIGN="LEFT">&amp;Back\tCtrl + B</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT"></TD>
		<TD ALIGN="LEFT">Prompt</TD>
		<TD ALIGN="LEFT">Back to the previous page\nBack</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">Menu Entry</TD>
		<TD ALIGN="LEFT">ID</TD>
		<TD ALIGN="LEFT">IDM_GO_NEXT</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT"></TD>
		<TD ALIGN="LEFT">Caption</TD>
		<TD ALIGN="LEFT">&amp;Next\tCtrl + N</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT"></TD>
		<TD ALIGN="LEFT">Prompt</TD>
		<TD ALIGN="LEFT">Go forward to the next page\nNext</TD>
	</TR>
</TABLE>
</P>
<P>Once you add the menu entries, you can use the Class Wizard to add functions to
the view class on both of these menu events. For the IDM_GO_BACK menu ID, add an
event function on the COMMAND event message. Edit the function, adding the code in
Listing 21.5.</P>
<P>
<H4>LISTING 21.5. THE CWebBrowseView OnGoBack FUNCTION.</H4>
<PRE>1: void CWebBrowseView::OnGoBack()
2: {
3:     // TODO: Add your command handler code here
4:
5:     // Go to the previous page
6:     GoBack();
7: }</PRE>
<P>Open the Class Wizard again, and add an event-handler function for the IDM_GO_NEXT
object ID on the COMMAND event message. Edit this function with the code in Listing
21.6.</P>
<P>
<H4>LISTING 21.6. THE CWebBrowseView OnGoNext FUNCTION.</H4>
<PRE>1: void CWebBrowseView::OnGoNext()
2: {
3:     // TODO: Add your command handler code here
4:
5:     // Go to the next page
6:     GoForward();
7: }</PRE>
<P>Now you can run your application and use the menus to back up to the previous
Web pages from wherever you surfed to and then trace your steps forward again. However,
it's somewhat difficult using the menus, so what you need to do is add an accelerator
for each of these menu entries.</P>
<P>If you open the accelerator table in the resources tree, you see a bunch of accelerators
tied to menu IDs. Each of these accelerators consist of an ID and a key combination.
If you right-click anywhere in the accelerator table, you see the option of adding
a new accelerator to the table. Choosing this option presents you a dialog to enter
the accelerator information. First, you need to specify the menu ID that the accelerator
will be tied to. (As with toolbar buttons, accelerators are tied to menu entries.)
Below that, you can enter the key that will trigger the accelerator, or you can select
a key from the drop-down list.</P>
<P>On the right side of the dialog, you can select the modifiers for the key. Modifiers
are the other keys that must be pressed in combination with the key that you've already
specified for the accelerator to be triggered. Once you've entered all the necessary
information for the accelerator, close the dialog and the information you specified
is added to the table.</P>


<BLOCKQUOTE>
	<P>
<HR>
<STRONG>TIP:</STRONG> It's recommended that you use either the Ctrl or Alt key as
	one of the modifier keys on all accelerators using standard keys. If you don't use
	one of these two keys as part of the accelerator, your application might get confused
	about when the user is typing information into your application and when the user
	is triggering an accelerator. 
<HR>


</BLOCKQUOTE>

<P>To add accelerators to the back and forward menus in your application, delete
the accelerator for the ID_FILE_OPEN menu ID because you won't use it in this application.
Add a new accelerator and specify the ID as IDM_GO_BACK and the key as B and select
the Ctrl modifier. Add a second accelerator, specifying the ID as IDM_GO_NEXT and
the key as N and select the Ctrl modifier. When you run your application, you can
use the Ctrl+B key combination to back up to the previous page and the Ctrl+N key
combination to go forward.</P>
<P>To really make your application work like most available Web browsers, you would
also add toolbar buttons for these two menu entries with arrows pointing to the left
for back and to the right for forward.</P>
<P>
<H4>Controlling the Browser</H4>
<P>Often when browsing, you come across a Web page that you don't want to wait to
download. You'll want to stop the transfer part-way through. Maybe you entered the
wrong URL or maybe the download is taking too long. It doesn't matter why you want
to stop the download; it's enough that you want to stop it. This is why the CHtmlView
class has the Stop function. It cancels the download currently in progress. To add
this functionality to your application, add a new menu entry to the View menu in
the Menu Designer. Specify the menu entry properties in Table 21.3.</P>
<P>
<H4>TABLE 21.3. MENU PROPERTY SETTINGS.</H4>
<P>
<TABLE BORDER="1">
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT"><I>Object</I></TD>
		<TD ALIGN="LEFT"><I>Property</I></TD>
		<TD ALIGN="LEFT"><I>Setting</I></TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">Menu Entry</TD>
		<TD ALIGN="LEFT">ID</TD>
		<TD ALIGN="LEFT">IDM_VIEW_STOP</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT"></TD>
		<TD ALIGN="LEFT">Caption</TD>
		<TD ALIGN="LEFT">Sto&amp;p</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT"></TD>
		<TD ALIGN="LEFT">Prompt</TD>
		<TD ALIGN="LEFT">Stop the current transfer\nStop</TD>
	</TR>
</TABLE>
</P>
<P>Using the Class Wizard, add an event-handler function to the view class for this
menu ID on the COMMAND event message. Edit the function with the code in Listing
21.7.</P>
<P>
<H4>LISTING 21.7. THE CWebBrowseView OnViewStop FUNCTION.</H4>
<PRE>1: void CWebBrowseView::OnViewStop()
2: {

⌨️ 快捷键说明

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