⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.vb

📁 Visual.Basic.NET实用编程百例-47.6M.zip
💻 VB
字号:
Friend Class Form1
    Inherits System.Windows.Forms.Form
#Region "Windows 窗体设计器生成的代码"
    Public Sub New()
        MyBase.New()
        If m_vb6FormDefInstance Is Nothing Then
            If m_InitializingDefInstance Then
                m_vb6FormDefInstance = Me
            Else
                Try
                    '对于启动窗体,所创建的第一个实例为默认实例。
                    If System.Reflection.Assembly.GetExecutingAssembly.EntryPoint.DeclaringType Is Me.GetType Then
                        m_vb6FormDefInstance = Me
                    End If
                Catch
                End Try
            End If
        End If
        '此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()
    End Sub
    '窗体重写处置,以清理组件列表。
    Protected Overloads Overrides Sub Dispose(ByVal Disposing As Boolean)
        If Disposing Then
            If Not components Is Nothing Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(Disposing)
    End Sub
    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer
    Public ToolTip1 As System.Windows.Forms.ToolTip
    Public WithEvents Winsock1 As AxMSWinsockLib.AxWinsock
    '注意: 以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器来修改它。
    '不要使用代码编辑器修改它。
    Public WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
        Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
        Me.Button1 = New System.Windows.Forms.Button
        Me.Winsock1 = New AxMSWinsockLib.AxWinsock
        CType(Me.Winsock1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.BackColor = System.Drawing.SystemColors.Control
        Me.Button1.Cursor = System.Windows.Forms.Cursors.Default
        Me.Button1.ForeColor = System.Drawing.SystemColors.ControlText
        Me.Button1.Location = New System.Drawing.Point(56, 32)
        Me.Button1.Name = "Button1"
        Me.Button1.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.Button1.Size = New System.Drawing.Size(142, 32)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "下载搜狐首页"
        '
        'Winsock1
        '
        Me.Winsock1.Enabled = True
        Me.Winsock1.Location = New System.Drawing.Point(8, 56)
        Me.Winsock1.Name = "Winsock1"
        Me.Winsock1.OcxState = CType(resources.GetObject("Winsock1.OcxState"), System.Windows.Forms.AxHost.State)
        Me.Winsock1.Size = New System.Drawing.Size(28, 28)
        Me.Winsock1.TabIndex = 1
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.BackColor = System.Drawing.SystemColors.Control
        Me.ClientSize = New System.Drawing.Size(272, 101)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.Winsock1)
        Me.Cursor = System.Windows.Forms.Cursors.Default
        Me.Location = New System.Drawing.Point(4, 23)
        Me.Name = "Form1"
        Me.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.Text = "文件下载程序"
        CType(Me.Winsock1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub
#End Region
#Region "升级支持"
    Private Shared m_vb6FormDefInstance As Form1
    Private Shared m_InitializingDefInstance As Boolean
    Public Shared Property DefInstance() As Form1
        Get
            If m_vb6FormDefInstance Is Nothing OrElse m_vb6FormDefInstance.IsDisposed Then
                m_InitializingDefInstance = True
                m_vb6FormDefInstance = New Form1
                m_InitializingDefInstance = False
            End If
            DefInstance = m_vb6FormDefInstance
        End Get
        Set(ByVal Value As Form1)
            m_vb6FormDefInstance = Value
        End Set
    End Property
#End Region
    Dim myall As String

    Private Sub Button1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button1.Click
        '建立连接
        Winsock1.Protocol = 0
        Winsock1.RemoteHost = "sohu.com"
        Winsock1.RemotePort = 80
        Winsock1.Connect() '开始提取数据
    End Sub

    Private Sub Winsock1_connectEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Winsock1.ConnectEvent
        Dim strCommand As String
        Dim strWebPage As String
        strWebPage = "Http://www.sohu.com/" '指定网页,这里是指搜狐的首页
        strCommand = "Get " & strWebPage & " HttP/1.0" & vbCrLf
        strCommand = strCommand & "accept/" & vbCrLf
        strCommand = strCommand & "accepttext/Html" & vbCrLf
        strCommand = strCommand & vbCrLf
        Winsock1.SendData(strCommand) '发送命令
    End Sub


    Private Sub Winsock1_DataarriVal(ByVal eventSender As System.Object, ByVal eventArgs As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) Handles Winsock1.DataArrival

        '开始下载,收到数据时,发生DataarriVal事件
        On Error Resume Next
        Dim WebData As String
        Winsock1.GetData(WebData, VariantType.String)
        myall = myall & WebData '取得相关的网页文件
    End Sub

    Private Sub Winsock1_closeEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Winsock1.CloseEvent '当下载完成时发生。
        '自己定义相关的操作
        'myall 是下载的网页,你可以把它显示在文本框中,也可把它保存为文件,也可通过一些自定义的函数进行信息的提取,限于篇幅,这里就不介绍了。
        Winsock1.Close() '关闭 Winsock
        MsgBox(myall, , "下载的文件内容")
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -