标题:新手求教!关于利用WebBrowser制作浏览器的问题
只看楼主
肇励影
Rank: 2
等 级:论坛游民
威 望:2
帖 子:61
专家分:73
注 册:2016-8-5
结帖率:88.89%
已结贴  问题点数:10 回复次数:5 
新手求教!关于利用WebBrowser制作浏览器的问题
我打算做个浏览器,但是我发现在打开链接时,新的网页会出现在ie而不是程序里,怎样才能让新的网页出现在本程序里而不是外部打开IE?(注:就类似于市面上见到的浏览器,在单击一个链接时,创建一个标签并显示新的网页)

以下是图片:

(例如,打开了360网址导航)


(当单击“人民网”以后,弹出ie而不是新建标签并直接在本程序中显示人民网的网页内容)

以下是代码:
程序代码:

Private Sub WebBrowser1_NavigateComplete2(ByVal pDisp As Object, URL As Variant)
List1.ListIndex = List1.ListCount - 1
List1.AddItem (WebBrowser1.LocationURL)
End Sub

Private Sub WebBrowser1_StatusTextChange(ByVal Text As String)
Text1.Text = WebBrowser1.LocationURL
Me.Caption = "Sundy浏览器 - [" & WebBrowser1.LocationName & "]"

End Sub



以下是程序设计的界面图片:



先谢谢各位好心人了!
搜索更多相关主题的帖子: WebBrowser 人民网 网址导航 浏览器 图片 
2017-04-07 21:01
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4912
专家分:29900
注 册:2008-10-15
得分:5 
Private Sub WebBrowser1_NewWindow2(ppDisp As Object, Cancel As Boolean)
'浏览器要求新开窗口时
On Error Resume Next
Dim fr As New Form1         '新建一个窗体,里面包含一个 WebBrowser 控件

fr.Show                     '显示
Set ppDisp = fr.WebBrowser1.Object     '设置新的窗口为这个
End Sub

授人于鱼,不如授人于渔
早已停用QQ了
2017-04-07 22:04
肇励影
Rank: 2
等 级:论坛游民
威 望:2
帖 子:61
专家分:73
注 册:2016-8-5
得分:0 
回复 2楼 风吹过b
我是想做类似于360,百度之类的浏览器,单击链接后新建一个标签(界面图左侧list1),并显示新页面内容,不是再弹出一个窗口
2017-04-08 14:08
xzlxzlxzl
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖北
等 级:贵宾
威 望:125
帖 子:1091
专家分:5825
注 册:2014-5-3
得分:5 
结合你的要求,我和2楼答主同样使用了NewWindow2事件,代码及运行效果如下:
程序代码:
Private Sub Command1_Click()
  WebBrowser1.Navigate Text1
End Sub

Private Sub List1_Click()
  WebBrowser1.Navigate List1
End Sub

Private Sub WebBrowser1_NavigateComplete2(ByVal pDisp As Object, URL As Variant)
  Dim a As String, i As Integer, j As Boolean
  a = WebBrowser1.LocationURL
  j = True
  For i = 0 To List1.ListCount - 1
    If a = List1.List(i) Then
      j = False
      Exit For
    End If
  Next
  If j Then List1.AddItem a
End Sub

Private Sub WebBrowser1_NewWindow2(ppDisp As Object, Cancel As Boolean)
  Dim a As String
  a = WebBrowser1.Document.ActiveElement.href
  Cancel = True
  WebBrowser1.Navigate a
  
End Sub


2017-04-08 23:46
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4912
专家分:29900
注 册:2008-10-15
得分:0 
好吧,做了一个基本框架,你自己去完善吧。

1、窗体,一个 webbrows(0) ,一个Text1,一个Command1,一个list1
程序代码:
Option Explicit

Private Sub Command1_Click()

web(webindex).URL = Text1.Text
WebBrowser1(webindex).Navigate web(webindex).URL
web(webindex).Title = ""

End Sub

Private Sub Form_Load()


