重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
系统托盘里显示图标的话用NotifyIcon组件,建立一个ContextMenuStrip控件与之关联。关联的位置在NotifyIcon.ContextMenuStrip。关机直接shell 命令列:rundll32.exe user.exe,exitwindows就可以,声音提示用My.Compurer.Audio.Play ("路径"),提示消息用Msgbox函数……通过判断返回值在确定是否关机……手头没有VB.Net……大概思路就是这样,不知道能不能对你有帮助……如果要代码的话,不忙的时候可以给你写
创新互联服务项目包括玉环网站建设、玉环网站制作、玉环网页制作以及玉环网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,玉环网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到玉环省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
最近在网上搜索了一些关于实现关机 重启 注销的文章 发现大多介绍的是VB NET 用API实现这些功能 且在XPsp 环境下无法正常的关机与注销 而对于VB NET 的介绍几乎没有 本文章所涉及的知识点有
用实现关机 重启 注销功能 通过使用textbox与timer控件的结合编写定时器功能 为你的程序加上超链接
本篇文章具有一定的基础性和广泛的实用性 相信能够给 初学者带来一定的帮助
本文所使用的编程环境是Microsoft Visual Studio 首先打开 Visual Studio 在文件 (File) 菜单上 单击新建项目 (New Project) 在新建项目 (New Project) 对话框的模板 (Templates) 窗格中 单击 Windows 应用程序 (Windows Application) 单击确定 (OK)
具体步骤如下
首先在Form 窗体上添加一个Label 控件属性text设置为:今天: 然后分别添加 个button控件name分别为button button button 它们的text属性分别为 关闭计算机(启动定时器) 注销 重新启动
图
现在我们就需要为程序加上一个定时器了 这个定时器需要与textbox 控件相关联 输入正确时间格式后就可以启动定时功能了 然后我们需要在窗体上添加一个timer 一个textbox 控件 和一个RadioButton 控件 让它们保留默认值不变 其中 TextBox 控件的text属性设置为 : : RadioButton 控件text设置为 指定时间关机|时间格式 小时: 分钟: 秒如图 所示
图
以上界面工作基本完成现在需要输入代码了
双击窗体进入常规 声明Public Class Form 事件中
CODE
Imports System Runtime InteropServicesImports Microsoft VisualBasicPublic Class Form _ 调用系统参数Friend Shared Function GetCurrentProcess() As IntPtrEnd Function
_Friend Shared Function OpenProcessToken(ByVal h As IntPtr
ByVal acc As Integer ByRef phtok As IntPtr) As BooleanEnd Function
_Friend Shared Function LookupPrivilegeValue(ByVal host As String
ByVal name As String ByRef pluid As Long) As BooleanEnd Function
_Friend Shared Function AdjustTokenPrivileges(ByVal htok As IntPtr
ByVal disall As Boolean ByRef newst As TokPriv Luid
ByVal len As Integer ByVal prev As IntPtr
ByVal relen As IntPtr) As BooleanEnd Function
_Friend Shared Function ExitWindowsEx(ByVal flg As Integer
ByVal rea As Integer) As BooleanEnd 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 TokPriv Luid Public Count As Integer Public Luid As Long Public Attr As IntegerEnd Structure
Private Shared Sub DoExitWin(ByVal flg As Integer) Dim xc As Boolean 判断语句 Dim tp As TokPriv Luid Dim hproc As IntPtr = GetCurrentProcess() 调用进程值 Dim htok As IntPtr = IntPtr Zero xc = OpenProcessToken(hproc TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY htok) tp Count = tp Luid = tp Attr = SE_PRIVILEGE_ENABLED xc = LookupPrivilegeValue(Nothing SE_SHUTDOWN_NAME tp Luid) xc = AdjustTokenPrivileges(htok False tp IntPtr Zero IntPtr Zero) 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 System Object
ByVal e As System EventArgs) Handles Button ClickLogoOff() 注销计算机End Sub
双击重新启动按钮button 输入code
Private Sub Button _Click(ByVal sender As System Object
ByVal e As System EventArgs) Handles Button Click
Reboot()
End Sub
双击关闭计算机按钮button 输入code
Private Sub Button _Click(ByVal sender As System Object
ByVal e As System EventArgs) Handles Button Click startTime = TimeOfDay If Not IsDate(TextBox Text) Then 用IsData函数判断输入的时间格式MsgBox( 你所输入的不是时间格式 ! 错误 ) ElseentTime = TimeValue(TextBox Text) End If Timer Enabled = True 启动定时器 Me WindowState = System Windows Forms FormWindowState Minimized 最小化窗体
End Sub
如图
图
双击timer 控件如图
图
输入代码
Private Sub Timer _Tick(ByVal sender As System Object
ByVal e As System EventArgs) Handles Timer Tick xianzaiTime = TimeOfDay If RadioButton Checked Then
If DateDiff(Microsoft VisualBasic DateInterval Second
xianzaiTime entTime) Then 用DateDiff函数判断是否到时间了
End IfEnd If
PowerOff() 关闭计算机End Sub
好了 基本上一个定时关机程序就完成了 接下来加一个超级链接吧!当然对于高手来说可能是废话 但是对于新手来说这也是必备的 这个超级链接当然是我们最喜欢的天极网开发频道了
先创建一个Label控件吧 把它托到窗体上 将text属性设置为天极网开发频道如图
图
接下来需要输入代码了 双击窗体Form
进入Public Class Form 事件
代码
Private Declare Function ShellExecute Lib shell 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
如图
图
双击刚才添加的label属性text:(天极网开发频道)中输入以下代码
Private Sub Label _Click(ByVal sender As System Object
ByVal e As System EventArgs) Handles Label ClickShellExecute( open CStr( ) CStr( ) )
End SubEnd Class
OK!全部搞定 按F 键运行如图 所示 选择相应选项后点击(关闭计算机启动定时器按钮)就可以了 现在程序将按照你所设定的时间而启动关闭计算机选项
lishixinzhi/Article/program/net/201311/12366
在SystemEvents类中 可以 用户试图注销或关闭系统时发生。 (当用户试图注销或关闭系统时发生。当用户试图注销或关闭系统时发生。) 这个 事件处理函数中 可以找到如下方法
Private Shared WM_QUERYENDSESSION As Integer = H11
Private Shared systemShutdown As Boolean = False
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_QUERYENDSESSION Then
'MessageBox.Show("queryendsession: this is a logoff, shutdown, or reboot")
systemShutdown = True
End If
' If this is WM_QUERYENDSESSION, the closing event should be raised in the base WndProc.
MyBase.WndProc(m)
End Sub 'WndProc
Private Sub Form1_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If (systemShutdown) Then
' Reset the variable because the user might cancel the shutdown.
systemShutdown = False
If (System.Windows.Forms.DialogResult.Yes = _
MessageBox.Show("My application", "Do you want to save your work before logging off?", MessageBoxButtons.YesNo)) Then
e.Cancel = True
Else
e.Cancel = False
End If
End If
End Sub
软糖来回答罗:通过System.Diagnostics命名空间下的Process类来关闭程序的进程
Dim 进程集合 = Process.GetProcessesByName("进程名称")
For Each 进程 In 进程集合
进程.Kill()
'进程.Close() '或者使用关闭
Next
也可以先获取所有进程,再来判断这些进程的名称ProcessName
Dim 获取本地所有进程 = Process.GetProcesses()
For Each 进程 In 获取本地所有进程
If 进程.ProcessName = "explorer.exe" Then 进程.Kill()
Next
这是点击Option 你可以
用个msgbox函数 点击YES时候运行关机代码即可
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Integer, ByVal dwReserved As Integer) As Integer
Const EWX_FORCE As Short = 4
Const EWX_LOGOFF As Short = 0
Const EWX_REBOOT As Short = 2
Const EWX_SHUTDOWN As Short = 1
Dim retval As Integer
' 定义Esc按键
Const VK_ESCAPE As Short = H1Bs
Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
If Option1.Checked Then
' 注销当前用户
retval = ExitWindowsEx(EWX_FORCE, 0) bitsCN.Com
ElseIf Option2.Checked Then
' 关闭计算机
retval = ExitWindowsEx(EWX_SHUTDOWN, 0)
ElseIf Option3.Checked Then
' 重新启动
retval = ExitWindowsEx(EWX_REBOOT, 0)
End If
End Sub
Private Sub Command2_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command2.Click
Me.Close()
End Sub
' 按Esc键时,结束应用程序
Private Sub Form1_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
If KeyAscii = VK_ESCAPE Then BBS.bitsCN.com网管论坛
Me.Close()
End If
If KeyAscii = 0 Then
eventArgs.Handled = True
End If
End Sub