源代碼地址:http://pan.baidu.com/s/1sjywCH3

怎么讓下載地址為一個全域變數,而不是文本框。 下載路徑也不用彈出這種提示框

直接為 App.Path & "\"就可以。
表單代碼:
Option Explicit
Private downerr As Boolean
Private downing As Boolean
Private downtime As Long
Private Sub Command1_Click()
If Len(txtUrl.Text) < 1 Then
MsgBox "請輸入要下載的檔案路徑!", vbInformation, "檔案下載器"
txtUrl.SetFocus
Exit Sub
End If
On Error GoTo TrackErr
With cdgFile
.FileName = getFileName(txtUrl.Text)
.Filter = "所有檔案(*.*)|*.*"
.CancelError = True
.ShowSave
If Len(.FileName) > 0 Then
downerr = False
Command1.Enabled = False
Label1.Caption = "正在下載檔案:" & .FileName & "," & 0 & "/" & 0 & "位元組。"
Caption = "開始下載。。。"
txtUrl.Locked = True
downing = True
downtime = 0
Label3.Caption = "用時:0 秒。"
Timer1.Enabled = True
DoEvents
Downloader1.BeginDownload txtUrl.Text, .FileName
End If
End With
Exit Sub
TrackErr:
If Err.Number <> 32755 Then MsgBox "發生了錯誤!原因:" & Err.Description, vbCritical, "下載錯誤!"
Err.Clear
End Sub
Private Sub Downloader1_DownloadComplete(ByVal maxbytes As Long, ByVal sLocalFile As String)
Timer1.Enabled = False
Command1.Enabled = True
txtUrl.Locked = False
Caption = "檔案下載結束"
If maxbytes > 0 Then
MsgBox "檔案下載完成!大小:" & maxbytes & "位元組。", vbInformation, "檔案下載器"
If Not downerr Then txtUrl.Text = ""
End If
txtUrl.SetFocus
downing = False
End Sub
Private Sub Downloader1_DownloadError(ByVal sLocalFile As String, ByVal ErrDescription As String)
downerr = True
MsgBox sLocalFile & "下載錯誤!" & vbCrLf & "原因:" & vbCrLf & ErrDescription, vbExclamation, "檔案下載器"
End Sub
Private Sub Downloader1_DownloadProgress(ByVal curbytes As Long, ByVal maxbytes As Long, ByVal sLocalFile As String, ByVal lSpeed As Long, ByVal lRemnantTime As Long)
With ucGuage1
.Max = maxbytes
.Value = curbytes
End With
Label1.Caption = "正在下載檔案:" & sLocalFile & vbCrLf & "下載速度:" & ToFormatByteToString(lSpeed) & "每秒" & vbCrLf & "剩余時間:" & ToFormatTimeToString(lRemnantTime) & vbCrLf & "下載進度:" & ToFormatByteToString(curbytes) & "/" & ToFormatByteToString(maxbytes) & "。"
Caption = "正在下載:完成 " & Format(curbytes / maxbytes * 100, "0.00") & " %。。。"
DoEvents
End Sub
Private Function ToFormatByteToString(ByVal num As Long) As String
Select Case num
Case Is < 0
Case 0 To 1023
ToFormatByteToString = CStr(num) & " 位元組"
Case 1024 To 1048575
ToFormatByteToString = Format(num / 1024, "0.000") & " K 位元組"
Case 1048576 To 1073741823
ToFormatByteToString = Format(num / 1048576, "0.000") & " M 位元組"
Case Else
ToFormatByteToString = Format(num / 1073741824, "0.000") & " G 位元組"
End Select
End Function
Private Function ToFormatTimeToString(ByVal num As Long) As String
Dim mh As Long, mm As Long, ms As Long, mstr As String
mh = num \ 3600 '小時
mm = (num Mod 3600) \ 60 '分鐘
ms = num Mod 60 '秒數
If mh > 0 Then mstr = mh & "小時"
If mm > 0 Then mstr = mstr & mm & "分鐘"
If ms > 0 Then mstr = mstr & ms & "秒"
If Len(mstr) < 1 Then mstr = "0秒"
Debug.Print "時間:" & num & "秒," & mstr
ToFormatTimeToString = mstr
End Function
Private Sub Form_Unload(Cancel As Integer)
If downing Then
If MsgBox("正在下載檔案!您確認要退出并放棄當前的下載?", vbOKCancel + vbQuestion, "檔案下載器") = vbCancel Then
Cancel = 1
End If
End If
End Sub
Private Sub Timer1_Timer()
downtime = downtime + 1
Label3.Caption = "用時:" & ToFormatTimeToString(downtime) & "。"
End Sub
Private Sub txtUrl_Change()
txtUrl.ToolTipText = txtUrl.Text
End Sub
Private Sub txtUrl_GotFocus()
With txtUrl
.SelStart = 0
.SelLength = Len(.Text)
End With
End Sub
Private Function getFileName(ByVal strPath As String) As String
Dim strTemp As String
If Len(strPath) = 0 Then Exit Function
If InStr(1, strPath, "\") = 0 Then
If InStr(1, strPath, "/") = 0 Then
getFileName = strPath
Else
strTemp = StrReverse(strPath)
getFileName = StrReverse(Mid(strTemp, 1, InStr(1, strTemp, "/") - 1))
End If
Else
strTemp = StrReverse(strPath)
getFileName = StrReverse(Mid(strTemp, 1, InStr(1, strTemp, "\") - 1))
End If
End Function
自定義控制元件代碼:
Option Explicit
Private mtime As Date
Event DownloadProgress(ByVal curbytes As Long, ByVal maxbytes As Long, ByVal sLocalFile As String, ByVal lSpeed As Long, ByVal lRemnantTime As Long)
Event DownloadError(ByVal sLocalFile As String, ByVal ErrDescription As String)
Event DownloadComplete(ByVal maxbytes As Long, ByVal sLocalFile As String)
Dim BytMask(0 To 15) As Long
Private Sub UserControl_AsyncReadComplete(AsyncProp As AsyncProperty)
On Error Resume Next
Dim bytValue() As Byte, fn As Long
If AsyncProp.BytesMax <> 0 Then
fn = FreeFile
bytValue = AsyncProp.Value
Open AsyncProp.PropertyName For Binary Access Write As #fn
Put #fn, , bytValue
Close #fn
Else
RaiseEvent DownloadError(AsyncProp.PropertyName, "檔案大小為零位元組!")
End If
RaiseEvent DownloadComplete(CLng(AsyncProp.BytesMax), AsyncProp.PropertyName)
End Sub
Private Sub UserControl_AsyncReadProgress(AsyncProp As AsyncProperty)
Dim mspeed As Long, mmax As Long, mread As Long
On Error Resume Next
mmax = AsyncProp.BytesMax
mread = AsyncProp.BytesRead
mspeed = CLng(mread \ DateDiff("s", mtime, Now))
If mmax <> 0 Then
RaiseEvent DownloadProgress(CLng(mread), CLng(mmax), AsyncProp.PropertyName, mspeed, (mmax - mread) \ mspeed)
End If
End Sub
Private Sub UserControl_Initialize()
SizeIt
End Sub
Private Sub UserControl_Resize()
SizeIt
End Sub
Public Sub BeginDownload(ByVal url As String, ByVal sLocalFile As String)
Dim hr As Long, strTemp As String
If Len(url) = 0 Then
MsgBox "指定的URL無效!", vbCritical, "URL無效"
CancelDown sLocalFile
Exit Sub
End If
strTemp = sLocalFile
While Dir(strTemp, vbNormal) <> Empty Or Len(strTemp) = 0
If Len(strTemp) = 0 Then
hr = vbNo
MsgBox "檔案路徑不能為空!", vbExclamation, "錯誤"
Else
hr = MsgBox("檔案已經存在!是否要覆寫原來的檔案?", vbQuestion + vbYesNoCancel, "替換檔案")
End If
Select Case hr
Case vbYes '替換舊檔案
SetAttr sLocalFile, vbNormal
Kill sLocalFile
Case vbNo '不替換
strTemp = InputBox("請重新指定存放路徑!", "重新指定存放路徑", sLocalFile)
If Len(strTemp) = 0 Then
CancelDown sLocalFile
Exit Sub
End If
Case vbCancel
CancelDown sLocalFile
Exit Sub
End Select
Wend
sLocalFile = strTemp
On Error GoTo ErrorBeginDownload
mtime = Now
UserControl.AsyncRead URLEncode(url), vbAsyncTypeByteArray, sLocalFile, vbAsyncReadForceUpdate
Exit Sub
ErrorBeginDownload:
RaiseEvent DownloadError(sLocalFile, Err.Description)
CancelDown sLocalFile
Exit Sub
End Sub
Private Sub SizeIt()
On Error Resume Next
With UserControl
.Width = ScaleX(32, vbPixels, vbTwips)
.Height = ScaleY(32, vbPixels, vbTwips)
End With
Exit Sub
End Sub
Private Sub CancelDown(ByVal strPath As String)
RaiseEvent DownloadComplete(0&, strPath)
End Sub
Public Function URLEncode(ByVal strURL As String) As String
If Len(strURL) = 0 Then Exit Function
Dim strTemp As String, lngTemp As Long
Dim lngLeng As Long, i As Long
lngLeng = Len(strURL)
For i = 1 To lngLeng
lngTemp = CharToAscii(Mid(strURL, i, 1))
Select Case lngTemp
Case 32
strTemp = strTemp & "%20"
Case 0 To 255
strTemp = strTemp & Chr(lngTemp)
Case Else
strTemp = strTemp & "%" & _
IIf(((lngTemp And &HFF00) \ 256) > 15, _
Hex((lngTemp And &HFF00) \ 256), _
"0" & Hex((lngTemp And &HFF00) \ 256)) & "%" & _
IIf((lngTemp And &HFF) > 15, _
Hex(lngTemp And &HFF), _
"0" & Hex(lngTemp And &HFF))
End Select
Next
URLEncode = strTemp
End Function
'將字串中的第一個字符轉換為ASCII碼,保證是正數,因為在某些情況下,負數不好使用
Private Function CharToAscii(ByVal strValue As String) As Long
If Len(strValue) = 0 Then Exit Function
Dim lngChar As Long, lngAscii As Long
lngChar = Asc(Mid(strValue, 1, 1))
Select Case lngChar
Case 0 To 255
CharToAscii = lngChar
Case Is > 255
CharToAscii = lngChar
Case Else
CharToAscii = lngChar + 65536
End Select
End Function
uj5u.com熱心網友回復:
第二個控制元件代碼:Option ExplicitConst MIN_WIDTH = 48 '控制元件最小寬度
Const MIN_HEIGHT = 16 '控制元件最小高度
Const FIX_MARGIN = 2 '控制元件內部固定空間
Const MAX_BORDER_WIDTH = 5 '最大邊框寬度
Const MAX_BORDER_STYLE_VALUE = 5 '最大邊框樣式值
Const m_def_BackColor = vbButtonFace '默認控制元件背景顏色為[按鈕表面](系統顏色)
Const m_def_ForeColor = vbWindowText '默認控制元件字體顏色為視窗文本(系統顏色)
Const m_def_GuageColor = vbWindowText '默認進度顏色為視窗文本(系統顏色)
Const m_def_ShadowColor = vb3DShadow '默認陰影顏色為3D陰影顏色(系統顏色)
Const m_def_HighlightColor = vb3DHighlight '默認高亮顏色為3D高亮顏色(系統顏色)
Const m_def_BorderStyle = 1 '默認控制元件邊框樣式為凹陷效果
Const m_def_BorderWidth = 1 '默認控制元件邊框寬度為1
Const m_def_Value = 0 '默認進度值為0
Const m_def_Max = 100 '默認最大值
Const m_def_Min = 0 '默認最小值
Dim m_BackColor As OLE_COLOR '控制元件背景顏色
Dim m_ForeColor As OLE_COLOR '控制元件字體顏色
Dim m_GuageColor As OLE_COLOR '控制元件進度顏色
Dim m_ShadowColor As OLE_COLOR '控制元件陰影顏色
Dim m_HighlightColor As OLE_COLOR '控制元件高亮顏色
Dim m_BorderStyle As Integer '控制元件邊框樣式
Dim m_BorderWidth As Integer '控制元件邊框寬度
Dim m_Value As Double '進度值
Dim m_Max As Double '最大值
Dim m_Min As Double '最小值
Dim m_ScaleX As Single, m_ScaleY As Single '
Dim m_ClientWidth As Single '
Dim m_PropertySeted As Boolean '屬性是否初始化
'控制元件背景顏色屬性
Public Property Get BackColor() As OLE_COLOR
BackColor = m_BackColor
End Property
Public Property Let BackColor(ByVal New_BackColor As OLE_COLOR)
If m_BackColor = New_BackColor Then Exit Property
m_BackColor = New_BackColor
UserControl.BackColor() = New_BackColor
Call DrawControlBorder(m_BorderStyle)
Call PropertyChanged("BackColor")
End Property
'控制元件字體顏色屬性
Public Property Get ForeColor() As OLE_COLOR
ForeColor = m_ForeColor
End Property
Public Property Let ForeColor(ByVal New_ForeColor As OLE_COLOR)
m_ForeColor = New_ForeColor
lblValue.ForeColor = New_ForeColor
Call PropertyChanged("ForeColor")
End Property
'控制元件進度顏色屬性
Public Property Get GuageColor() As OLE_COLOR
GuageColor = m_GuageColor
End Property
Public Property Let GuageColor(ByVal New_GuageColor As OLE_COLOR)
m_GuageColor = New_GuageColor
shpMask.FillColor = New_GuageColor
shpMask.BorderColor = New_GuageColor
Call PropertyChanged("GuageColor")
End Property
'控制元件陰影顏色屬性
Public Property Get ShadowColor() As OLE_COLOR
ShadowColor = m_ShadowColor
End Property
Public Property Let ShadowColor(ByVal New_ShadowColor As OLE_COLOR)
If m_ShadowColor = New_ShadowColor Then Exit Property
Call DrawControlBorder(0) '清除以前的邊框
m_ShadowColor = New_ShadowColor
Call ShowValue
Call PropertyChanged("ShadowColor")
End Property
'控制元件高亮顏色屬性
Public Property Get HighlightColor() As OLE_COLOR
HighlightColor = m_HighlightColor
End Property
Public Property Let HighlightColor(ByVal New_HighlightColor As OLE_COLOR)
If m_HighlightColor = New_HighlightColor Then Exit Property
Call DrawControlBorder(0)
m_HighlightColor = New_HighlightColor
Call ShowValue
Call PropertyChanged("HighlightColor")
End Property
'控制元件邊框樣式屬性
Public Property Get BorderStyle() As Integer
BorderStyle = m_BorderStyle
End Property
Public Property Let BorderStyle(ByVal New_BorderStyle As Integer)
If m_BorderStyle = New_BorderStyle Then Exit Property
Call DrawControlBorder(0) '清除以前的邊框
Select Case New_BorderStyle
Case Is < 0
m_BorderStyle = 0
Case Is > MAX_BORDER_STYLE_VALUE '最大邊框樣式值
m_BorderStyle = MAX_BORDER_STYLE_VALUE
Case Else
m_BorderStyle = New_BorderStyle
End Select
Call ShowValue
Call PropertyChanged("BorderStyle")
End Property
'控制元件邊框寬度屬性
Public Property Get BorderWidth() As Integer
BorderWidth = m_BorderWidth
End Property
Public Property Let BorderWidth(ByVal New_BorderWidth As Integer)
If m_BorderWidth = New_BorderWidth Then Exit Property
Call DrawControlBorder(0) '清除以前的邊框
Select Case New_BorderWidth
Case Is < 1
m_BorderWidth = 1
Case Is > MAX_BORDER_WIDTH
m_BorderWidth = MAX_BORDER_WIDTH
Case Else
m_BorderWidth = New_BorderWidth
End Select
Call ShowValue
Call PropertyChanged("BorderWidth")
End Property
'控制元件進度值屬性
Public Property Get Value() As Double
Value = m_Value
End Property
Public Property Let Value(ByVal New_Value As Double)
Select Case New_Value
Case Is < m_Min
m_Value = m_Min
Case Is > m_Max
m_Value = m_Max
Case Else
m_Value = New_Value
End Select
Call ShowValue
Call PropertyChanged("Value")
End Property
Public Property Get Max() As Double
Max = m_Max
End Property
Public Property Let Max(ByVal New_Max As Double)
m_Max = New_Max
If m_Value > New_Max Then m_Value = New_Max
Call ShowValue
Call PropertyChanged("Max")
End Property
Public Property Get Min() As Double
Min = m_Min
End Property
Public Property Let Min(ByVal New_Min As Double)
m_Min = New_Min
If m_Value < New_Min Then m_Value = New_Min
Call ShowValue
Call PropertyChanged("Min")
End Property
Private Sub UserControl_Initialize()
'
m_ScaleX = Width / ScaleWidth
m_ScaleY = Height / ScaleHeight
End Sub
Private Sub UserControl_InitProperties()
'
m_BackColor = m_def_BackColor
m_ForeColor = m_def_ForeColor
m_GuageColor = m_def_GuageColor
m_ShadowColor = m_def_ShadowColor
m_HighlightColor = m_def_HighlightColor
m_BorderStyle = m_def_BorderStyle
m_BorderWidth = m_def_BorderWidth
m_Value = m_def_Value
m_Max = m_def_Max
m_Min = m_def_Min
UserControl.BackColor() = m_BackColor
lblValue.ForeColor = m_ForeColor
shpMask.FillColor = m_GuageColor
shpMask.BorderColor = m_GuageColor
m_PropertySeted = True
Call ShowValue
End Sub
Private Sub UserControl_Paint()
Call DrawControlBorder(m_BorderStyle) '重畫邊框
End Sub
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
'
m_BackColor = PropBag.ReadProperty("BackColor", m_def_BackColor)
m_ForeColor = PropBag.ReadProperty("ForeColor", m_def_ForeColor)
m_GuageColor = PropBag.ReadProperty("GuageColor", m_def_GuageColor)
m_ShadowColor = PropBag.ReadProperty("ShadowColor", m_def_ShadowColor)
m_HighlightColor = PropBag.ReadProperty("HighlightColor", m_def_HighlightColor)
m_BorderStyle = PropBag.ReadProperty("BorderStyle", m_def_BorderStyle)
m_BorderWidth = PropBag.ReadProperty("BorderWidth", m_def_BorderWidth)
m_Value = PropBag.ReadProperty("Value", m_def_Value)
m_Max = PropBag.ReadProperty("Max", m_def_Max)
m_Min = PropBag.ReadProperty("Min", m_def_Min)
UserControl.BackColor() = m_BackColor
lblValue.ForeColor = m_ForeColor
shpMask.FillColor = m_GuageColor
shpMask.BorderColor = m_GuageColor
m_PropertySeted = True
Call ShowValue
End Sub
Private Sub UserControl_Resize()
With UserControl.Extender
If ScaleWidth < MIN_WIDTH Then .Width = m_ScaleX * MIN_WIDTH
If ScaleHeight < MIN_HEIGHT Then .Height = m_ScaleY * MIN_HEIGHT
End With
If m_PropertySeted Then
Call ShowValue
Else
Call DrawControlBorder(m_BorderStyle) '屬性沒加載時,只顯示邊框,不顯示進度
End If
End Sub
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
'
Call PropBag.WriteProperty("BackColor", m_BackColor, m_def_BackColor)
Call PropBag.WriteProperty("ForeColor", m_ForeColor, m_def_ForeColor)
Call PropBag.WriteProperty("GuageColor", m_GuageColor, m_def_GuageColor)
Call PropBag.WriteProperty("ShadowColor", m_ShadowColor, m_def_ShadowColor)
Call PropBag.WriteProperty("HighlightColor", m_HighlightColor, m_def_HighlightColor)
Call PropBag.WriteProperty("BorderStyle", m_BorderStyle, m_def_BorderStyle)
Call PropBag.WriteProperty("BorderWidth", m_BorderWidth, m_def_BorderWidth)
Call PropBag.WriteProperty("Value", m_Value, m_def_Value)
Call PropBag.WriteProperty("Max", m_Max, m_def_Max)
Call PropBag.WriteProperty("Min", m_Min, m_def_Min)
End Sub
Private Sub ShowValue()
'畫邊框
Call DrawControlBorder(m_BorderStyle)
'顯示進度
With lblValue
.Caption = Format((m_Value - m_Min) / (m_Max - m_Min), "#0.00%")
.Move (ScaleWidth - .Width) / 2, (ScaleHeight - .Height) / 2
End With
m_ClientWidth = ScaleWidth - (m_BorderWidth + FIX_MARGIN) * 2
shpMask.Visible = (m_Value > m_Min)
If (m_Value > m_Min) Then shpMask.Move m_BorderWidth + FIX_MARGIN, _
m_BorderWidth + FIX_MARGIN, (m_Value - m_Min) * (m_ClientWidth / (m_Max - m_Min)), _
ScaleHeight - (m_BorderWidth + FIX_MARGIN) * 2
DoEvents
End Sub
Private Sub DrawControlBorder(ByVal ds As Integer)
'目前只實作了三種樣式:無邊框,凹陷效果,凸起效果
Select Case ds
Case 0 '無邊框,作用背景顏色,覆寫以前的邊框
Line (0, 0)-(ScaleWidth - 1, m_BorderWidth - 1), m_BackColor, BF
Line (0, 0)-(m_BorderWidth - 1, ScaleHeight - 1), m_BackColor, BF
Line (0, ScaleHeight - (m_BorderWidth - 1) - 1)-(ScaleWidth - 1, ScaleHeight - 1), m_BackColor, BF
Line (ScaleWidth - (m_BorderWidth - 1) - 1, 0)-(ScaleWidth - 1, ScaleHeight - 1), m_BackColor, BF
Case 1 '凹陷效果
Line (0, 0)-(ScaleWidth - 1, m_BorderWidth - 1), m_ShadowColor, BF
Line (0, 0)-(m_BorderWidth - 1, ScaleHeight - 1), m_ShadowColor, BF
Line (0, ScaleHeight - (m_BorderWidth - 1) - 1)-(ScaleWidth - 1, ScaleHeight - 1), m_HighlightColor, BF
Line (ScaleWidth - (m_BorderWidth - 1) - 1, 0)-(ScaleWidth - 1, ScaleHeight - 1), m_HighlightColor, BF
Case 2 '凸起效果
Line (0, 0)-(ScaleWidth - 1, m_BorderWidth - 1), m_HighlightColor, BF
Line (0, 0)-(m_BorderWidth - 1, ScaleHeight - 1), m_HighlightColor, BF
Line (0, ScaleHeight - (m_BorderWidth - 1) - 1)-(ScaleWidth - 1, ScaleHeight - 1), m_ShadowColor, BF
Line (ScaleWidth - (m_BorderWidth - 1) - 1, 0)-(ScaleWidth - 1, ScaleHeight - 1), m_ShadowColor, BF
End Select
End Sub
uj5u.com熱心網友回復:
Downloader1.BeginDownload 全域變數, App.Path & "\檔案名"
uj5u.com熱心網友回復:
已經弄好了不過還是感謝
uj5u.com熱心網友回復:
已經弄好了不過還是感謝
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/89581.html
標籤:網絡編程
