开头的时候本想用 My.Computer.Network.IsAvailable 来判断网络状态的,但发现不行,最后改用 ping 8.8.8.8 的方式来判断,8.8.8.8是谷歌的公共DNS。
界面设定图:
此外,程序还包含一个Timer控件用于定时执行命令,一个NotifyIcon控件用于显示托盘图标,一个ContextMenuStrip控件用于托盘图标的右键菜单。
源代码:
Public Class monitor1
Dim i As Integer = 0
Dim a As String
Private Sub Form1_Load(sender As System.Object,e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
a = My.Computer.Network.Ping("8.8.8.8") '通过Ping 8.8.8.8来判断网络是否连通
If a = True Then
Label1.Text = "网络状态:网络已连接" '如果网络已连接,则显示
Light1.FillColor = Color.Green '同时将“指示灯”颜色更改为绿色
Light1.FillGradientColor = Color.LightGreen
NotifyIcon1.Text = "网络监视器" & Chr(13) & "网络状态:网络已连接"
Else
Label1.Text = "网络状态:网络未连接"
NotifyIcon1.Text = "网络监视器" & Chr(13) & "网络状态:网络未连接"
End If
NotifyIcon1.ContextMenuStrip = ContextMenuStrip1 '为托盘图标添加右键菜单
End Sub
Private Sub Timer1_Tick(sender As System.Object,e As System.EventArgs) Handles Timer1.Tick 'Timer1空间的Tick间隔为20000毫秒
Dim result2 As String
a = My.Computer.Network.Ping("8.8.8.8")
If a = True Then
Label1.Text = "网络状态:网络已连接"
Light1.FillColor = Color.Green
Light1.FillGradientColor = Color.LightGreen
NotifyIcon1.Text = "网络监视器" & Chr(13) & "网络状态:网络已连接" '设置托盘图标的显示内容
Else
Label1.Text = "网络状态:网络未连接" '如果网络未连接,则显示
Light1.FillColor = Color.Red '同时更改“指示灯”颜色为红色
Light1.FillGradientColor = Color.Pink
NotifyIcon1.Text = "网络监视器" & Chr(13) & "网络状态:网络未连接"
If Label2.Text = "网络断开自动关机功能:已开启" Then '如果自动关机功能已开启
Dim systemD As String
systemD = System.Environment.SystemDirectory
Process.Start(systemD & "\shutdown.exe","-s -t 60") '则执行关机命令,延迟60秒
Timer1.Enabled = False '令Timer1不可用,以阻止多次执行同一动作
result2 = MsgBox("网络已断开,计算机将在60秒内关闭",MsgBoxStyle.OkCancel) '弹出对话框提示计算机即将关闭
If result2 = vbCancel Then
Process.Start(systemD & "\shutdown.exe","-a") '如果用户在关机前点击了对话框的“取消”按钮,则取消自动关机
Label2.Text = "网络断开自动关机功能:未开启"
End If
End If
End If
End Sub
Private Sub Button1_Click(sender As System.Object,e As System.EventArgs) Handles Button1.Click
Dim result1 As String
If Label2.Text = "网络断开自动关机功能:未开启" Then
Label2.Text = "网络断开自动关机功能:已开启"
result1 = MsgBox("自动关机功能已开启,是否让程序后台运行?",vbYesNoCancel)
Select Case result1
Case vbYes
Me.WindowState = FormWindowState.Minimized '如果用户选“是”,则隐藏程序
Case vbCancel
Label2.Text = "网络断开自动关机功能:未开启" '如果用户选“取消”,则取消设定
End Select
Else
Label2.Text = "网络断开自动关机功能:未开启"
End If
End Sub
Private Sub NotifyIcon1_MouseDoubleClick(sender As System.Object,e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
Me.WindowState = FormWindowState.Normal '双击托盘图标,显示窗口
End Sub
Private Sub monitor1_Resize(sender As Object,e As System.EventArgs) Handles Me.Resize
If Me.WindowState = FormWindowState.Minimized Then
Me.ShowInTaskbar = False '当程序最小化,则不在任务栏显示图标
End If
End Sub
Private Sub ShownHide1_Click(sender As System.Object,e As System.EventArgs) Handles ShownHide1.Click
If ShownHide1.Text = "隐藏窗口" Then
Me.WindowState = FormWindowState.Minimized
ShownHide1.Text = "显示窗口"
Else
Me.WindowState = FormWindowState.Normal
ShownHide1.Text = "隐藏窗口"
End If
End Sub
Private Sub Exit1_Click(sender As System.Object,e As System.EventArgs) Handles Exit1.Click
End
End Sub
End Class
程序在Windows7 +Visual Studio 2010 Express下编译通过