ReDim web(0)
web(0).index = 0
web(0).URL = "About:Black"

End Sub

Private Sub List1_Click()
webindex = List1.ListIndex
Text1.Text = web(webindex).URL

Call viewweb

End Sub

Private Sub WebBrowser1_BeforeNavigate2(index As Integer, ByVal pDisp As Object, URL As Variant, Flags As Variant, TargetFrameName As Variant, PostData As Variant, Headers As Variant, Cancel As Boolean)

Dim i As Long
For i = 0 To UBound(web)
    If web(i).index = index Then
        Exit For
    End If
Next i
web(i).URL = URL
List1.List(i) = URL


End Sub

Private Sub WebBrowser1_DocumentComplete(index As Integer, ByVal pDisp As Object, URL As Variant)

Dim i As Long
For i = 0 To UBound(web)
    If web(i).index = index Then
        Exit For
    End If
Next i
web(i).Title = WebBrowser1(index).Document.Title
List1.List(i) = web(i).Title



End Sub

Private Sub WebBrowser1_NewWindow2(index As Integer, ppDisp As Object, Cancel As Boolean)

'Stop

Call Addweb
Set ppDisp = WebBrowser1(web(webindex).index).Object

End Sub


Public Sub viewlist()

List1.Clear
Dim i As Long

For i = 0 To UBound(web)
If web(i).index <> -1 Then
    If web(i).Title <> "" Then
        List1.AddItem web(i).Title
    Else
        List1.AddItem web(i).URL
    End If
End If
Next i
End Sub


Public Sub viewweb()
Dim obj As WebBrowser

For Each obj In WebBrowser1
    If obj.index = webindex Then
        obj.Visible = True
    Else
        obj.Visible = False
    End If
Next

End Sub


2、模块
程序代码:
Option Explicit

Public Type 结构类型
    index   As Long     '对应 webbrows 的index
    URL     As String   '对应 webbrows 的网址
    Title   As String   '网页标题
End Type

Public web() As 结构类型        '保存整个webbrows数据
Public webindex As Long
Public webnext As Long

Public Sub DELweb(id As Long)

Dim obj As WebBrowser
Dim i As Long

Form1.List1.RemoveItem web(id).index        '删列表

For Each obj In Form1.WebBrowser1           '删对象
    If obj.index = web(id).index Then
        Unload obj
        Exit For
    End If
Next

For i = id To UBound(web()) - 1             '删数据结构
    web(i).index = web(i + 1).index
    web(i).Title = web(i + 1).Title
    web(i).URL = web(i).URL
Next i

web(i).index = -1                           '设置不可用

If web(id).index <> -1 Then
    webindex = web(id).index                    '下一个窗口为当前窗口
Else
    webindex = 0
End If

Call Form1.viewweb

Call Form1.viewlist

End Sub

Public Sub Addweb()

Dim i As Long, j As Long
Dim webmax As Long

j = -1
For i = 0 To UBound(web)
    If webmax < web(i).index Then
        webmax = web(i).index
    End If
    If web(i).index = -1 And j > -1 Then
        j = i
    End If
Next i
If j = -1 Then
    ReDim Preserve web(i)
    j = i
End If

webmax = webmax + 1
Load Form1.WebBrowser1(webmax)
web(j).index = webmax
webindex = j

'web(j).url = url
'Form1.WebBrowser1(webmax).Navigate url

Call Form1.viewweb
Call Form1.viewlist

End Sub



-------------
其实,模块里这二个过程,也应该是放在窗体里的,因为模块里操作了窗体上的控件。
已初步实现了你的新页面功能。
关闭页面功能,未实现,只写了一个实现代码,但未经任何测试。


授人于鱼,不如授人于渔
早已停用QQ了
2017-04-09 12:36
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4912
专家分:29900
注 册:2008-10-15
得分:0 

授人于鱼,不如授人于渔
早已停用QQ了
2017-04-09 12:38



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-475874-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.129298 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved