WebBrowse控件的使用 VB6

时间:2022-06-09 01:50:45

采用C/S和B/S结合起来做某一项目,在C/S中,调用 WebBrowse控件,调用 B/S中对应的页面,理论上可以满足,

存在以下问题,

1.身份验证问题,

2.WebBrowse控件中建立的SessionID会话,和IE中的值不同,(同一客户端),由于原来不同页面之间使用了Session保存数据,所以 这个总是必须解决:

      查了资料以后,通过以下方式可以解决,处理NewWindow2事件,对于弹出窗口大小及位置,标题,关闭等问题,通过WindowClosing,NavigateComplete2,WindowSetTop,
  WindowSetLeft,WindowSetWidth,WindowSetHeight事件处理
等事件处理,源码如下:

 

'说明,建立Form1,Form2, Form1上放WebBrowse1,Command1,Text1;From2上放WebBrowse1

'Form1代码

Option Explicit

Private Sub Command1_Click()
    Me.WebBrowser1.Navigate (Text1.Text)
End Sub

Private Sub Form_Load()
    Text1.Text = "http://localhost:8088/pxgl/WebModules/Login/Logincs.aspx?UserID=000HLD0000&module=1"
    Me.WebBrowser1.AddressBar = True
    Me.WebBrowser1.ToolBar = True
    Me.WebBrowser1.MenuBar = True
End Sub

Private Sub Form_Resize()
    On Error Resume Next
    Me.WebBrowser1.Width = Me.Width - (Me.WebBrowser1.Left * 2) - 200
    Me.WebBrowser1.Height = Me.Height - Me.WebBrowser1.Top - 400
End Sub

'新窗口处理
Private Sub WebBrowser1_NewWindow2(ppDisp As Object, Cancel As Boolean)
    Dim frm2 As New Form2
    frm2.WebBrowser1.RegisterAsBrowser = True
    Set ppDisp = frm2.WebBrowser1.object
    frm2.Show

End Sub
 

 

'Form2代码

 

Option Explicit


Private Sub Form_Load()
    Me.ScaleMode = 1
End Sub

Private Sub Form_Resize()
    On Error Resume Next
    Me.WebBrowser1.Width = Me.Width - 200
    Me.WebBrowser1.Height = Me.Height - 400
    Me.WebBrowser1.Visible = True
End Sub

'新窗口处理
Private Sub WebBrowser1_NewWindow2(ppDisp As Object, Cancel As Boolean)
    Dim frm2 As New Form2
    frm2.WebBrowser1.RegisterAsBrowser = True
    Set ppDisp = frm2.WebBrowser1.object
    frm2.Show

End Sub


'关闭窗口
Private Sub WebBrowser1_WindowClosing(ByVal IsChildWindow As Boolean, Cancel As Boolean)
    Unload Me
End Sub

'以下设置窗口大小

Private Sub WebBrowser1_NavigateComplete2(ByVal pDisp As Object, URL As Variant)
    Me.Caption = pDisp.LocationName
    Call Form_Resize
End Sub

Private Sub WebBrowser1_WindowSetHeight(ByVal Height As Long)
    On Error Resume Next
    Me.Height = Me.ScaleY(Height, vbPixels, Me.ScaleMode)
End Sub

Private Sub WebBrowser1_WindowSetLeft(ByVal Left As Long)
    On Error Resume Next
    Me.Left = Me.ScaleY(Left, vbPixels, Me.ScaleMode)
End Sub

Private Sub WebBrowser1_WindowSetTop(ByVal Top As Long)
    On Error Resume Next
    Me.Top = Me.ScaleX(Top, vbPixels, Me.ScaleMode)
End Sub

Private Sub WebBrowser1_WindowSetWidth(ByVal Width As Long)
    On Error Resume Next
    Me.Width = Me.ScaleX(Width, vbPixels, Me.ScaleMode)
End Sub

 

'结论

解决
1.WebBrowse与IE之间的Session丢失问题.
 通过WebBrowser1_NewWindow2事件处理

2.弹出窗口大小,标题,自动关闭,问题
 通过WebBrowser1_WindowClosing,NavigateComplete2,WindowSetTop,
  WindowSetLeft,WindowSetWidth,WindowSetHeight事件处理