下面请跟着图老师小编一起来了解下用VB.NET 2005编写定时关机程序,精心挑选的内容希望大家喜欢,不要忘记点个赞哦!
【 tulaoshi.com - 编程语言 】
笔者最近在网上搜索了一些关于VB.net实现关机、重启、注销的文章,发现大多介绍的是VB.NET2003用API实现这些功能,且在XPsp2环境下无法正常的关机与注销。而对于VB.NET2005的介绍几乎没有。本文章所涉及的知识点有:
以上界面工作基本完成现在需要输入代码了
双击窗体进入常规-声明Public Class Form1 事件中
CODE:
Imports System.Runtime.InteropServices
Imports Microsoft.VisualBasic
Public Class Form1
DllImport("kernel32.dll", ExactSpelling:=True) _'调用系统参数
Friend Shared Function GetCurrentProcess() As IntPtr
End Function
DllImport("advapi32.dll", ExactSpelling:=True, SetLastError:=True) _
Friend Shared Function OpenProcessToken(ByVal h As IntPtr, ByVal acc As Integer, ByRef phtok As IntPtr) As Boolean
End Function
DllImport("advapi32.dll", SetLastError:=True) _
Friend Shared Function LookupPrivilegeValue(ByVal host As String, ByVal name As String, ByRef pluid As Long) As Boolean
End Function
DllImport("advapi32.dll", ExactSpelling:=True, SetLastError:=True) _
Friend Shared Function AdjustTokenPrivileges(ByVal htok As IntPtr, ByVal disall As Boolean, ByRef newst As TokPriv1Luid, ByVal len As Integer, ByVal prev As IntPtr, ByVal relen As IntPtr) As Boolean
End Function
DllImport("user32.dll", ExactSpelling:=True, SetLastError:=True) _
Friend Shared Function ExitWindowsEx(ByVal flg As Integer, ByVal rea As Integer) As Boolean
End Function
Friend Const SE_PRIVILEGE_ENABLED As Integer = &H2
Friend Const TOKEN_QUERY As Integer = &H8
Friend Const TOKEN_ADJUST_PRIVILEGES As Integer = &H20
Friend Const SE_SHUTDOWN_NAME As String = "SeShutdownPrivilege"
Friend Const EWX_LogoFF As Integer = &H0 '注销计算机
Friend Const EWX_SHUTDOWN As Integer = &H1'关闭计算机
Friend Const EWX_REBOOT As Integer = &H2'重新启动计算机
Friend Const EWX_FORCE As Integer = &H4'关闭所有进程,注销计算机
Friend Const EWX_POWEROFF As Integer = &H8
Friend Const EWX_FORCEIFHUNG As Integer = &H10
StructLayout(LayoutKind.Sequential, Pack:=1) _
'引用参数
Friend Structure TokPriv1Luid
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 TokPriv1Luid
Dim hproc As IntPtr = GetCurrentProcess()
'调用进程值
Dim htok As IntPtr = IntPtr.Zero
xc = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, htok)
tp.Count = 1
tp.Luid = 0
tp.Attr = SE_PRIVILEGE_ENABLED
xc = LookupPrivilegeValue(Nothing, SE_SHUTDOWN_NAME, tp.Luid)
xc = AdjustTokenPrivileges(htok, False, tp, 0, IntPtr.Zero, IntPtr.Zero)
xc = ExitWindowsEx(flg, 0)
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 '保存开始定时时间
双击注销button2按钮输入code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
LogoOff()'注销计算机
End Sub
双击重新启动按钮button3,输入code:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Reboot()
End Sub
双击关闭计算机按钮button1,输入code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
startTime = TimeOfDay
If Not IsDate(TextBox1.Text) Then
'用IsData函数判断输入的时间格式
MsgBox("你所输入的不是时间格式,!", , "错误")
Else
entTime = TimeValue(TextBox1.Text)
End If
Timer1.Enabled = True
'启动定时器
Me.WindowState = System.Windows.Forms.FormWindowState.Minimized
'最小化窗体
End Sub
如图3
双击timer1控件如图4
输入代码:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
xianzaiTime = TimeOfDay
If RadioButton1.Checked Then
If DateDiff(Microsoft.VisualBasic.DateInterval.Second, xianzaiTime, entTime) 0 Then '用DateDiff函数判断是否到时间了
End If
End If
PowerOff() '关闭计算机
End Sub
好了基本上一个定时关机程序就完成了,接下来加一个超级链接吧!当然对于高手来说可是废话,但是对于新手来说这也是必备的。这个超级链接当然是我们最喜欢的天极网开发频道了。
先创建一个Label控件吧,把它托到窗体上,将text属性设置为天极网开发频道如图5
接下来需要输入代码了,双击窗体Form1
进入Public Class Form1事件
代码:
Private Declare Function ShellExecute Lib "shell32.dll" 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
如图6
双击刚才添加的label属性text:(天极网开发频道)中输入以下代码:
Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click
ShellExecute(0, "open", "http://dev.yesky.com ", CStr(0), CStr(0), 1)
End Sub
End Class
OK全部搞定,按F5键运行如图7所示,选择相应选项后点击(关闭计算机启动定时器按钮)就可以了,现在程序将按照你所设定的时间而启动关闭计算机选项。
图7
来源:http://www.tulaoshi.com/n/20160219/1627058.html
看过《用VB.NET 2005编写定时关机程序》的人还看了以下文章 更多>>