LBSALE[1]LBSALE[watermark]http://www.thysea.com/lb/cgi-bin/topic.cgi?forum=127&topic=1869&show=0
在上述贴子中提到的问题,那种方法虽然可以解决但总是觉得别扭!通过几天的努力,如今我用VB.net可以更方便的控制。以托盘的形式显示,实现代码非常简单,本次源码予于公布:
Public Class Form1
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If My.Computer.Network.IsAvailable = False Then
MessageBox.Show(vbCrLf & "您的计算机没有接入Internet,或者没有活动网卡,程序终止!", "→无条件为你⊙程序设计", MessageBoxButtons.OK, MessageBoxIcon.Error)
End
End If
If My.Computer.Network.Ping("192.168.0.1", 1000) = False Then
Dim m = CreateObject("WScript.Shell")
m.Run("shutdown.exe -s -t 60")
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MessageBox.Show(vbCrLf & "程序已经执行,将以托盘方式显示在任务栏的通知区域!", "→无条件为你⊙程序设计", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Try
If Me.Visible Then Me.Hide()
Me.KeyPreview = True ';注册键盘事件
Me.ShowInTaskbar = False ';在任务栏处不显示窗体
Catch ex As Exception
MessageBox.Show(ex.Message, "出错啦!原因:", MessageBoxButtons.OK, MessageBoxIcon.Error)
End
End Try
End Sub
Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
MessageBox.Show(vbCrLf & "恭喜!程序退出成功,欢迎您的使用!", "→无条件⊙制作!", MessageBoxButtons.OK, MessageBoxIcon.Information)
Me.Close()
End Sub
End Class
程序自动生成窗体代码如下,请用2005版本的VB.net编译:
_
Partial Class Form1
Inherits System.Windows.Forms.Form
';Form 重写 Dispose,以清理组件列表。
_
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
';Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
';注意: 以下过程是 Windows 窗体设计器所必需的
';可以使用 Windows 窗体设计器修改它。
';不要使用代码编辑器修改它。
_
Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
Me.NotifyIcon1 = New System.Windows.Forms.NotifyIcon(Me.components)
Me.SuspendLayout()
';
';Timer1
';
Me.Timer1.Enabled = True
Me.Timer1.Interval = 1000
';
';NotifyIcon1
';
Me.NotifyIcon1.Icon = CType(resources.GetObject("NotifyIcon1.Icon"), System.Drawing.Icon)
Me.NotifyIcon1.Text = "双击,退出程序…"
Me.NotifyIcon1.Visible = True
';
';Form1
';
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(310, 200)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
Friend WithEvents Timer1 As System.Windows.Forms.Timer
Friend WithEvents NotifyIcon1 As System.Windows.Forms.NotifyIcon
End Class
作者: wang832023 时间: 2006-5-24 00:08 标题: [原创]用VB.net编写掉线关机程序