笔者最近在网上搜索了一些关于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 |
图2 |
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 '保存输入时 |