最近在网上搜索了一些关于实现关机重启注销的文章发现大多介绍的是VBNET用API实现这些功能且在XPsp环境下无法正常的关机与注销而对于VBNET的介绍几乎没有本文章所涉及的知识点有
用实现关机重启注销功能
通过使用textbox与timer控件的结合编写定时器功能
为你的程序加上超链接
本篇文章具有一定的基础性和广泛的实用性相信能够给初学者带来一定的帮助
本文所使用的编程环境是Microsoft Visual Studio 首先打开 Visual Studio在文件 (File) 菜单上单击新建项目 (New Project) 在新建项目 (New Project) 对话框的模板 (Templates) 窗格中单击 Windows 应用程序 (Windows Application)单击确定 (OK)
具体步骤如下
首先在Form窗体上添加一个Label控件属性text设置为:今天:然后分别添加个button控件name分别为buttonbuttonbutton它们的text属性分别为 关闭计算机(启动定时器) 注销 重新启动
图
现在我们就需要为程序加上一个定时器了这个定时器需要与textbox控件相关联输入正确时间格式后就可以启动定时功能了然后我们需要在窗体上添加一个timer一个textbox控件和一个RadioButton控件让它们保留默认值不变其中 TextBox控件的text属性设置为:: RadioButton控件text设置为指定时间关机|时间格式小时:分钟:秒如图所示
图
以上界面工作基本完成现在需要输入代码了
双击窗体进入常规声明Public Class Form 事件中
CODE
Imports SystemRuntimeInteropServices
Imports MicrosoftVisualBasic
Public Class Form
_调用系统参数Friend Shared Function GetCurrentProcess() As IntPtr
End Function
_Friend Shared Function OpenProcessToken(ByVal h As IntPtr
ByVal acc As Integer ByRef phtok As IntPtr) As Boolean
End Function
_Friend Shared Function LookupPrivilegeValue(ByVal host As String
ByVal name As String ByRef pluid As Long) As Boolean
End Function
_Friend Shared Function AdjustTokenPrivileges(ByVal htok As IntPtr
ByVal disall As Boolean ByRef newst As TokPrivLuid
ByVal len As Integer ByVal prev As IntPtr
ByVal relen As IntPtr) As Boolean
End Function
_Friend Shared Function ExitWindowsEx(ByVal flg As Integer
ByVal rea As Integer) As Boolean
End Function
Friend Const SE_PRIVILEGE_ENABLED As Integer = &H
Friend Const TOKEN_QUERY As Integer = &H
Friend Const TOKEN_ADJUST_PRIVILEGES As Integer = &H
Friend Const SE_SHUTDOWN_NAME As String = SeShutdownPrivilege
Friend Const EWX_LOGOFF As Integer = &H 注销计算机
Friend Const EWX_SHUTDOWN As Integer = &H关闭计算机
Friend Const EWX_REBOOT As Integer = &H重新启动计算机
Friend Const EWX_FORCE As Integer = &H关闭所有进程注销计算机
Friend Const EWX_POWEROFF As Integer = &H
Friend Const EWX_FORCEIFHUNG As Integer = &H
_ 引用参数
Friend Structure TokPrivLuid
Public Count As Integer
Public Luid As Long
Public Attr As Integer
End Structure
Private Shared Sub DoExitWin(ByVal flg As Integer)
Dim xc As Boolean 判断语句
Dim tp As TokPrivLuid
Dim hproc As IntPtr = GetCurrentProcess()
调用进程值
Dim htok As IntPtr = IntPtrZero
xc = OpenProcessToken(hproc TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY htok)
tpCount =
tpLuid =
tpAttr = SE_PRIVILEGE_ENABLED
xc = LookupPrivilegeValue(Nothing SE_SHUTDOWN_NAME tpLuid)
xc = AdjustTokenPrivileges(htok False tp IntPtrZero IntPtrZero)
xc = ExitWindowsEx(flg )
End Sub
Public Shared Sub Reboot()
DoExitWin((EWX_FORCE Or EWX_REBOOT)) 重新启动计算机
End Sub
Public Shared Sub PowerOff()
DoExitWin((EWX_FORCE Or EWX_POWEROFF)) 关闭计算机
End Sub
Public Shared Sub LogoOff()
DoExitWin((EWX_FORCE Or EWX_LOGOFF)) 注销计算机
End Sub
Dim entTime As Object 保存输入时间
Dim xianzaiTime As Object 保存实时时间
Dim startTime As Object 保存开始定时时间
注销button按钮输入code
Private Sub Button_Click(ByVal sender As SystemObject
ByVal e As SystemEventArgs) Handles ButtonClick
LogoOff()注销计算机
End Sub
双击重新启动按钮button输入code
Private Sub Button_Click(ByVal sender As SystemObject
ByVal e As SystemEventArgs) Handles ButtonClick
Reboot()
End Sub
双击关闭计算机按钮button输入code
Private Sub Button_Click(ByVal sender As SystemObject
ByVal e As SystemEventArgs) Handles ButtonClick
startTime = TimeOfDay
If Not IsDate(TextBoxText) Then
用IsData函数判断输入的时间格式
MsgBox(你所输入的不是时间格式! 错误)
Else
entTime = TimeValue(TextBoxText)
End If
TimerEnabled = True
启动定时器
MeWindowState = SystemWindowsFormsFormWindowStateMinimized
最小化窗体
End Sub
如图
图
双击timer控件如图
图
输入代码
Private Sub Timer_Tick(ByVal sender As SystemObject
ByVal e As SystemEventArgs) Handles TimerTick
xianzaiTime = TimeOfDay
If RadioButtonChecked Then
If DateDiff(MicrosoftVisualBasicDateIntervalSecond
xianzaiTime entTime) < Then 用DateDiff函数判断是否到时间了
End If
End If
PowerOff() 关闭计算机
End Sub
好了基本上一个定时关机程序就完成了接下来加一个超级链接吧!当然对于高手来说可能是废话但是对于新手来说这也是必备的这个超级链接当然是我们最喜欢的天极网开发频道了
先创建一个Label控件吧把它托到窗体上将text属性设置为天极网开发频道如图
图
接下来需要输入代码了双击窗体Form
进入Public Class Form事件
代码
Private Declare Function ShellExecute Lib shelldll
Alias ShellExecuteA(ByVal hwngnd As Integer
ByVal lpOperation As String ByVal lpFile As String
ByVal lpParameters As String ByVal lpDirectory As String
ByVal nShowCmd As Integer) As Integer
如图
图
双击刚才添加的label属性text:(天极网开发频道)中输入以下代码
Private Sub Label_Click(ByVal sender As SystemObject
ByVal e As SystemEventArgs) Handles LabelClick
ShellExecute( open CStr() CStr() )
End Sub
End Class
OK!全部搞定按F键运行如图所示选择相应选项后点击(关闭计算机启动定时器按钮)就可以了现在程序将按照你所设定的时间而启动关闭计算机选项
图