📄 ch21.htm
字号:
handlers to the dialog bar. You need to trigger an action when the user finishesentering a URL into the edit box. The closest event available to you through theClass Wizard is the EN_CHANGED event, which will trigger for each letter the usertypes. What you need is an event that will trigger when the user presses the Enterkey. Fortunately, when the user types in the edit box on the dialog bar and pressesthe Enter key, the IDOK command ID is sent to the frame class. What you can do isadd 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 onthe 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 theCMainFrame class. Specify the function type as void, the function declaration asOnNewAddress, and the access as public. Edit the new function, adding the code inListing 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 URL6: m_wndDlgBar.GetDlgItem(IDC_EADDRESS)->GetWindowText(sAddress);7: // Navigate to the new URL8: ((CWebBrowseView*)GetActiveView())->Navigate(sAddress);9: }</PRE><P>In this function, line 6 got the text in the edit box using the GetWindowTextfunction, placing the text into the m_sAddress variable. The dialog bar was declaredin the CMainFrame class as the m_wndDlgBar variable, so you were able to use theGetDlgItem 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 pointerto the CWebBrowseView class. This allowed you to call the Navigate function on theview 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 browsercomponent to go to that Web page, how do you trigger this function? You have to addthe message-map entry by hand because this is one that the Class Wizard isn't ableto add. In the message map, after the closing marker of the AFX_MSG_MAP (the sectionmaintained by the Class Wizard), add the ON_COMMAND macro, specifying the IDOK commandand your new function as the handler to be called, as in Listing 21.2. You can alsoadd this entry before the Class Wizard section as long as it's on either side andnot 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 Âhere.4: // DO NOT EDIT what you see in these blocks of generated Âcode !5: ON_WM_CREATE()6: //}}AFX_MSG_MAP7: 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 boxon the toolbar and press the Enter key, and your application should browse to theWeb 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>Browsingto 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 otherWeb sites. When you do this, you wouldn't know what Web site you accessed if yourbrowser didn't place the URL into the address box, indicating the site where youare and providing the opportunity to copy or modify the URL to find another pageon the same site.</P><P>Getting the current URL from the browser is a simple matter of calling the GetLocationURLfunction and passing the result to the dialog bar. The problem is when to get theURL. It turns out that some event functions in the CHtmlView class can be overriddenin your class. These functions will be triggered on various events that are triggeredby the browser control. There are event functions for starting the navigation, beginninga download, monitoring a download's progress, and, most important for our needs,indicating a download has finished. None of these event handler functions can beadded 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 memberfunction to the view class of your application. Specify the function type as void,the function declaration as OnDocumentComplete(LPCTSTR lpszUrl), and the access aspublic. 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 bar4: ((CMainFrame*)GetParentFrame())->SetAddress(lpszUrl);5: }</PRE><P>You'll notice in this function that you didn't need to call the GetLocationURLfunction 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 functionto 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 memberfunction 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 control4: m_wndDlgBar.GetDlgItem(IDC_EADDRESS)->SetWindowText(lpszURL);5: }</PRE><P>In this function, you took the opposite path from the one you used to get thetext from the edit box. You used the SetWindowText to change the text in the editbox to the URL that you are passing in. When you run your application, you shouldbe able to see the URL address on the dialog bar change to reflect the Web page thatyou 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 goto that Web site, and you can see the address of any Web sites that you view, it'dbe nice if you could back up from where you might have gone. This is a simple matterof 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 toolbarbuttons to perform the same calls.</P><P>To add this functionality, open the main menu in the Menu Designer. You can deletethe Edit menu from the bar, and all of the entries below it, because they are ofno use in the application that you are building today. Grab the blank menu entryon the bar, and drag it to the left of the Help menu. Open the properties dialogfor this menu entry and give it a caption of &Go. This is the menu where allnavigation 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 propertiesfor 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">&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">&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 tothe view class on both of these menu events. For the IDM_GO_BACK menu ID, add anevent function on the COMMAND event message. Edit the function, adding the code inListing 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 here4:5: // Go to the previous page6: GoBack();7: }</PRE><P>Open the Class Wizard again, and add an event-handler function for the IDM_GO_NEXTobject ID on the COMMAND event message. Edit this function with the code in Listing21.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 here4:5: // Go to the next page6: GoForward();7: }</PRE><P>Now you can run your application and use the menus to back up to the previousWeb 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 acceleratorfor each of these menu entries.</P><P>If you open the accelerator table in the resources tree, you see a bunch of acceleratorstied 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 addinga new accelerator to the table. Choosing this option presents you a dialog to enterthe accelerator information. First, you need to specify the menu ID that the acceleratorwill 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 selecta key from the drop-down list.</P><P>On the right side of the dialog, you can select the modifiers for the key. Modifiersare the other keys that must be pressed in combination with the key that you've alreadyspecified for the accelerator to be triggered. Once you've entered all the necessaryinformation for the accelerator, close the dialog and the information you specifiedis 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, deletethe 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 selectthe Ctrl modifier. Add a second accelerator, specifying the ID as IDM_GO_NEXT andthe key as N and select the Ctrl modifier. When you run your application, you canuse the Ctrl+B key combination to back up to the previous page and the Ctrl+N keycombination to go forward.</P><P>To really make your application work like most available Web browsers, you wouldalso add toolbar buttons for these two menu entries with arrows pointing to the leftfor 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 todownload. You'll want to stop the transfer part-way through. Maybe you entered thewrong URL or maybe the download is taking too long. It doesn't matter why you wantto stop the download; it's enough that you want to stop it. This is why the CHtmlViewclass has the Stop function. It cancels the download currently in progress. To addthis functionality to your application, add a new menu entry to the View menu inthe 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&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 thismenu ID on the COMMAND event message. Edit the function with the code in Listing21.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 + -