一、添加控件并修改属性
首先打开VB,建立一个EXE文件,在该窗体上放置控件如下:三个Label控件,标题分别为"当前时间"、"欢迎你使用网络设置"和"使你能够轻松控制上网"。三个Button控件,标题分别为"确定"、"取消"和"立即登陆"。三个Text控件,两个Check控件,标题分别为"上网时间"和"下网时间",Value的值都为1,就是被选中状态,最后是两个Timer控件,Timer1的Enabled值为True,Timer2的为False,两个的Interval值都为1。调整好后如图所示。
二、编写代码
为实现网络的连接和断开需要在程序开头声明以下函数:
Const INTERNET_DIALSTATE_DISCONNECTED = 1
Const INTERNET_AUTODIAL_FORCE_ONLINE = 1
Const INTERNET_AUTODIAL_FORCE_UNATTENDED = 2
Const INTERNET_DIAL_UNATTENDED = &H8000
Dim iHandle As Long
Private Declare Function InternetDial Lib "wininet.dll" ( _ByVal hwndParent As Long, _ByVal lpszConnectoid As String, _ByVal dwFlags As Long, _lpdwConnection As Long, _ByVal dwReserved As Long) As Long
参数dwConnection指定拨号连接句柄
Private Declare Function InternetHangUp Lib "wininet.dll" _
(ByVal dwConnection As Long, _
ByVal dwReserved As Long) As Long
Private Declare Function InternetAutodial Lib "wininet.dll" _
(ByVal dwFlags As Long, ByVal dwReserved As Long) As Long
Private Declare Function InternetAutodialHangup Lib "wininet.dll" _
(ByVal dwReserved As Long) As Long
Private Sub Check1_Click()
判断用户的选择
If Check1.Value = 0 Then
Text2.Enabled = False
Check2.Value = 0
Else
Text2.Enabled = True
End If
End Sub
Private Sub Check2_Click()
判断用户的选择
If Check2.Value = 0 Then
Text3.Enabled = False
Else
Text3.Enabled = True
End If
End Sub
Private Sub Command1_Click()
使设置生效并显示设置信息
Dim alert1, alert2
If Check1.Value = 1 And Check2.Value = 1 Then
If Not IsDate(Text2.Text) Or Not IsDate(Text3.Text) Then
MsgBox ("你输入的不是时间格式,请重试!")
Else
alert1 = Text2.Text
alert2 = Text3.Text
Label2.Caption = "注意:计算机将在" + Text2.Text + "登陆网络"
Label3.Caption = "注意:计算机将在" + Text3.Text + "断开网络"
Timer1.Enabled = True
End If
Else
If Check1.Value = 1 Then
If Not IsDate(Text2.Text) Then
MsgBox ("你输入的不是时间格式,请重试!")
Else
alert1 = Text2.Text
Label2.Caption = "注意:计算机将在" + Text2.Text + "登陆网络"
Timer1.Enabled = True
End If
End If
End If
End Sub
Private Sub Command2_Click()
取消设置并显示取消信息
If Check1.Value = 1 And Check2.Value = 1 Then
Text2.Text = "00:00:00"
Text3.Text = "00:00:00"
Timer1.Enabled = False
Label2.Caption = "注意:你已取消了定时登陆网络"
Label3.Caption = "注意:你已取消了定时断开网络"
Else
If Check1.Value = 1 Then
Text2.Text = "00:00:00"
Timer1.Enabled = False
Label2.Caption = "注意:你已取消了定时登陆网络"
End If
End If
End Sub
Private Sub Command3_Click()
这里默认以163拨号方式实现登陆,其它拨号方式只需修改一下参数就可以了。
InternetDial Me.hWnd, "163", INTERNET_AUTODIAL_FORCE_UNATTENDED, iHandle, 0
End Sub
Private Sub Form_Load()
初始化上网和下网的时间格式
Text2.Text = "00:00:00"
Text3.Text = "00:00:00"
End Sub
Private Sub Timer1_Timer()
根据用户的选择来判断实现定时上下网
Dim A
If Check1.Value = 1 And Check2.Value = 1 Then
Text1.Text = Time
A = TimeValue(Text1.Text)
If Text2.Text = A Then
InternetDial Me.hWnd, "163", INTERNET_AUTODIAL_FORCE_UNATTENDED, iHandle, 0
Else
If Text3.Text = A Then
If iHandle <> 0 Then
InternetHangUp iHandle, 0
iHandle = 0
End If
End If
End If
Else
If Check1.Value = 1 Then
Text1.Text = Time
A = TimeValue(Text1.Text)
If Text2.Text = A Then
InternetDial Me.hWnd, "163", INTERNET_AUTODIAL_FORCE_UNATTENDED, iHandle, 0
End If
End If
End If
End Sub
Private Sub Timer2_Timer()
显示当前时间
Text1.Text = Time
End Sub
以上就是程序的全部源代码,编译后就是一个简单控制上网的小软件,操作简单,非常实用,以上程序在Windows98+VB6.0中调试通过。其实通过这个小程序可以看出,很多的实用小软件都源于我们日常生活中的一些小问题,只要你善于发现和思考,相信你也一样能够做到。