用VB.NET实现定时关机

前端之家收集整理的这篇文章主要介绍了用VB.NET实现定时关机前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

最近在网上搜索了一些关于编程实现定时关机、重启、注销的文章,发现大多介绍的是VB.NET2005用API实现这些功能

本文所使用的编程环境是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秒

双击窗体进入常规-声明Public Class Form1 事件中。以上介绍VB.NET实现定时关机

 
 
  1. Imports System.Runtime.InteropServices
  2. Imports Microsoft.VisualBasic
  3. Public Class Form1
  4. '调用系统参数
  5. Friend Shared Function GetCurrentProcess() As IntPtr
  6. End Function
  7. Friend Shared Function OpenProcessToken(ByVal h As IntPtr,
  8. ByVal acc As Integer,ByRef phtok As IntPtr) As Boolean
  9. End Function
  10. Friend Shared Function LookupPrivilegeValue(ByVal host As String,
  11. ByVal name As String,ByRef pluid As Long) As Boolean
  12. End Function
  13. Friend Shared Function AdjustTokenPrivileges(ByVal htok As IntPtr,
  14. ByVal disall As Boolean,ByRef newst As TokPriv1Luid,
  15. ByVal len As Integer,ByVal prev As IntPtr,
  16. ByVal relen As IntPtr) As Boolean
  17. End Function
  18. Friend Shared Function ExitWindowsEx(ByVal flg As Integer,
  19. ByVal rea As Integer) As Boolean
  20. End Function
  21. Friend Const SEPRIVILEGEENABLED As Integer = &H2
  22. Friend Const TOKENQUERY As Integer = &H8
  23. Friend Const TOKENADJUSTPRIVILEGES As Integer = &H20
  24. Friend Const SESHUTDOWNNAME As String = "SeShutdownPrivilege"
  25. Friend Const EWXlogoFF As Integer = &H0 '注销计算机
  26. Friend Const EWXSHUTDOWN As Integer = &H1'关闭计算机
  27. Friend Const EWXREBOOT As Integer = &H2'重新启动计算机
  28. Friend Const EWXFORCE As Integer = &H4'关闭所有进程,注销计算机
  29. Friend Const EWXPOWEROFF As Integer = &H8
  30. Friend Const EWXFORCEIFHUNG As Integer = &H10
  31. '引用参数
  32. Friend Structure TokPriv1Luid
  33. Public Count As Integer
  34. Public Luid As Long
  35. Public Attr As Integer
  36. End Structure
  37. Private Shared Sub DoExitWin(ByVal flg As Integer)
  38. Dim xc As Boolean '判断语句
  39. Dim tp As TokPriv1Luid
  40. Dim hproc As IntPtr = GetCurrentProcess()
  41. '调用进程值
  42. Dim htok As IntPtrIntPtr = IntPtr.Zero
  43. xc = OpenProcessToken(hproc,TOKENADJUSTPRIVILEGES Or TOKENQUERY,htok)
  44. tp.Count = 1
  45. tp.Luid = 0
  46. tp.Attr = SEPRIVILEGEENABLED
  47. xc = LookupPrivilegeValue(Nothing,SESHUTDOWNNAME,tp.Luid)
  48. xc = AdjustTokenPrivileges(htok,False,tp,IntPtr.Zero,IntPtr.Zero)
  49. xc = ExitWindowsEx(flg,0)
  50. End Sub
  51. Public Shared Sub Reboot()
  52. DoExitWin((EWXFORCE Or EWXREBOOT)) '重新启动计算机
  53. End Sub
  54. Public Shared Sub PowerOff()
  55. DoExitWin((EWXFORCE Or EWXPOWEROFF)) '关闭计算机
  56. End Sub
  57. Public Shared Sub logoOff()
  58. DoExitWin((EWXFORCE Or EWXlogoFF)) '注销计算机
  59. End Sub
  60. Dim entTime As Object '保存输入时间
  61. Dim xianzaiTime As Object '保存实时时间
  62. Dim startTime As Object '保存开始定时时间

你看使用以上方法就可以实现电脑定时关机,但是这种方法仅适用于会.net编程的用户,不会编程的用户,我还是建议您使用定时关机软件来给电脑定时关机。

定时关机 3000专业的电脑自动关机软件、定时关机软件、家长和电脑管理人员的好帮手、满足各种对电脑定时关机有需求的用户

以上这两种方法都可以实现电脑的定时关机,你如果要是下载定时关机3000的话,建议您到官方网站下载正式版。

原文链接:https://www.f2er.com/vb/259843.html

猜你在找的VB相关文章