程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> Visual Basic語言 >> VB綜合教程 >> 用VB.NET 2005編寫定時關機程序

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

編輯:VB綜合教程
  筆者最近在網上搜索了一些關於VB.net實現關機、重啟、注銷的文章,發現大多介紹的是VB.NET2003用API實現這些功能,且在XPsp2環境下無法正常的關機與注銷。而對於VB.NET2005的介紹幾乎沒有。本文章所涉及的知識點有:
  
  1.用vb.net實現關機、重啟、注銷功能
  
  2.通過使用textbox與timer控件的結合編寫定時器功能。
  
  3.為你的程序加上超鏈接。
  
  本篇文章具有一定的基礎性和廣泛的實用性,相信能夠給VB.net2005初學者帶來一定的幫助。
  
  本文所使用的編程環境是Microsoft Visual Studio 2005,首先打開 Visual Studio。在文件 (File) 菜單上,單擊新建項目 (New Project)。 在新建項目 (New Project) 對話框的模板 (Templates) 窗格中,單擊 Windows 應用程序 (Windows Application)。單擊確定 (OK)。
  
  1.首先在Form1窗體上添加一個Label1控件屬性text設置為:今天:2.然後分別添加3個button控件name分別為button1、button2、button3它們的text屬性分別為1. 關閉計算機(啟動定時器)2. 注銷3. 重新啟動。(圖1)
  
  
  圖1
  現在我們就需要為程序加上一個定時器了,這個定時器需要與textbox1控件相關聯,輸入正確時間格式後就可以啟動定時功能了。然後我們需要在窗體上添加一個timer、一個textbox1控件、和一個RadioButton1控件。讓它們保留默認值不變。其中. TextBox1控件的text屬性設置為:00:00:00 。RadioButton1控件text設置為:指定時間關機|時間格式:00小時:00分鐘:00秒如圖2所示
  
  
  圖2

  
  以上界面工作基本完成現在需要輸入代碼了

  雙擊窗體進入常規-聲明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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved