韶光以外by陶夜:VC知识库:如何使IE在打开新窗口时,网页始终在本窗口中打开(也就是不另开新窗口)?

来源:百度文库 编辑:九乡新闻网 时间:2024/05/03 21:27:53
Re:如何使IE在打开新窗口时,网页始终在本窗口中打开(也就是不另开新窗口)?
Knowledge Base
HOWTO: Use the WebBrowser Control NewWindow2 Event
PSS ID Number: Q184876
Article Last Modified on 08-27-2002
--------------------------------------------------------------------------------
The information in this article applies to:
Microsoft Internet Explorer (Programming) 4.0, 4.01, 5
--------------------------------------------------------------------------------
Summary
This article describes how to use the NewWindow2 event, fired by the Microsoft WebBrowser control provided with Microsoft Internet Explorer 4.0, to specify that your browser application should be used in all cases where a new browser window is opened.
This article will describe this procedure using both Visual Basic 5.0 (VB) and the Microsoft Foundation Classes (MFC) that are part of Visual C++ 5.0 (VC).
More Information
The NewWindow2 event occurs when a new window is to be created for displaying a resource. This event precedes the creation of a new window from within the WebBrowser control, for example, in response to a navigation targeted to a new window or to a scripted window.open method.
Event handlers for this event receive two out parameters:
ppDisp: A pointer to the IDispatch interface of a WebBrowser or InternetExplorer object. You set this pointer equal to the IDispatch interface of a new or existing WebBrowser or InternetExplorer object.
Cancel: A pointer to a Boolean value. Setting this value to VARIANT_TRUE causes navigation to stop and no new window is opened.
In order to specify that your browser application should be used when a new window is opened, you set ppDisp equal to a new WebBrowser object that is contained in a new window created by your application. In this scenario, if a user chooses to open a Web page in a new window, the new window in your application will be used to display the new Web page.
In addition, set the RegisterAsBrowser property to TRUE for the newly created WebBrowser control in order for it to participate in window name resolution. For example, if this window name is used elsewhere in script, then this control will be used instead of a newly created one because it checks all of the existing window names before opening a new window.
Here is some sample Visual Basic code to accomplish this:
Private Sub WebBrowser1_NewWindow2(ppDisp As Object,
Cancel As Boolean)
Dim frmWB As Form1
Set frmWB = New Form1
frmWB.WebBrowser1.RegisterAsBrowser = TRUE
Set ppDisp = frmWB.WebBrowser1.Object
frmWB.Visible = True
End Sub
Using MFC, you may wish to do this in one of three types of applications:
Dialog-based
Single document interface (SDI)
Multiple document interface (MDI)
Here is some sample MFC code that would accomplish this task in a dialog- based application:
void CYourDlg::OnNewWindow2(LPDISPATCH FAR* ppDisp, BOOL FAR* Cancel)
{
m_dlgNewWB = new CYourDlg;
m_dlgNewWB->Create(IDD_WBDLG_DIALOG);
m_dlgNewWB->m_webBrowser.SetRegisterAsBrowser(TRUE);
*ppDisp = m_dlgNewWB->m_webBrowser.GetApplication();
}
Here is some sample MFC code that would accomplish this task in an SDI or MDI application. This code creates a new frame that contains a WebBrowser control. In an SDI application, this frame would appear to the user to look like another instance of the application. In an MDI application, this frame is the same as if the user had chosen to open a new child window.
void CYourView::OnNewWindow2(LPDISPATCH FAR* ppDisp,
BOOL FAR* Cancel)
{
// Get a pointer to the application object
CWinApp* pApp = AfxGetApp();
// Get the correct document template
CDocTemplate* pDocTemplate;
POSITION pos = pApp->GetFirstDocTemplatePosition();
pDocTemplate = pApp->GetNextDocTemplate(pos);
ASSERT(pDocTemplate);
// Create the new frame
CFrameWnd* pNewFrame = pDocTemplate->CreateNewFrame(GetDocument(),
(CFrameWnd*)AfxGetMainWnd());
ASSERT(pNewFrame);
// Activate the frame and set its active view
pDocTemplate->InitialUpdateFrame(pNewFrame, NULL);
CYourView* pWBVw = (CYourView*)pNewFrame->GetActiveView();
ASSERT(pWBVw);
pWBVw->m_webBrowser.SetRegisterAsBrowser(TRUE);
*ppDisp = pWBVw->m_webBrowser.GetApplication();
}
References
"Reusing the WebBrowser and MSHTML" in the Internet Client SDK Help:
http://www.microsoft.com/msdn/sdk/inetsdk/help/
(c) Microsoft Corporation 1998, All Rights Reserved. Contributions by Scott Roberts, Microsoft Corporation.
Additional query words: window.open
Keywords: kbIE400 kbie401 kbWebBrowser kbGrpDSInet kbie500
Issue Type: kbhowto
Technology: kbIEsearch kbAudDeveloper kbSDKIESearch kbZNotKeyword kbSDKIE400 kbSDKIE401 kbSDKIE500
--------------------------------------------------------------------------------
Send feedback to Microsoft
© 2002-2003 Microsoft Corporation. All rights reserved.
回复人: YangTze 2008-11-21 11:19:57
Re:Re:如何使IE在打开新窗口时,网页始终在本窗口中打开(也就是不另开新窗口)?
Knowledge Base
HOWTO: Cause Navigation to Occur in Same WebBrowser Window
PSS ID Number: Q185538
Article Last Modified on 07-20-2001
--------------------------------------------------------------------------------
The information in this article applies to:
Microsoft Internet Explorer (Programming) 4.0, 4.01, 5, 5.5
--------------------------------------------------------------------------------
Summary
When hosting the Internet Explorer 4.x or later WebBrowser control in a Visual Basic application, you may want to have the navigation always occur in your application and not other Internet Explorer windows. If you handle the NewWindow2 event and set the Cancel flag equal to True, navigation is canceled completely. Since NewWindow2 does not provide you with the URL to navigate to as the Internet Explorer 3.x NewWindow event did, there doesn't appear to be any way to have the navigation occur in the same window.
Fortunately, Internet Explorer 4.x or later provide the WebBrowser_V1 object for compatibility with Internet Explorer 3.x. Using the WebBrowser_V1 object, you can have your application receive events from version 3.x, 4.x, and 5.x. That means that you can handle the version 3.x NewWindow event and then have the navigation occur in the current window.
More Information
In order to implement this functionality in your Visual Basic application, follow these step:
Create a form with a WebBrowser control on it.
In the declarations section of that form, add the following:
Dim WithEvents Web_V1 as SHDocVwCtl.WebBrowser_V1
This will declare a WebBrowser_V1 variable that can receive events WebBrowser_V1 provides you with the NewWindow event.
In the Form_Load event, add the following:
Set Web_V1 = WebBrowser1.Object
WebBrowser1.Navigate2 "http://www.microsoft.com/"
This sets the WebBrowser_V1 object to the existing Internet Explorer WebBrowser object.
After the NewWindow2 event fires, the Web_V1_NewWindow event will fire with the linked URL as one of its input arguments. Remember not to set Cancel to True in NewWindow2. Also, set the Processed variable to True in the NewWindow event handler so that a new instance of Internet Explorer will not be created. The following code shows this event handler and the code necessary to navigate within the current window:
Private Sub Web_V1_NewWindow(ByVal URL As String, _
ByVal Flags As Long, _
ByVal TargetFrameName As String, _
PostData As Variant, _
ByVal Headers As String, _
Processed As Boolean)
Processed = True
WebBrowser1.Navigate URL
End Sub
Right-click a link and select "Open in New Window" and you will find the link will still open inside your WebBrowser Control.
Please note that Internet Explorer does not fire a NewWindow or NewWindow2 event when the user presses CTRL+N or points to New under the File menu and clicks Window.
References
For additional information, please see the following article in the Microsoft Knowledge Base:
Q184876 HOWTO: Use the WebBrowser Control NewWindow2 Event
For more information, see the MSDN Online Web Workshop:
http://msdn.microsoft.com/workshop/
(c) Microsoft Corporation 1998, All Rights Reserved. Contributions by Scott Roberts, Microsoft Corporation
Additional query words: NewWindow NewWindow2
Keywords: kbIE400 kbie401 kbWebBrowser kbGrpDSInet kbie500 kbDSupport kbie550
Issue Type: kbhowto
Technology: kbIEsearch kbAudDeveloper kbSDKIESearch kbIE500Search kbSDKIE400 kbSDKIE401 kbSDKIE500 kbSDKIE550 kbIE550Search
--------------------------------------------------------------------------------
Send feedback to Microsoft
© 2002-2003 Microsoft Corporation. All rights reserved.
回复人: Pole 2008-11-21 11:23:17
不是这种方法。这是用htmlView或者控件时,我是写的一个console程序,创建IE实例
回复人: YangTze 2008-11-21 11:25:33
开始写了sink类,实现invoke函数,能处理NewWindow2消息,但就做不下去了?怎么做不下去了?
回复人: Pole 2008-11-21 11:27:58
要在Newwindow2消息中,获取新的URL,让本窗口来打开这个URL,并且还要关闭新打开的窗口
首先,新的URL根本获取不了。用了几种方法都不行……
回复人: YangTze (得分:20) 2008-11-21 11:30:21
呵呵:
The application processing this notification can respond in one of three ways:
Create a new, hidden, nonnavigated WebBrowser or InternetExplorer object that is returned in ppDisp. Upon return from this event, the object that fired this event will then configure and navigate (including a BeforeNavigate2 event) the new object to the target location.
Cancel the navigation by setting Cancel to True.
Do nothing and do not set ppDisp to any value. This will cause the object that fired the event to create a new InternetExplorer object to handle the navigation.