一、添加控件并修改属性
首先打开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()
初始化上网和下网的时间格式