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

📄 frmmain.frm

📁 用API实现的下载文件的例子,使用方便,不需要庞大的ocx.支持代理服务器
💻 FRM
字号:
VERSION 5.00
Begin VB.Form frmMain 
   BorderStyle     =   1  'Fixed Single
   Caption         =   "文件下载测试"
   ClientHeight    =   6900
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   6450
   Icon            =   "frmMain.frx":0000
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   ScaleHeight     =   6900
   ScaleWidth      =   6450
   StartUpPosition =   3  '窗口缺省
   Begin VB.Frame Frame5 
      Height          =   525
      Left            =   60
      TabIndex        =   25
      Top             =   -30
      Width           =   6315
      Begin VB.CommandButton cmdFetch 
         Caption         =   "开始"
         Height          =   315
         Left            =   4740
         TabIndex        =   27
         Top             =   150
         Width           =   1515
      End
      Begin VB.TextBox txtURL 
         Height          =   285
         Left            =   810
         TabIndex        =   26
         Text            =   "http://www.dotdotnet.com/"
         Top             =   150
         Width           =   3885
      End
      Begin VB.Label Label1 
         AutoSize        =   -1  'True
         Caption         =   "地址:"
         Height          =   180
         Left            =   120
         TabIndex        =   28
         Top             =   210
         Width           =   540
      End
   End
   Begin VB.Frame Frame4 
      Caption         =   "结果:"
      Height          =   3405
      Left            =   60
      TabIndex        =   23
      Top             =   3390
      Width           =   6315
      Begin VB.TextBox txtOutput 
         BeginProperty Font 
            Name            =   "Courier New"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   3015
         Left            =   120
         Locked          =   -1  'True
         MultiLine       =   -1  'True
         ScrollBars      =   3  'Both
         TabIndex        =   24
         Top             =   240
         Width           =   6075
      End
   End
   Begin VB.Frame Frame1 
      Caption         =   "代理服务器"
      Height          =   2835
      Left            =   3480
      TabIndex        =   11
      Top             =   540
      Width           =   2895
      Begin VB.ComboBox cmbProxyServer 
         Height          =   300
         ItemData        =   "frmMain.frx":1CFA
         Left            =   600
         List            =   "frmMain.frx":1D0A
         Style           =   2  'Dropdown List
         TabIndex        =   17
         Top             =   240
         Width           =   2175
      End
      Begin VB.TextBox txtUsername 
         Height          =   285
         Left            =   240
         TabIndex        =   16
         Text            =   "Administrator"
         Top             =   1320
         Width           =   2535
      End
      Begin VB.TextBox txtPassword 
         Height          =   285
         IMEMode         =   3  'DISABLE
         Left            =   240
         PasswordChar    =   "*"
         TabIndex        =   15
         Text            =   "MyPassword"
         Top             =   1920
         Width           =   2535
      End
      Begin VB.TextBox txtProxy 
         Alignment       =   2  'Center
         Height          =   285
         Left            =   600
         TabIndex        =   14
         Text            =   "10.0.0.1"
         Top             =   720
         Width           =   1215
      End
      Begin VB.TextBox txtProxyPort 
         Alignment       =   2  'Center
         Height          =   285
         Left            =   2280
         TabIndex        =   13
         Text            =   "80"
         Top             =   720
         Width           =   495
      End
      Begin VB.CheckBox chkRemoteDNS 
         Caption         =   "使用远程DNS"
         Height          =   255
         Left            =   120
         TabIndex        =   12
         Top             =   2370
         Value           =   1  'Checked
         Width           =   2535
      End
      Begin VB.Label Label3 
         AutoSize        =   -1  'True
         BackStyle       =   0  'Transparent
         Caption         =   "类型:"
         Height          =   180
         Left            =   120
         TabIndex        =   22
         Top             =   270
         Width           =   540
      End
      Begin VB.Label Label4 
         AutoSize        =   -1  'True
         BackStyle       =   0  'Transparent
         Caption         =   "账号:"
         Height          =   180
         Left            =   120
         TabIndex        =   21
         Top             =   1080
         Width           =   540
      End
      Begin VB.Label Label5 
         AutoSize        =   -1  'True
         BackStyle       =   0  'Transparent
         Caption         =   "密码:"
         Height          =   180
         Left            =   120
         TabIndex        =   20
         Top             =   1680
         Width           =   540
      End
      Begin VB.Label Label6 
         AutoSize        =   -1  'True
         BackStyle       =   0  'Transparent
         Caption         =   "代理:"
         Height          =   180
         Left            =   120
         TabIndex        =   19
         Top             =   750
         Width           =   540
      End
      Begin VB.Label Label7 
         AutoSize        =   -1  'True
         BackStyle       =   0  'Transparent
         Caption         =   "端口:"
         Height          =   180
         Left            =   1830
         TabIndex        =   18
         Top             =   750
         Width           =   540
      End
   End
   Begin VB.Frame Frame2 
      Caption         =   "下载类型"
      Height          =   1815
      Left            =   60
      TabIndex        =   3
      Top             =   540
      Width           =   3255
      Begin VB.OptionButton optDlType 
         Caption         =   "下载到文件"
         Height          =   255
         Index           =   0
         Left            =   240
         TabIndex        =   10
         Top             =   240
         Width           =   2895
      End
      Begin VB.PictureBox Picture1 
         BorderStyle     =   0  'None
         Height          =   675
         Left            =   480
         ScaleHeight     =   45
         ScaleMode       =   3  'Pixel
         ScaleWidth      =   177
         TabIndex        =   6
         Top             =   480
         Width           =   2655
         Begin VB.OptionButton optDlToFile 
            Enabled         =   0   'False
            Height          =   255
            Index           =   0
            Left            =   0
            TabIndex        =   9
            Top             =   15
            Value           =   -1  'True
            Width           =   255
         End
         Begin VB.TextBox txtFilename 
            Enabled         =   0   'False
            Height          =   285
            Left            =   240
            TabIndex        =   8
            Text            =   "C:\myfile.ext"
            Top             =   0
            Width           =   2415
         End
         Begin VB.OptionButton optDlToFile 
            Caption         =   "使用暂时的文件"
            Enabled         =   0   'False
            Height          =   315
            Index           =   1
            Left            =   0
            TabIndex        =   7
            Top             =   360
            Width           =   2655
         End
      End
      Begin VB.OptionButton optDlType 
         Caption         =   "下载到缓冲器"
         Height          =   255
         Index           =   1
         Left            =   240
         TabIndex        =   5
         Top             =   1200
         Width           =   2895
      End
      Begin VB.OptionButton optDlType 
         Caption         =   "下载到下面文本框"
         Height          =   255
         Index           =   2
         Left            =   240
         TabIndex        =   4
         Top             =   1440
         Value           =   -1  'True
         Width           =   2895
      End
   End
   Begin VB.Frame Frame3 
      Caption         =   "状态"
      Height          =   975
      Left            =   60
      TabIndex        =   0
      Top             =   2400
      Width           =   3255
      Begin VB.PictureBox picProgress 
         AutoRedraw      =   -1  'True
         ForeColor       =   &H8000000D&
         Height          =   255
         Left            =   120
         ScaleHeight     =   1
         ScaleMode       =   0  'User
         ScaleWidth      =   100
         TabIndex        =   1
         Top             =   600
         Width           =   3015
      End
      Begin VB.Label lblDownloadStatus 
         BackStyle       =   0  'Transparent
         Caption         =   "Downloaded x bytes from y bytes"
         Height          =   255
         Left            =   120
         TabIndex        =   2
         Top             =   240
         Width           =   3015
      End
   End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim WithEvents RemoteFile As DataConnection
