淺析VB.NET實現定時關機
在向大家詳細介紹VB.NET實現定時關機之前,首先讓大家了解下VB.NET2005,然后全面介紹VB.NET實現定時關機具體步驟。
筆者最近在網上搜索了一些關于VB.NET實現定時關機、重啟、注銷的文章,發(fā)現大多介紹的是VB.NET2003用API實現這些功能,且在XPsp2環(huán)境下無法正常的關機與注銷。而對于VB.NET2005的介紹幾乎沒有。
本篇文章具有一定的基礎性和廣泛的實用性,相信能夠給VB.NET2005初學者帶來一定的幫助。
本文所使用的編程環(huán)境是Microsoft Visual Studio 2005,首先打開 Visual Studio。在文件 (File) 菜單上,單擊新建項目 (New Project)。 在新建項目 (New Project) 對話框的模板 (Templates) 窗格中,單擊 Windows 應用程序 (Windows Application)。單擊確定 (OK)。
具體步驟如下:
首先在Form1窗體上添加一個Label1控件屬性text設置為:今天:2.然后分別添加3個button控件name分別為button1、button2、button3它們的text屬性分別為1. 關閉計算機(啟動定時器)2. 注銷3. 重新啟動。
現在我們就需要為程序加上一個定時器了,這個定時器需要與textbox1控件相關聯,輸入正確時間格式后就可以啟動定時功能了。然后我們需要在窗體上添加一個timer、一個textbox1控件、和一個RadioButton1控件。讓它們保留默認值不變。其中. TextBox1控件的text屬性設置為:00:00:00 。RadioButton1控件text設置為:指定時間關機|時間格式:00小時:00分鐘:00秒
雙擊窗體進入常規(guī)-聲明Public Class Form1 事件中。以上介紹VB.NET實現定時關機
- Imports System.Runtime.InteropServices
- Imports Microsoft.VisualBasic
- Public Class Form1
- '調用系統(tǒng)參數
- 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 TokPriv1Luid,
- 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 SEPRIVILEGEENABLED As Integer = &H2
- Friend Const TOKENQUERY As Integer = &H8
- Friend Const TOKENADJUSTPRIVILEGES As Integer = &H20
- Friend Const SESHUTDOWNNAME As String = "SeShutdownPrivilege"
- Friend Const EWXLOGOFF As Integer = &H0 '注銷計算機
- Friend Const EWXSHUTDOWN As Integer = &H1'關閉計算機
- Friend Const EWXREBOOT As Integer = &H2'重新啟動計算機
- Friend Const EWXFORCE As Integer = &H4'關閉所有進程,注銷計算機
- Friend Const EWXPOWEROFF As Integer = &H8
- Friend Const EWXFORCEIFHUNG As Integer = &H10
- '引用參數
- 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 IntPtrIntPtr = IntPtr.Zero
- xc = OpenProcessToken(hproc, TOKENADJUSTPRIVILEGES Or TOKENQUERY, htok)
- tp.Count = 1
- tp.Luid = 0
- tp.Attr = SEPRIVILEGEENABLED
- xc = LookupPrivilegeValue(Nothing, SESHUTDOWNNAME, tp.Luid)
- xc = AdjustTokenPrivileges(htok, False, tp, 0, IntPtr.Zero, IntPtr.Zero)
- xc = ExitWindowsEx(flg, 0)
- End Sub
- Public Shared Sub Reboot()
- DoExitWin((EWXFORCE Or EWXREBOOT)) '重新啟動計算機
- End Sub
- Public Shared Sub PowerOff()
- DoExitWin((EWXFORCE Or EWXPOWEROFF)) '關閉計算機
- End Sub
- Public Shared Sub LogoOff()
- DoExitWin((EWXFORCE Or EWXLOGOFF)) '注銷計算機
- End Sub
- Dim entTime As Object '保存輸入時間
- Dim xianzaiTime As Object '保存實時時間
- Dim startTime As Object '保存開始定時時間
【編輯推薦】