VB.NET實現(xiàn)關(guān)機和重新啟動
2024-07-10 13:01:06
供稿:網(wǎng)友
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)
elseif option2.checked then
' 關(guān)閉計算機
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鍵時,結(jié)束應用程序
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
me.close()
end if
if keyascii = 0 then
eventargs.handled = true
end if
end sub
本實例通過使用exitwindowex()api函數(shù)來達到關(guān)機和重新啟動的目的。在exitwindowex()函數(shù)中,參數(shù)uflags指定要進行何種操作。在表86-2中列出了參數(shù)uflags的值及其說明。
表86-2 參數(shù)uflags的值及說明
常量名
值
說明
ewx_force
4
終止所有進程,包括沒有響應的進程,并注銷windows
ewx_reboot
2
重新啟動系統(tǒng)
ewx_shutdown
1
關(guān)閉系統(tǒng)
ewx_logoff
0
終止所有正在運行的進程,并注銷windows