Attribute RemoteFile.VB_VarHelpID = -1
Dim DlType As DOWNLOAD_TYPE
Private Sub Form_Load()
    'Initialize WinSock... (this*must* be done)
    StartWinsock vbNullString
    'Create a new DataConnection class
    Set RemoteFile = New DataConnection
    'Set default proxy to 'No Proxy'
    cmbProxyServer.ListIndex = 0
    'Set default download type to Stream Buffer
    optDlType_Click 2
End Sub
Private Sub Form_Unload(Cancel As Integer)
    'Clean up...
    Set RemoteFile = Nothing
    EndWinsock
End Sub

Private Sub optDlType_Click(Index As Integer)
    optDlToFile(0).Enabled = (Index = 0)
    optDlToFile(1).Enabled = (Index = 0)
    txtFilename.Enabled = (Index = 0)
    DlType = Index
End Sub
Private Sub cmbProxyServer_Click()
    chkRemoteDNS.Enabled = (cmbProxyServer.ListIndex = 2 Or cmbProxyServer.ListIndex = 3)
    txtPassword.Enabled = (cmbProxyServer.ListIndex = 3)
    txtUsername.Enabled = chkRemoteDNS.Enabled
    txtProxy.Enabled = (cmbProxyServer.ListIndex <> 0)
    txtProxyPort.Enabled = (cmbProxyServer.ListIndex <> 0)
