请教,如何使用htmlview把所有新开的连接都在一个窗口内显示

chenmu_2002 2003-03-13 10:58:33
如题
...全文
92 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenmu_2002 2003-03-21
  • 打赏
  • 举报
回复
作如下改动结果死机了,其是我主要是为了在htmlview中显示系统目录,
即Navigate2("c:\\")之类,为了让他在双击的情况下不出新窗口。(发现其view变成了listview,呵呵,怎么办?)
网页我觉得还是出新窗口比较好:)
IUnknown * pUnk = this->m_wndBrowser.GetControlUnknown();
if(pUnk)
{
IDispatch * pDisp = NULL;
HRESULT hr = pUnk->QueryInterface(IID_IDispatch,(void**)&pDisp);
if(SUCCEEDED(hr)&&pDisp)
*ppDisp = pDisp;
}
*Cancel = true;
蒋晟 2003-03-20
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/1477/1477671.xml?temp=.2194025
chenmu_2002 2003-03-20
  • 打赏
  • 举报
回复
好东东,可惜在VC里面没有地方加入这个属性,555555555 气死人了
kingcom_xu 2003-03-20
  • 打赏
  • 举报
回复
Knowledge Base

HOWTO: Cause Navigation to Occur in Same WebBrowser WindowPSS 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.

kingcom_xu 2003-03-15
  • 打赏
  • 举报
回复
我在VB中试了好像不行
蒋晟 2003-03-15
  • 打赏
  • 举报
回复
void CMyHtmlView::OnNewWindow2(LPDISPATCH* ppDisp, BOOL* Cancel)
把ppDisp赋值为当前Webbrowser对象就行了
kingcom_xu 2003-03-14
  • 打赏
  • 举报
回复
up and gz
chenmu_2002 2003-03-14
  • 打赏
  • 举报
回复
我不是要打开的窗口变成我的HtmlView,而是要所有的连接都在一个窗口内打开,不要弹出。
chenmu_2002 2003-03-13
  • 打赏
  • 举报
回复
知道肯定用它,可是如果只是把*Cancel置为真,那新开的所有窗口根本就不会打开了。
需要的是NewWindow2中怎么做?:)
kingcom_xu 2003-03-13
  • 打赏
  • 举报
回复
DWebBrowserEvents2::NewWindow2 Event
?
蒋晟 2003-03-13
  • 打赏
  • 举报
回复
Keep Passing the Open Windows
Dear Web Team:

I am developing a custom browser using the Microsoft Foundation Class (MFC) CHtmlView class. I want to make all windows open within my custom browser, and not in Microsoft Internet Explorer.

Dhiren Vyas

The Web Team replies:

As you probably already know, developing your own custom browser gives you complete control over your user interface whilst making use of the incredible technology that Internet Explorer has to offer. You get to choose what your browser looks like and what it can do for your customers. Microsoft Visual C++ makes this task even easier by providing an MFC class, CHtmlView, that implements a view based on the WebBrowser control—a reusable component of Internet Explorer.

There are obstacles, though. When a Web page opens a window (by calling window.open, for example) an Internet Explorer object is created. This means that the Web page will appear in an Internet Explorer window. If you want all windows to appear in your application, you can use the NewWindow2 event, available on the DWebBrowserEvents2 interface. We'll demonstrate how this event can be used in a multiple document interface (MDI) MFC application. We'll be brief because this subject is covered in detail in the Knowledge Base article HOWTO: Use the WebBrowser Control NewWindow2 Event (Q184876).

First let's create an MDI MFC application using the AppWizard. During the final page of the wizard, select a view class that is derived from CHtmlView. Then use the ClassWizard to add an event handler for the NewWindow2 event. Finally, add code to your NewWindow2 event handler function:

void CMyHtmlView::OnNewWindow2(LPDISPATCH* ppDisp, BOOL* Cancel)
{
// Get a pointer to the application object.
CWinApp* pApp = AfxGetApp();

// Get the correct document template.
POSITION pos = pApp->GetFirstDocTemplatePosition();
CDocTemplate* pDocTemplate = pApp->GetNextDocTemplate( pos );

// Create a new frame.
CFrameWnd* pFrame = pDocTemplate->CreateNewFrame(
GetDocument(),
(CFrameWnd*)AfxGetMainWnd() );

// Activate the frame.
pDocTemplate->InitialUpdateFrame( pFrame, NULL );
CNewWindow2View* pView = (CNewWindow2View*)pFrame->GetActiveView();

// Pass pointer of WebBrowser object.
pView->SetRegisterAsBrowser( TRUE );
*ppDisp = pView->GetApplication();
}

Navigate to a Web page that opens a new window and you'll see the Web page displayed in your application. This works because your application intercepts the NewWindow2 event, creates a new document/frame/view combination, and passes the IDispatch for the WebBrowser object. This causes your CHtmlView-derived class to be used to display the Web page. Note that the WebBrowser object must be created each time and not have navigated to a URL, or this won’t work.

You'll probably want to make this more useful by obtaining the window information, such as the height and width, so that you can modify your view accordingly. A simple approach to this is to handle the BeforeNavigate2 event that is fired after the new window is created. Add the following code to your application to resize the view to the size of the new window. Note that this is an overridden method that allows access to the WebBrowser object, and that an additional Boolean member, m_bResizeWindow, is required. Set this to false in the constructor and true in the NewWindow2 event handler to ensure this code is only called for new windows.

void CMyHtmlView::BeforeNavigate2(LPDISPATCH pDisp, VARIANT* URL,
VARIANT* Flags, VARIANT* TargetFrameName,
VARIANT* PostData, VARIANT* Headers, BOOL* Cancel)
{
if ( m_bResizeWindow )
{
IWebBrowser2* pWB = NULL;

// QI the dispatch for WebBrowser control.
HRESULT hr = pDisp->QueryInterface( IID_IWebBrowser2,
(void**)&pWB );
if ( SUCCEEDED(hr) )
{
long x,y;

// Get dimensions.
pWB->get_Width( &x );
pWB->get_Height( &y );

// Resize frame.
SetScrollSizes( MM_TEXT, CSize(x,y) );
ResizeParentToFit();
pWB->Release();
}
m_bResizeWindow = false;
}
}

3,055

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC HTML/XML
社区管理员
  • HTML/XML社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