|
源代码网推荐
笔者最近在网上搜索了一些关于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://www.webjx.com ", CStr(0), CStr(0), 1) 源代码网推荐End Sub 源代码网推荐End Class
OK全部搞定,按F5键运行如图7所示,选择相应选项后点击(关闭计算机启动定时器按钮)就可以了,现在程序将按照你所设定的时间而启动关闭计算机选项。
 源代码网推荐图7
源代码网供稿. |