VB开机、关机、重启代码
vb关机程序代码
CODE:Imports System.Runtime.InteropServicesImports Microsoft.VisualBasicPublic Class Form1<DllImport("kernel32.dll", ExactSpelling:=True)> _'调用系统参数Friend Shared Function GetCurrentProcess() As IntPtrEnd 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 BooleanEnd Function<DllImport("advapi32.dll", SetLastError:=True)> _Friend Shared Function LookupPrivilegeValue(ByVal host As String, ByVal name As String, ByRef pluid As Long) As BooleanEnd 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 BooleanEnd Function<DllImport("user32.dll", ExactSpelling:=True, SetLastError:=True)> _Friend Shared Function ExitWindowsEx(ByVal flg As Integer, ByVal rea As Integer) As BooleanEnd FunctionFriend Const SE_PRIVILEGE_ENABLED As Integer = &H2Friend Const TOKEN_QUERY As Integer = &H8Friend Const TOKEN_ADJUST_PRIVILEGES As Integer = &H20Friend 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 = &H8Friend Const EWX_FORCEIFHUNG As Integer = &H10<StructLayout(LayoutKind.Sequential, Pack:=1)> _'引用参数Friend Structure TokPriv1LuidPublic Count As IntegerPublic Luid As LongPublic Attr As IntegerEnd StructurePrivate Shared Sub DoExitWin(ByVal flg As Integer)Dim xc As Boolean '判断语句Dim tp As TokPriv1LuidDim hproc As IntPtr = GetCurrentProcess()'调用进程值Dim htok As IntPtr = IntPtr.Zeroxc = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, htok)tp.Count = 1tp.Luid = 0tp.Attr = SE_PRIVILEGE_ENABLEDxc = LookupPrivilegeValue(Nothing, SE_SHUTDOWN_NAME, tp.Luid) xc = AdjustTokenPrivileges(htok, False, tp, 0, IntPtr.Zero, IntPtr.Zero) xc = ExitWindowsEx(flg, 0)End SubPublic Shared Sub Reboot()DoExitWin((EWX_FORCE Or EWX_REBOOT)) '重新启动计算机End SubPublic Shared Sub PowerOff()DoExitWin((EWX_FORCE Or EWX_POWEROFF)) '关闭计算机End SubPublic Shared Sub LogoOff()DoExitWin((EWX_FORCE Or EWX_LOGOFF)) '注销计算机End SubDim 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.ClickLogoOff()'注销计算机End Sub双击重新启动按钮button3,输入code:Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.ClickReboot()End Sub双击关闭计算机按钮button1,输入code:Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickstartTime = TimeOfDayIf Not IsDate(TextBox1.Text) Then'用IsData函数判断输入的时间格式MsgBox("你所输入的不是时间格式,!", , "错误")ElseentTime = TimeValue(TextBox1.Text)End IfTimer1.Enabled = True'启动定时器Me.WindowState = System.Windows.Forms.FormWindowState.Minimized '最小化窗体End Sub如图3图3双击timer1控件如图4图4输入代码:Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.TickxianzaiTime = TimeOfDayIf RadioButton1.Checked ThenIf DateDiff(Microsoft.VisualBasic.DateInterval.Second, xianzaiTime, entTime) < 0 Then '用DateDiff函数判断是否到时间了End IfEnd IfPowerOff() '关闭计算机End Sub好了基本上一个定时关机程序就完成了,接下来加一个超级链接吧!当然对于高手来说可是废话,但是对于新手来说这也是必备的。
Vb 定时关机源码
Vb 定时关机看到网上有很多关于定时关机的代码或例子,对于初学者来说,面对许多的声名或变量,却是两眼一抹黑,什么也看不懂,其实只调用系统自带的关机命令就可以实现Set g = CreateObject("WScript.Shell")g.Run "shutdown /f /s /t 0 "在这里跟大家分享,高手请路过第一步:打开vb 编程器,新建一个标准EXE工程然后在窗体上加入一个时间控件,两个按钮,两个输入框,还有一个标签下面设置控件的属性,为了方便,我将控件名称和显示都改成了中文时间控件:点击窗体上的时间控件,在右侧的属性栏中设置。
Vb支持中文名称将两个按钮分别设为退出和启动将两个输入框名称分别设为小时和分钟,这是用来输入要定时的时间的输入框的文本属性都要设为空:如图:还有个标签属性,名称设为当前时间,显示设为空下面开始骗写代码了:为了简单易懂,变量中没有加入变量声明,懂的自已写上即可先骗写两个按钮的代码,双击退出按钮,就会弹出代码编写框,将两个按钮代码写上,代码如下:然后回到窗体,双击定时器控件开始编下如下代码:Private Sub 定时器_Timer()NowTime = Time '获取系统当前的时间DinTime = 小时.Text & ":" & 分钟.Text '获取输入框中的时间,'因为时间格式是hh:mm ,中间有时间分隔符:' 所以要用连接符& 将冒号加入形成时间格式If Hour(NowTime) = Hour(DinTime) And Minute(NowTime) = Minute(DinTime) Then Set g = CreateObject("WScript.Shell") '调用系统关机命令g.Run "shutdown /f /s /t 0 " '执行关机End If当前时间.Caption = NowTime ‘标签用来显示当前时间,提示程序正在运行状态End Sub如下图:这是为了方便大家看而将系统时间赋值给一个变量,也可以写成这样:Private Sub 定时器_Timer()DinTime = 小时.Text & ":" & 分钟.TextIf Hour(Time) = Hour(DinTime) And Minute(Time) = Minute(DinTime) ThenSet g = CreateObject("WScript.Shell")g.Run "shutdown /f /s /t 0 "End If当前时间.Caption = TimeEnd SubShutdown 后面参数/f /s /t 0 之前一定要有空格,否则不能执行当然输入框也可以用一个来获得定时的时间,只是中间要有冒号,如:12:20到此,一个最简易的定时器就完成了,运行,然后在输入框中设定好时间,点击启动就开始工作了,从当前时间标签上就可以看到定时器正在运行:当然,一个真正的程序这样还是不行的,如果当输入框中没有任何输入或是为空的时候,运行时就会出错,如果输入字母或是字符也会出错,还有当小时输入框的输入超过24小时,或是分钟输入超过60分钟的时候,时间判断永远都无法成立,就无法执行关机所以要在代码中加入逻辑判断:Private Sub 定时器_Timer()DinTime = 小时.Text & ":" & 分钟.TextIf 小时.Text = "" Or 分钟.Text = "" Or 小时.Text > 23 Or 分钟.Text > 59 Then MsgBox "时间设置错误!不能为空或是超出正常时间范围!" ‘提示错误定时器.Enabled = False ‘判断错误成立后停止定时器ElseIf Hour(Time) = Hour(DinTime) And Minute(Time) = Minute(DinTime) ThenSet g = CreateObject("WScript.Shell")g.Run "shutdown /f /s /t 0 "End IfEnd If当前时间.Caption = TimeEnd Sub如下图:一个完整的vb 定时器就完成了,生成EXE就可以使用了,只是界面有点粗糙,可以多增加一些控件或是图片进去,使界面更好看,下面是本人制作的:代码就懒得写出来了,大神们请勿喷,每个人都是从菜鸟走过来的,对于一个初学者来说,做出一个属于自己的软件,是令人非常高兴的作者:极品宁采晨。
VB实现关机和重新启动
VB实现关机和重新启动今天,我们将制作一个能重新启动和关闭计算机的应用程序。
程序运行后,弹出如图所示的界面,用户选择相应的选项后即可执行关机和重新启动操作。
并将此次的选择保存下来。
运行结果技术要点●关闭和重新启动计算机●窗体的KeyPreview 属性实现过程■新建项目打开Visual ,选择“新建项目”,在项目类型窗口中选择“Visual Basic项目”,在模板窗口中选择“Windows应用程序”,在名称域中输入“ControlComputer”,然后选择保存路径。
单击“确认”。
■添加控件向当前窗体添加一个Label控件、一个PictureBox控件、两个Button按钮和三个RadioButton控件。
■设置属性切换到属性栏,对窗体及控件设置属性,详细情况如表1所示。
注意:如果KeyPreview 属性被设置为True,窗体将先于该窗体上的控件接收此事件。
可用 KeyPreview 属性来创建全局键盘处理例程。
表1 窗体及控件的属性值■添加代码Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Integer, ByValdwReserved As Integer) As IntegerConst EWX_FORCE As Short = 4Const EWX_LOGOFF As Short = 0Const EWX_REBOOT As Short = 2Const EWX_SHUTDOWN As Short = 1Dim retval As Integer' 定义Esc按键Const VK_ESCAPE As Short = &H1BsPrivate Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs AsSystem.EventArgs) Handles Command1.ClickIf Option1.Checked Then' 注销当前用户retval = ExitWindowsEx(EWX_FORCE, 0)ElseIf Option2.Checked Then' 关闭计算机retval = ExitWindowsEx(EWX_SHUTDOWN, 0)ElseIf Option3.Checked Then' 重新启动retval = ExitWindowsEx(EWX_REBOOT, 0)End IfEnd SubPrivate Sub Command2_Click(ByVal eventSender As System.Object, ByVal eventArgs AsSystem.EventArgs) Handles Command2.ClickMe.Close()End Sub' 按Esc键时,结束应用程序Private Sub Form1_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs AsSystem.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPressDim KeyAscii As Short = Asc(eventArgs.KeyChar)If KeyAscii = VK_ESCAPE ThenMe.Close()End IfIf KeyAscii = 0 TheneventArgs.Handled = TrueEnd IfEnd Sub■运行程序单击菜单“调试|启动”或单击图标运行程序。
VB的开机自动运行程序的代码
VB的开机自动运行程序的代码有两种方法,1是注册表方式模块代码Option ExplicitPublic Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As LongPublic Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As LongPublic Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As LongPublic Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As LongPublic Const REG_SZ = 1Public Const HKEY_LOCAL_MACHINE = &H80000002'*************************************************************************'**函数名:SetAutoRun'**输入:ByVal Autorun(Boolean) -'**输出:无'**功能描述:随WINDOWS自动启动/取消启动模块'**全局变量:'**调用方法:Call SetAutoRun(True/False)'**作者:mythhack'**日期:2009-12-12 01:00:25'**修改人:'**日期:'**版本:V1.0.0'*************************************************************************Public Sub SetAutoRun(ByVal Autorun As Boolean)Dim KeyId As LongDim MyexePath As StringDim regkey As StringMyexePath = App.Path & "\" & App.EXEName & ".exe" '获取程序位置regkey = "Software\Microsoft\Windows\CurrentVersion\Run" '键值位置变量Call RegCreateKey(HKEY_LOCAL_MACHINE, regkey, KeyId) '建立If Autorun ThenRegSetValueEx KeyId, "MySoftware", 0&, REG_SZ, ByVal MyexePath, LenB(MyexePath) ElseRegDeleteValue KeyId, "MySoftware"End IfRegCloseKey KeyIdEnd Sub调用方法SetAutoRun(ByVal Autorun As Boolean)2是利用Vb5stkit.dll里面的函数窗体部分代码,加入6个按钮Option ExplicitPrivate Sub CmdAddStartup_Click() '在开始菜单的启动程序组下创建记事本的快捷方式Call OSfCreateShellLink("\启动", "记事本", GetWindowsPath & "\Notepad.exe", "")End SubPrivate Sub CmdAddDeskTop_Click() '在桌面创建记事本的快捷方式Call OSfCreateShellLink("..\..\桌面", "记事本", GetWindowsPath & "\Notepad.exe", "")End SubPrivate Sub CmdAddProgram_Click() '在程序菜单的Notepad程序组下创建记事本的快捷方式Call OSfCreateShellGroup("Notepad") '先建立程序组Call OSfCreateShellLink("Notepad", "记事本", GetWindowsPath & "\Notepad.exe", "")End SubPrivate Sub CmdAddStartMenu_Click()Dim i As LongFor i = 1 To 5 '在开始菜单创建记事本的快捷方式,必须用循环才能创建?Call OSfCreateShellLink("..\..\「开始」菜单", "记事本", GetWindowsPath & "\Notepad.exe", "")NextEnd SubPrivate Sub CmdQuickLaunch_Click() '在快捷工具栏下创建记事本的快捷方式Call OSfCreateShellLink("..\..\Application Data\Microsoft\Internet Explorer\Quick Launch", "记事本", GetWindowsPath & "\Notepad.exe", "")End SubPrivate Sub CmdDelAllLink_Click()Call OSfRemoveShellLink("..\..\「开始」菜单", "记事本") '删除开始菜单上的快捷方式Call OSfRemoveShellLink("..\..\桌面", "记事本") '删除桌面上的快捷方式'Call OSfRemoveShellLink("Notepad", "记事本") '删除Notepad程序组下的快捷方式,这样不能删除程序组Call RemoveShellGroup '删除Notepad程序组下的快捷方式Call OSfRemoveShellLink("\启动", "记事本") '删除启动菜单下的快捷方式Call OSfRemoveShellLink("..\..\Application Data\Microsoft\Internet Explorer\Quick Launch", "记事本") '删除快捷工具栏下的快捷方式End SubPrivate Sub RemoveShellGroup()On Error GoTo ToExit '打开错误陷阱'------------------------------------------------'RmDir删除一个存在的目录或文件夹。
VB 实现自动关机 源代码
工作内容:
协助总经理制定公司发展战略规划、经营计划、业务发展计划;
将公司内部管理制度化、规范化;
制定公司组织结构和管理体系、相关的管理、业务规范和制度;
组织、监督公司各项规划和计划的实施;
开展企业形象宣传活动;
能,下面我们就来看看如何实现Windows XP的自动关机。
LDC723E2160312241
Windows XP的关机是由Shutdown.exe程序来控制的,位于Windows\System32文件夹中。如
果想让Windows 2000也实现同样的效果,可以把Shutdown.exe复制到系统目录下。
六、根据总经理安排,主持召开经营例会或专项业务会议,对业务工作进行调查协调、指导。
七、就公司经营管理方针及长远规划、年度计划提出合理建议。
八、积极努力完成具体分工主管线上的目标任务。
九、完成总经理交办的其它工作任务
按时提交公司发展现状报告、发展计划报告;
指导公司人才队伍的建设工作;
协助总经理对公司运作与各职能部门进行管理,协助监督各项管理制度的制定及推行;
协助总经理推进公司企业文化的建设工作;
完成总经理临时下达的任务。
副总经理职责
在总经理领导下,协助总经理完成公司各项管理工作。
一、协助总经理制订公司发展规划、实施方案、管理制度。
二、协助总经理做好公司日常经营管理工作,执行公司各项决议,完成公司经营计划及总经理分配的各项具体工作。
三、检查各项管理制度的落实执行情况。
四、根据总经理授权,对日常业务经营活动进行检查、协调、督导。发现问题及时报告,并采取必要措施予以解决。
VB实现自动关机源代码
VB实现自动关机源代码让Windows 2000/XP自动关机 at 18:30 Shutdown -s如果你想出门,但电脑又正在进行工作,这时就要用到自动关机。
大多数实现自动关机的方法都是使用一些第三方软件,这样不仅麻烦,而且为实现这个小功能而专门动用一个软件,显的小题大做了!其实Windows XP(Windows 2000也可以)自身就具备定时关机的功能,下面我们就来看看如何实现Windows XP的自动关机。
LDC723E2160312241Windows XP的关机是由Shutdown.exe程序来控制的,位于Windows\System32文件夹中。
如果想让Windows 2000也实现同样的效果,可以把Shutdown.exe复制到系统目录下。
比如你的电脑要在22:00关机,可以选择“开始→运行”,输入“at 22:00 Shutdown -s”,这样,到了22点电脑就会出现“系统关机”对话框,默认有30秒钟的倒计时并提示你保存工作。
如果你想以倒计时的方式关机,可以输入“Shutdown.exe -s -t 3600”,这里表示60分钟后自动关机,“3600”代表60分钟。
设置好自动关机后,如果想取消的话,可以在运行中输入“shutdown -a”。
另外输入“shutdown -i”,则可以打开设置自动关机对话框,对自动关机进行设置。
Shutdown.exe的参数,每个都具有特定的用途,执行每一个都会产生不同的效果,比如“-s”就表示关闭本地计算机,“-a”表示取消关机操作,下面列出了更多参数,大家可以在Shutdown.exe中按需使用。
-f:强行关闭应用程序-m \\计算机名:控制远程计算机-i:显示图形用户界面,但必须是Shutdown的第一个选项-l:注销当前用户-r:关机并重启-t 时间:设置关机倒计时-c "消息内容":输入关机对话框中的消息内容(不能超127个字符)有时候,我们需要定时关闭计算机,下面介绍一个在Windows XP下实现定时关机的简单方法。
vb重启[精华]
VB重启Private Sub Form_Load()Shell "shutdown -r -f -t 0", vbHideEnd Sub意思就是运行一个XP外部命令Shutdown这个命令的常用参数有-r 重启-s 关机-l 注销-f 强制执行-t time 倒计时时间,time单位是秒重新启动计算机的实现在Windows下安装过程序的用户可能都有这样的体会,当安装完某些应用程序后,安装程序会要求重新启动计算机以使设置生效。
那么,如果要用VB程序重新启动计算机该如何实现呢?其实利用ExitWindowsEx函数即可。
程序的编写1.首先运行VB,然后新建一个工程(工程类型为标准EXE),然后双击Form1窗体,进入代码(Code)输入框,在其中输入:Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long)As Long2.点击“工具箱”中的commandbutton(命令按钮)工具,即可在窗体Form1中加入一个命令按钮,然后将该按钮属性中的Caption值定义为“重新启动”。
完成之后双击该按钮,进入代码(code)输入框输入以下代码:Private Sub Command1_Click()ExitWindowsEx &H43,0End Sub程序的执行程序输入完成后,点击VB“运行”菜单中的“启动”或“全面编译”命令,然后点击“重新启动”按钮,即可重新启动计算机。
二、磁盘格式化在Windows 9X中,当用鼠标右键单击“我的电脑”中的“3.5软盘A”然后在出现的右键菜单中选择“格式化”选项,就将弹出标准的“磁盘格式化”窗口。
下面我们来看看如何在应用程序中利用函数轻松调用Windows 95标准的“磁盘格式化”窗口,实现对磁盘相应的操作功能。
vb自动关机多功能源代码
'在窗体上随机画点
Me.PSet (Rnd * Me.Width, Rnd * Me.Height), RGB(Rnd * 225, Rnd * 225, Rnd * 225)
Me.DrawWidth = 2 '窗体输出的线条宽度为2
Private Sub Form_Load()
Set Label1 = Me.Controls.Add("bel", "Label1") '在线添加Label1控件
Set Timer1 = Me.Controls.Add("VB.Timer", "Timer1") '在线添加Timer1控件
form2
Dim display As String
Dim dispvalue As Integer
Dim blinkcounter As Integer
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
.ForeColor = &HFF& 'label前景色为黑色
'设置label的位置
.Top = Me.Height / 2 - Label1.Height / 2
.Left = Me.Width / 2 - Label1.Width / 2
End With
关机重启代码(整合篇)
用VB实现控制电脑自动关机,自动重启,自动注销,自动待机等代码下载设定状态SetSuspendState是否支持休眠IsPwrHibernateAllowed是否支持待机IsPwrSuspendAllowed看MSDN的Power Management部分ColdMooon'在窗体内'放置一个时间控件,两个标签控件和一个文体框控件,和两个单选按钮,两个命令按钮Option ExplicitDim txtTime '保存输入时间Dim nowTime '保存实时时间Dim oldTime '保存开始定时时间Private Sub Form_Load()me.caption="定时关机"bel1.caption="关机时间:"me.Option1.caption="在指定时间关机(时间格式:00:00:00)"me.Option2.caption="延迟指定的时间关机"mand1.caption="确定"mand2.caption="取消"Me.Text1 = GetSetting("ShutDownWindows", "Options", "ShutDownTime", "1:00")me!timer1.interval=1000End SubPrivate Sub Command1_Click()oldTime = TimeIf Not IsDate(Text1.Text) Then '用IsData函数判断输入的时间格式MsgBox "你所输入的不是时间格式,请修改!", , "Wrong"ElseIf Option1.Value ThentxtTime = TimeValue(Text1.Text)Timer1.Enabled = True '启动定时器Me.WindowState = 1 '最小化窗体Me.Caption = "定时关机-启动"ElseIf Option2.Value ThenMe.WindowState = 1Me.Caption = "定时关机-暂停"End IfEnd SubPrivate Sub Command2_Click()Unload MeEnd SubPrivate Sub Form_Unload(Cancel As Integer)SaveSetting "ShutDownWindows", "Options", "ShutDownTime", Me.Text1 End SubPrivate Sub Timer1_Timer()nowTime = Timebel2.Caption = TimeIf Option1.Value ThenIf DateDiff("s", nowTime, txtTime) <= 0 And DateDiff("s", nowTime, txtTime) >= -60 Then '用DateDiff函数判断是否到时间了ShutDownPC TrueTimer1.Enabled = FalseEnd IfEnd IfEnd Sub'在标准模块内Private Const EWX_LOGOFF = 0Public Const EWX_SHUTDOWN = 1Public Const EWX_POWEROFF = 8 '此参数在VB自带API浏览器并未提供,值得注意,如果没有此参数在Win2kServer会死在关机屏幕下Private Const EWX_REBOOT = 2Private Const EWX_FORCE = 4Private Const TOKEN_ADJUST_PRIVILEGES = &H20Private Const TOKEN_QUERY = &H8Private Const SE_PRIVILEGE_ENABLED = &H2Private Const ANYSIZE_ARRAY = 1Private Const VER_PLATFORM_WIN32_NT = 2Type OSVERSIONINFOdwOSVersionInfoSize As LongdwMajorVersion As LongdwMinorVersion As LongdwBuildNumber As LongdwPlatformId As LongszCSDVersion As String * 128End TypeType LUIDLowPart As LongHighPart As LongEnd TypeType LUID_AND_ATTRIBUTESpLuid As LUIDAttributes As LongEnd TypeType TOKEN_PRIVILEGESPrivilegeCount As LongPrivileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTESEnd TypePrivate Declare Function GetCurrentProcess Lib "kernel32" () As Long Private Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As LongPrivate Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As LongPrivate Declare Function AdjustTokenPrivileges Lib "advapi32" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState AsTOKEN_PRIVILEGES, ReturnLength As Long) As LongPublic Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As LongPrivate Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (ByRef lpVersionInformation As OSVERSIONINFO) As Long'Detect if the program is running under Windows NT 检测是否为NTPublic Function IsWinNT() As BooleanDim myOS As OSVERSIONINFOmyOS.dwOSVersionInfoSize = Len(myOS)GetVersionEx myOSIsWinNT = (myOS.dwPlatformId = VER_PLATFORM_WIN32_NT)End Function'set the shut down privilege for the current application 为当前应用程序设置关机权限Private Sub EnableShutDown()Dim hProc As LongDim hToken As LongDim mLUID As LUIDDim mPriv As TOKEN_PRIVILEGESDim mNewPriv As TOKEN_PRIVILEGEShProc = GetCurrentProcess()OpenProcessToken hProc, TOKEN_ADJUST_PRIVILEGES +TOKEN_QUERY, hTokenLookupPrivilegeValue "", "SeShutdownPrivilege", mLUIDmPriv.PrivilegeCount = 1mPriv.Privileges(0).Attributes = SE_PRIVILEGE_ENABLEDmPriv.Privileges(0).pLuid = mLUID' enable shutdown privilege for the current application 赋予当前应用程序关机权限AdjustTokenPrivileges hToken, False, mPriv, 4 + (12 * mPriv.PrivilegeCount), mNewPriv, 4 + (12 * mNewPriv.PrivilegeCount)End Sub' Shut Down PC 关闭PCPublic Sub ShutDownPC(Force As Boolean)Dim ret As LongDim Flags As LongFlags = EWX_SHUTDOWN Or EWX_POWEROFFIf Force Then Flags = Flags + EWX_FORCEIf IsWinNT Then EnableShutDownExitWindowsEx Flags, 0End Sub'Restart PC 重起PCPublic Sub RebootPC(Force As Boolean)Dim ret As LongDim Flags As LongFlags = EWX_REBOOTIf Force Then Flags = Flags + EWX_FORCEIf IsWinNT Then EnableShutDownExitWindowsEx Flags, 0End Sub'Log off the current user 注消(登出)当前用户Public Sub LogOff(Force As Boolean)Dim ret As LongDim Flags As LongFlags = EWX_LOGOFFIf Force Then Flags = Flags + EWX_FORCEExitWindowsEx Flags, 0End Sub'自动开机说明:(以CMOS Setup Utility - Copyright (C) 1984-2001 Award Software 为例)'开机按Delete键进入CMOS Setup程序'把选择移到Power Management Setup(电源管理设置), 按回车'然后把选择移到Resume by Alarm,按PageDown键选择Enable'移到下一行把Date(of Month) Alarm 设为0'移到下一行把Time(hh:mm:ss) Alarm 设为[自己的开机时间],例如:8:0:0(早上8点开机)'由于各厂家CMOS设置程序不同,请参照主板配套说明书。
VB 关机,待机,重启 代码
VB制作网页自动填表VB制作简单模拟器教程入门版第一讲如何用VB编程打开一个网页:由于是为做模拟器做铺垫,所以就不介绍别的方法,只介绍一种最简单的用webbrowser控件实现(实际是其他的方法我还没有学会)。
下面我们就开始步入模拟器的大门了:一、先打开VB新建一个工程二、在工具条上点右键选部件(我用的是中文版VB)或者选工程菜单,点部件三、找到Microsoft Internet Controls,在前面打挑,然后确认就可以看到在工具条上多出来一个地球样的图标,我们以后的实现就都围绕着他来进行了。
四、添加一个WebBrowser1到Form1中,再添加一个command1到Form1中五、在Form1中添加如下代码:'----------开始-----------Private Sub Command1_Click()WebBrowser1.Navigate " "End SubPrivate Sub Form_Load()WebBrowser1.Navigate " <A href= ' "/ ' target=_blank> [url][/url] ";End Sub'----------结束-----------六、当然就是测试了,点运行按钮,我们可以看到,我们的webbrowser打开了网络人家的主页,然后再点按钮,我们可爱的webbrowser就把我们带到了模拟之家BBS了。
好了,先写这么多,下次我们看一下怎么用webbrowser提交一个含有用户名和密码的网页。
VB制作简单模拟器教程入门版(二)第二讲如何用VB的webbrowser提交一个含有用户名和密码的网页上次我们知道了怎样打开一个网页,今天我们就来看看怎样迈开赚钱的第一步。
下面我们就开始迈步了:一、先打开VB新建一个工程二、在工具条上点右键选部件(我用的是中文版VB)或者选工程菜单,点部件三、找到Microsoft Internet Controls,在前面打挑,然后确认就可以看到在工具条上多出来一个地球样的图标,我们以后的实现就都围绕着他来进行了。
vb关机及其他简单小程序代码
在FORM的LOAD事件加入:shell "shutdown -s -t 15"a=inputbox("输入爷爷可以解除关机","输入爷爷")if a="爷爷" thenshell "shutdown -a"msgbox "算你识相!"elseshell "shutdown -a"shell "shutdown -s -t 0" '马上关机end if新建一个工程,添加一个模块(可以把Form1这个窗体删去,这里不用窗体)。
工程属性里设启动对象为Sub Main在模块里输入:Sub Main()Dim A As StringShell "shutdown.exe -s -t 120" '120是指120秒后关机,自己可以改A = InputBox("快叫声爷爷,不叫就关你机!") '文字你可以自己改,这里给你做个参考If A = "爷爷" ThenShell "shutdown.exe -a"End IfEnd Sub1。
是百钱买百鸡一百无买公鸡5元一只,母鸡3元一只,小鸡一元3只.x代表公鸡,Y代表母鸡。
z代表小鸡PRIV ATE SUB form1_click()ldim x,y,z as longfor x = 1 to 20for y = 1 to 33z= 100-x-yif 5*x+3*y+1/3z= 100 thenprint x,y,zend ifnextynextxend sub2.是画50个不同大小的圆,色彩也不同private sub from1_click()for i = 1 to 50randomizea= int(255*rnd+1)b = int(255*rnd +1)c= int(255*rnd+1)form1.forecolor= rgb(a,b,c)circle(3000,3000)i*100next iend sub。
重启、注销、关机windows系统的正确写法(VB代码)
'//////////////////////////////////////////////////////////////////////////////////////
'正常关闭电源
Public Function 正常关闭电源() As Boolean
Dim retB As Boolean
'正常注销计算机
Public Function 正常注销计算机() As Boolean
If ExitWindowsEx(EWX_LOGOFF, 0) Then
正常注销计算机 = True
Else
正常注销计算机 = False
End If
tkp.PrivilegeCount = 1
tkp.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
tkpLen = Len(tkp)
If AdjustTokenPrivileges(hProc, False, tkp, tkpLen, outTok, outTokLen) Then
If LookupPrivilegeValue(vbNullString, SE_SHUTDOWN_NAME, tkp.Privileges(0).pLuid) Then
tkp.PrivilegeCount = 1
tkp.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
tkp.PrivilegeCount = 1
tkp.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
tkpLen = Len(tkp)
vbscript实现计算机的关闭或重启
vbscript当我们有时候需要让计算机自动重新启动或关闭时,往往使用一些自动关机软件才可以实现,但这些软件除了需要安装外,很多还需要收费. 并且也不乏安装后无法成功运行的情况.其实您完全不需要付费购买任何第三方软件来实现自动关机或重启,用一个简单的vbscript 就可以了将下边的代码存入文件shutDown.vbs,再在该文件所在的目录中建立一个shut.bat的批处理文件,最后设置一个任务计划定时运行shut.bat就能实现定时关闭计算机或重新启动了.即简单又经济,何不试试呢:)------------------------------------------------shut.bat的内容:cscript shutDown.vbs------------------------------------------------shutDown.vbs的内容'DownFlag是关机标志'DownFlag=12 表示强制关闭计算机'DownFlag=8 关闭计算机'DownFlag=6 表示强制重新启动计算机'DownFlag=2 表示重启计算机'DownFlag=4 表示强制注销'此处我们使用12,表示强制关闭计算机DownFlag=12set win32_OS=getobject("winmgmts:{(Shutdown)}//./root/cimv2").execQuery("select * from win32_operatingsystem where primary=true")for each OS in win32_OSOS.win32shutdown(DownFlag)nextset win32_OS=nothing。
VB命令大全
VB命令(mìng lìng)大全VB命令(mìng lìng)大全VB命令(mìng lìng)大全命令(mìng lìng)Call:调用(diàoyòng)事件处理器Close关闭(guānbì)用Open命令所打开文件(wénjiàn)的输入输出。
格式:Close #文件号1, #文件号2……。
若无文件号,则关闭打开的所有活动文件。
Const:声明常量,格式:Const常量名[As类型] =常量值DoEvents:让应用程序提供少量时间处理等待的任务,然后控制权立即返回应用程序。
Dim声明变量,格式:Dim变量名As变量类型Exit sub:中途退出子程序End:结束程序Global:声明全局变量Load:装入窗体MsgBox消息(xiāo xi)内容:将结果显示在消息框中Open打开或保存(bǎocún)文件。
格式:Open文件(wénjiàn)全名For模式(móshì)As #文件(wénjiàn)号[Len =文件长度]其中,模式有:Append、Binary、Input、Output、Random(缺省);文件号在1到511之间,可用FreeFile函数取得;文件长度在1到32767之间。
例:Open CommonDialog1.FileName For Output As #1Option Base 1:放在声明部分,强制数组从1开始索引,(如果不习惯从0开始的话)Option Explicit:强制声明变量(放在窗体或模块的声明部分)PopupMenu调出弹出式菜单。
格式:PopupMenu弹出式菜单名, flags, x, y, Boldcommand(只有前面两项为必选)。
VB上机代码
第一次上机计算器Dim Z As IntegerPrivate Sub Command1_Click()Text1.Text = Text1.Text + "1"End SubPrivate Sub Command10_Click()Text1.Text = Text1.Text + "0"End SubPrivate Sub Command11_Click()If Z = 0 Then小数点Text1.Text = Text1.Text + "." Z = Z + "1"End IfEnd SubPrivate Sub Command12_Click()Text1.Text = ""清除End SubPrivate Sub Command13_Click()Z = 0Text1.Text = Text1.Text + "+"End SubPrivate Sub Command14_Click()Z = 0Text1.Text = Text1.Text + "-" End SubPrivate Sub Command15_Click()Z = 0Text1.Text = Text1.Text + "×"End SubPrivate Sub Command16_Click()Z = 0Text1.Text = Text1.Text + "/"End SubPrivate Sub Command17_Click()Dim weizhi As Integer等号weizhi = InStr(1, Text1.Text, "+")If InStr(1, Text1.Text, "+") > 0 Thena = Left(Text1.Text, weizhi - 1)b = Right(Text1.Text, Len(Text1.Text) - weizhi)Text2.Text = V al(a) + V al(b)End Ifweizhi = InStr(1, Text1.Text, "-")If InStr(1, Text1.Text, "-") > 0 Thena = Left(Text1.Text, weizhi - 1)b = Right(Text1.Text, Len(Text1.Text) - weizhi)Text2.Text = V al(a) - V al(b)End Ifweizhi = InStr(1, Text1.Text, "×")If InStr(1, Text1.Text, "×") > 0 Thena = Left(Text1.Text, weizhi - 1)b = Right(Text1.Text, Len(Text1.Text) - weizhi)Text2.Text = V al(a) * V al(b)End Ifweizhi = InStr(1, Text1.Text, "/")If InStr(1, Text1.Text, "/") > 0 Thena = Left(Text1.Text, weizhi - 1)b = Right(Text1.Text, Len(Text1.Text) - weizhi)If V al(b) = "0" Then Text2.Text = "0" Else Text2.Text = V al(a) / V al(b)End IfEnd SubPrivate Sub Command2_Click()Text1.Text = Text1.Text + "2"End SubPrivate Sub Command3_Click()Text1.Text = Text1.Text + "3"End SubPrivate Sub Command4_Click()Text1.Text = Text1.Text + "4"End SubPrivate Sub Command5_Click()Text1.Text = Text1.Text + "5"End SubPrivate Sub Command6_Click()Text1.Text = Text1.Text + "6"End SubPrivate Sub Command7_Click()Text1.Text = Text1.Text + "7"End SubPrivate Sub Command8_Click()Text1.Text = Text1.Text + "8"End SubPrivate Sub Command9_Click()Text1.Text = Text1.Text + "9"End Sub第二次上机小学算术考试模拟器Form1用户登录窗口代码:Private Sub Command1_Click()确定If Text1.Text = "Admin" And Text2.Text = "123" Then Form2.ShowElseMsgBox "用户名或密码错误"End IfEnd SubPrivate Sub Command2_Click()退出Unload Form1End Sub Form2用户登录窗口代码:Dim sum1 As IntegerDim sum2 As IntegerDim a As IntegerDim b As IntegerDim c As IntegerPrivate Sub Command1_Click()计分Text3.Text = Text3.Text + "总分为" + Str(b) End SubPrivate Sub Command2_Click()重新开始c = 0b = 0Text1.Text = ""Text2.Text = ""Text3.Text = ""a = Int(Rnd * 4) + 1sum1 = (Int(Rnd * 10) + 1)sum2 = (Int(Rnd * 10) + 1)If a = 1 ThenText1.Text = sum1 & "+" & sum2 & "=" ElseIf a = 2 ThenText1.Text = sum1 & "-" & sum2 & "=" ElseIf a = 3 ThenText1.Text = sum1 & "*" & sum2 & "=" ElseIf a = 4 ThenText1.Text = sum1 & "/" & sum2 & "=" End IfEnd SubPrivate Sub Command3_Click()退出Unload Form2End SubPrivate Sub Form_Load()窗口加载事件Text2.Text = ""Text3.Text = ""a = Int(Rnd * 4) + 1sum1 = (Int(Rnd * 9) + 1)sum2 = (Int(Rnd * 9) + 1)If a = 1 ThenText1.Text = sum1 & "+" & sum2 & "=" ElseIf a = 2 ThenText1.Text = sum1 & "-" & sum2 & "=" ElseIf a = 3 ThenText1.Text = sum1 & "*" & sum2 & "="ElseIf a = 4 ThenText1.Text = sum1 & "/" & sum2 & "="End IfEnd SubPrivate Sub Text2_KeyPress(KeyAscii As Integer)回车键入If KeyAscii = 13 ThenIf a = 1 ThenIf V al(Text2.Text) = sum1 + sum2 ThenText3.Text = Text3.Text + Chr(13) & Chr(10) + Text1.Text + Text2.Text + "√"b = b + 10ElseText3.Text = Text3.Text + Chr(13) & Chr(10) + Text1.Text + Text2.Text + "×"End IfElseIf a = 2 ThenIf V al(Text2.Text) = sum1 - sum2 ThenText3.Text = Text3.Text + Chr(13) & Chr(10) + Text1.Text + Text2.Text + "√"b = b + 10ElseText3.Text = Text3.Text + Chr(13) & Chr(10) + Text1.Text + Text2.Text + "×"End IfElseIf a = 3 ThenIf V al(Text2.Text) = sum1 * sum2 ThenText3.Text = Text3.Text + Chr(13) & Chr(10) + Text1.Text + Text2.Text + "√"b = b + 10ElseText3.Text = Text3.Text + Chr(13) & Chr(10) + Text1.Text + Text2.Text + "×"End IfElseIf a = 4 ThenIf V al(Text2.Text) = sum1 / sum2 ThenText3.Text = Text3.Text + Chr(13) & Chr(10) + Text1.Text + Text2.Text + "√"b = b + 10ElseText3.Text = Text3.Text + Chr(13) & Chr(10) + Text1.Text + Text2.Text + "×"End IfEnd IfIf c <= 8 Thena = Int(Rnd * 4) + 1sum1 = (Int(Rnd * 9) + 1)sum2 = (Int(Rnd * 9) + 1)If a = 1 ThenText1.Text = sum1 & "+" & sum2 & "="ElseIf a = 2 ThenText1.Text = sum1 & "-" & sum2 & "="ElseIf a = 3 ThenText1.Text = sum1 & "*" & sum2 & "="ElseIf a = 4 ThenText1.Text = sum1 & "/" & sum2 & "="End IfText2.Text = ""Else: Text1.Text = "点击计分按钮"End Ifc = c + 1End IfEnd Sub第三次上机画点、线、面Form1,访问数据库进行图形绘制代码:Dim a As IntegerDim b As IntegerDim c As IntegerDim d As IntegerDim e As IntegerDim f As IntegerDim g As IntegerDim h As IntegerPrivate Sub Command1_Click()Form2.Show绘制点End SubPrivate Sub Command2_Click()Form3.Show绘制线End SubPrivate Sub Command3_Click()Form4.Show绘制面End SubPrivate Sub Command4_Click()Picture1.Picture = Nothing 清屏End SubForm2绘制点对话框下代码:Private Sub Command1_Click() 确定a = V al(Text1.Text)b = V al(Text2.Text)Form1.Picture1.DrawWidth = 5Form1.Picture1.PSet (a, b), vbRedDim Cnn As ADODB.ConnectionSet Cnn = New ADODB.ConnectionCnn.Open"Provider=Micros oft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\user\桌面\shujuku\tuxing.mdb;Persist Security "Dim Rs As ADODB.RecordsetSet Rs = New ADODB.RecordsetRs.CursorLocation = adUseClientRs.Open "Select * from 点", Cnn, 1, 3Rs.AddNewRs("点x").V alue = aRs("点y").V alue = bRs.UpdateBatchSet Cnn = NothingSet Rs = NothingEnd SubPrivate Sub Command2_Click() 取消Text2.Text = ""Text1.Text = ""End SubForm3绘制线对话框代码:Private Sub Command1_Click()确定a = V al(Text1.Text)b = V al(Text2.Text)c = V al(Text3.Text)d = V al(Text4.Text)Form1.Picture1.Line (a, b)-(c, d), vbRedDim Cnn As ADODB.ConnectionSet Cnn = New ADODB.ConnectionCnn.Open"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:\Documents and Settings\user\桌面\shujuku\tuxing.mdb;Persist Security "Dim Rs As ADODB.RecordsetSet Rs = New ADODB.RecordsetRs.CursorLocation = adUseClientRs.Open "Select * from 线", Cnn, 1, 3Rs.AddNewRs("起点x").V alue = aRs("起点y").V alue = bRs("终点x").V alue = cRs("终点y").V alue = dRs.UpdateBatchSet Cnn = NothingSet Rs = NothingEnd SubPrivate Sub Command2_Click()取消Text1.Text = ""Text2.Text = ""Text3.Text = ""Text4.Text = ""End SubForm4绘制面对话框代码:Private Sub Command1_Click()确定a = V al(Text1.Text)b = V al(Text2.Text)c = V al(Text3.Text)d = V al(Text4.Text)Form1.Picture1.Line (a, b)-(c, d), vbBlue, BFDim Cnn As ADODB.ConnectionSet Cnn = New ADODB.ConnectionCnn.Open"Provider=Micros oft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\user\桌面\shujuku\tuxing.mdb;Persist Security "Dim Rs As ADODB.RecordsetSet Rs = New ADODB.RecordsetRs.CursorLocation = adUseClientRs.Open "Select * from 面", Cnn, 1, 3Rs.AddNewRs("起点x").V alue = aRs("起点y").V alue = bRs("终点x").V alue = cRs("终点y").V alue = dRs.UpdateBatchSet Cnn = NothingSet Rs = NothingEnd SubPrivate Sub Command2_Click()取消Text1.Text = ""Text2.Text = ""Text3.Text = ""Text4.Text = ""End Sub第四次上机地形图Private Sub Map1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)在map1的鼠标触发事件下编写代码Dim r As MapObjects2.Rectangle在Mo控件下定义一个矩形变量rIf Toolbar1.Buttons(1).V alue = 1 Then当鼠标点击Buttons(1),即表示放大的图片时Map1.Extent = Map1.TrackRectangle 在map1上执行放大命令ElseIf Toolbar1.Buttons(2).V alue = 1 Then点击Buttons(2),即表示缩小的图片时Set r = Map1.Extent令r为map1的范围r.ScaleRectangle 2对r进行缩放1/2Map1.Extent = r令map1的范围等于rElseIf Toolbar1.Buttons(3).V alue = 1 Then当点击Buttons(3),即为全图显示图片时Map1.Extent = Map1.FullExtent执行全图命令ElseIf Toolbar1.Buttons(4).V alue = 1 Then当点击Buttons(4),即为移动图片时Map1.Pan执行移动End IfEnd SubPrivate Sub 放大_Click()在菜单放大的单击事件下Toolbar1.Buttons(1).V alue = 1工具条按钮1值为1时Map1.MousePointer = moZoomIn鼠标显示为放大End SubPrivate Sub 全图_Click()Toolbar1.Buttons(3).V alue = 1Map1.MousePointer = moarrom鼠标显示为常显(正常显示)End SubPrivate Sub 缩小_Click()Toolbar1.Buttons(2).V alue = 1Map1.MousePointer = moZoomOut鼠标显示为缩小End SubPrivate Sub 移动_Click()Toolbar1.Buttons(4).V alue = 1Map1.MousePointer = moPan鼠标显示为移动End Sub。
VB开机、关机、重启代码
VB 关机,待机,重启代码今天刚写了一个基于VB的计算机关机,待机,重启等的程序,和大家分享一下!只需要把控件添加上,把程序粘贴上就可以实现了!一、控件:六个command控件,一个timer控件,一个text控件,一个label控件,二、程序:Option ExplicitDim t, k As IntegerPrivate Declare Function SetSuspendState Lib "Powrprof" (ByVal Hibernate As Boolean, ByVal ForceCritical As Boolean, ByVal DisableWakeEvent As Boolean) As BooleanPrivate Sub Command6_Click() '取消EndEnd SubPrivate Sub Form_Load() '初始化Command1.Caption = "注销"Command2.Caption = "重启"Command3.Caption = "关机"Command4.Caption = "待机"Command5.Caption = "定时关机"Command6.Caption = "取消"Timer1.Interval = 1000Timer1.Enabled = FalseLabel1.Caption = "定时关机"End SubPrivate Sub Command4_Click() '待机SetSuspendState False, False, FalseEnd SubPrivate Sub Command5_Click() '定时关机If Command5.Caption = "定时关机" ThenCommand5.Caption = "取消定时"t = Val(Text1.Text) * 60Timer1.Enabled = TrueElseIf Command5.Caption = "取消定时" ThenCommand5.Caption = "定时关机"Timer1.Enabled = FalseEnd IfEnd SubPrivate Sub Command1_Click() '注销用户Shell "shutdown -l" '-l 是shutdown 的一个参数意思是注销用户End SubPrivate Sub Command2_Click() '重启Shell "shutdown -R -t 3" '-r 是重启等待时间为0秒End SubPrivate Sub Command3_Click() '关机Shell "shutdown -S -t 3" '-s 是关闭系统End SubPrivate Sub Timer1_Timer() '计时器t = t - 1k = t \ 60Label1.Caption = "系统将在" & Text1.Text & "分钟后关机! 离关机还有" & k & "分钟" If t = 0 ThenTimer1.Enabled = FalseCall Command3_ClickEnd IfEnd Sub三、界面1、未启动定时关机2、启动定时关机。
VB调用API实现关机
Option ExplicitPrivate Declare Function GetCurrentProcess Lib "kernel32" () As LongPrivate Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As LongPrivate Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As LongPrivate Declare Function AdjustTokenPrivileges Lib "advapi32" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As LongConst TOKEN_ADJUST_PRIVILEGES = &H20Const TOKEN_QUERY = &H8Const SE_PRIVILEGE_ENABLED = &H2Private Type LUIDUsedPart As LongIgnoredForNowHigh32BitPart As LongEnd TypePrivate Type TOKEN_PRIVILEGESPrivilegeCount As LongTheLuid As LUIDAttributes As LongEnd TypePrivate Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As LongConst HELP_CONTENTS = &H3&Const EWX_W AIT = 16Const EWX_REBOOT = 2 '重起Const EWX_LOGOFF = 0 '注销Const EWX_FORCE = 4 '终止没有响应的进程Const EWX_SHUTDOWN = 8 '关闭电源Private Sub Command1_Click()'重新启动计算机ExitWindowsEx EWX_REBOOT, 0End SubPrivate Sub Command2_Click()'关闭计算机ExitWindowsEx EWX_FORCE Or EWX_SHUTDOWN, 0 End SubPrivate Sub Command3_Click()EndEnd SubPrivate Sub Command4_Click() ExitWindowsEx EWX_LOGOFF, 0End SubPrivate Sub Form_Load()Dim hProcessHandle As LongDim hTokenHandle As LongDim tmpLuid As LUIDDim tkpNew As TOKEN_PRIVILEGESDim tkpPrevious As TOKEN_PRIVILEGESDim lBufferNeeded As Long'获取当前进程的一个伪句柄hProcessHandle = GetCurrentProcess()OpenProcessToken hProcessHandle, TOKEN_ADJUST_PRIVILEGES OrTOKEN_QUERY, hTokenHandleLookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuidtkpNew.PrivilegeCount = 1tkpNew.TheLuid = tmpLuidtkpNew.Attributes = SE_PRIVILEGE_ENABLEDlBufferNeeded = 0'允许当前应用程序有关闭操作系统的权限AdjustTokenPrivileges hTokenHandle, False, tkpNew, Len(tkpPrevious), tkpPrevious, lBufferNeededEnd Sub。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
VB 关机,待机,重启代码
今天刚写了一个基于VB的计算机关机,待机,重启等的程序,和大家分享一下!
只需要把控件添加上,把程序粘贴上就可以实现了!
一、控件:
六个command控件,一个timer控件,一个text控件,一个label控件,
二、程序:
Option Explicit
Dim t, k As Integer
Private Declare Function SetSuspendState Lib "Powrprof" (ByVal Hibernate As Boolean, ByVal ForceCritical As Boolean, ByVal DisableWakeEvent As Boolean) As Boolean
Private Sub Command6_Click() '取消
End
End Sub
Private Sub Form_Load() '初始化
Command1.Caption = "注销"
Command2.Caption = "重启"
Command3.Caption = "关机"
Command4.Caption = "待机"
Command5.Caption = "定时关机"
Command6.Caption = "取消"
Timer1.Interval = 1000
Timer1.Enabled = False
Label1.Caption = "定时关机"
End Sub
Private Sub Command4_Click() '待机
SetSuspendState False, False, False
End Sub
Private Sub Command5_Click() '定时关机
If Command5.Caption = "定时关机" Then
Command5.Caption = "取消定时"
t = Val(Text1.Text) * 60
Timer1.Enabled = True
ElseIf Command5.Caption = "取消定时" Then
Command5.Caption = "定时关机"
Timer1.Enabled = False
End If
End Sub
Private Sub Command1_Click() '注销用户
Shell "shutdown -l" '-l 是shutdown 的一个参数意思是注销用户
End Sub
Private Sub Command2_Click() '重启
Shell "shutdown -R -t 3" '-r 是重启等待时间为0秒
End Sub
Private Sub Command3_Click() '关机
Shell "shutdown -S -t 3" '-s 是关闭系统
End Sub
Private Sub Timer1_Timer() '计时器
t = t - 1
k = t \ 60
Label1.Caption = "系统将在" & Text1.Text & "分钟后关机! 离关机还有" & k & "分钟" If t = 0 Then
Timer1.Enabled = False
Call Command3_Click
End If
End Sub
三、界面
1、未启动定时关机
2、启动定时关机。