End Sub
Private Sub cmdFetch_Click()
    txtOutput.Text = ""
    With RemoteFile
        .DownloadType = DlType
        .Filename = txtFilename.Text
        .UseTempFile = optDlToFile(1).Value
        .ProxyType = cmbProxyServer.ListIndex
        .ProxyHostname = txtProxy.Text
        .ProxyPort = Val(txtProxyPort.Text)
        .ProxyUsername = txtUsername.Text
        .ProxyPassword = txtPassword.Text
        .ProxyUseRemoteDNS = (chkRemoteDNS.Value = vbChecked)
        .AutoRedirect = True
        .AllowCache = False
        .UseResume = False
        picProgress.Cls
        .Disconnect
        .MaxDownload = 1972224 '40712
        .FetchURLString txtURL.Text
        Debug.Print CStr(.SocketHandle)
    End With
End Sub
Private Sub RemoteFile_BytesReceived(lByteCount As Long, ID As Long)
    'If the script knows how many bytes that it has to receive then
    If RemoteFile.DownloadLength > 0 Then
        '... draw a progress bar
        lblDownloadStatus.Caption = "Downloaded " + CStr(lByteCount) + " bytes from " + CStr(RemoteFile.DownloadLength) + "."
        picProgress.ScaleWidth = RemoteFile.DownloadLength
        picProgress.Line (0, 0)-(lByteCount, 1), , BF
    'Or else, simply show how many bytes we have received
    Else
        lblDownloadStatus.Caption = "Downloaded " + CStr(lByteCount) + " bytes."
    End If
End Sub
Private Sub RemoteFile_Connected(ID As Long)
    'Successfully connected to the remote host
    Debug.Print "Connected"
End Sub
Private Sub RemoteFile_Disconnected(ID As Long)
    Debug.Print "Disconnected"
    'If we have downloaded everything to a buffer...
    If RemoteFile.DownloadType = dtToBuffer Then
        '... then show it
        txtOutput.Text = RemoteFile.GetBufferAsString
        RemoteFile.ClearBuffer
    End If
    'Tell the user the download is complete
    If RemoteFile.DownloadType = dtToFile And RemoteFile.UseTempFile Then
        MsgBox "Download completed!" + vbCrLf + "Result saved to " + RemoteFile.Filename, vbInformation
    Else
        MsgBox "Download completed!", vbInformation
    End If
End Sub
Private Sub RemoteFile_DownloadFailed(Why As DOWNLOAD_FAILURE, ID As Long)
    'Uhoh... the download failed :(
    Debug.Print "Download Failed"
    MsgBox "The download failed... Error code " + CStr(Why), vbExclamation
End Sub
Private Sub RemoteFile_StreamBytes(lByteCount As Long, bBytes() As Byte, ID As Long)
    'Show the received bytes
    txtOutput.Text = txtOutput.Text + StrConv(bBytes, vbUnicode)
End Sub

⌨️ 快捷键说明

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