熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

VB.NET 2005編寫定時關機程序

2013-11-13 10:02:36  來源: .NET編程 

  最近在網上搜索了一些關於實現關機重啟注銷的文章發現大多介紹的是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鍵運行如圖所示選擇相應選項後點擊(關閉計算機啟動定時器按鈕)就可以了現在程序將按照你所設定的時間而啟動關閉計算機選項

  


From:http://tw.wingwit.com/Article/program/net/201311/12366.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.