利用Windows API获得系统高级功能
生活随笔
收集整理的這篇文章主要介紹了
利用Windows API获得系统高级功能
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
利用Windows?API獲得系統高級功能??
鄒剛
VB無疑是最先進的編程工具之一,但在涉及windows?32位系統的核心編程方面——譬如一些高級功能的實現上,它仍然顯得有些力不從心,這需要我們充分利用vb的強大的Windows?API函數調用能力來彌補。以下代碼將向您展示如何利用Windows?API控制系統托盤(圖標)區、開始菜單、任務欄以及禁用/起用ctrl-alt-del、退出網絡登錄、立即關機、重新啟動等系統高級功能。
啟動VB6,建立一個標準exe工程,添加14個command按鈕,caption屬性依次為?“隱藏開始菜單”、“顯示開始菜單”、“隱藏系統托盤”、“顯示系統托盤”、“禁用?ctrl-alt-del”、“起用?ctrl-alt-del”、“隱藏任務欄”、“顯示任務欄”、“立即重新啟動”、“關機”、“退出網絡登陸”、“顯示關機對話框”,調整上述控件到適當位置,雙擊窗體,寫入以下代碼:?
Option?Explicit
Private?Declare?Function?FindWindow?Lib?"user32"?Alias?"FindWindowA"?(ByVal?lpClassName?As?String,?ByVal?lpWindowName?As?String)?As?Long
'尋找窗口列表中第一個符合指定條件的頂級窗口
'lpClassName指向包含了窗口類名的空中止(C語言)字串的指針;或設為零,'表示接收任何類
'lpWindowName指向包含了窗口文本(或標簽)的空中止(C語言)字串的指針;'或設為零,表示接收任何窗口標題
Private?Declare?Function?ShowWindow?Lib?"user32"?(ByVal?hwnd?As?Long,?ByVal?nCmdShow?As?Long)?As?Long
'控制窗口的可見性
'hwnd窗口句柄,要向這個窗口應用由nCmdShow指定的命令
'nCmdShow為窗口指定可視性方面的一個命令
Private?Declare?Function?FindWindowEx?Lib?"user32"?Alias?"FindWindowExA"?(ByVal?hWnd1?As?Long,?ByVal?hWnd2?As?Long,?ByVal?lpsz1?As?String,?ByVal?lpsz2?As?String)?As?Long
'在窗口列表中尋找與指定條件相符的第一個子窗口?
'hWnd1在其中查找子的父窗口
'hWnd2從這個窗口后開始查找。這樣便可利用對FindWindowEx的多次調用找到符合條件的所有子窗口。如設為零,表示從第一個子窗口開始搜索
Private?Declare?Function?ExitWindowsEx?Lib?"user32"?(ByVal?uFlags?As?Long,?ByVal?dwReserved?As?Long)
'退出windows,并用特定的選項重新啟動
Private?Declare?Function?SystemParametersInfo?Lib?"user32"?Alias?"SystemParametersInfoA"?(ByVal?uAction?As?Long,?ByVal?uParam?As?Long,?ByRef?lpvParam?As?Any,?ByVal?fuWinIni?As?Long)?As?Long
'允許獲取和設置數量眾多的windows系統參數
'uAction指定要設置的參數
Private?Const?EWX_LOGOFF?=?0'中止進程,然后注銷
Private?Const?EWX_SHUTDOWN?=?1'關掉系統電源
Private?Const?EWX_REBOOT?=?2'重新引導系統
Private?Const?EWX_FORCE?=?4'強迫中止沒有響應的進程
Private?Const?SPI_SCREENSAVERRUNNING?=?97
Private?Sub?Command1_Click()?'隱藏開始菜單
Dim?Handle?As?Long,?FindClass?As?Long
FindClass?=?FindWindow("Shell_TrayWnd",?"")
Handle?=?FindWindowEx(FindClass,?0,?"Button",?vbNullString)
ShowWindow?Handle,?0
End?Sub
Private?Sub?Command11_Click()?'起用?ctrl-alt-del
Dim?ret?As?Integer
Dim?pOld?As?Boolean
ret?=?SystemParametersInfo(SPI_SCREENSAVERRUNNING,?True,?pOld,?0)
End?Sub
Private?Sub?Command12_Click()?'禁用?ctrl-alt-del
Dim?ret?As?Integer
Dim?pOld?As?Boolean
ret?=?SystemParametersInfo(SPI_SCREENSAVERRUNNING,?False,?pOld,?0)
End?Sub
Private?Sub?Command13_Click()?'立即重新啟動
ExitWindowsEx?EWX_REBOOT,?0
End?Sub
Private?Sub?Command14_Click()?'關機
ExitWindowsEx?EWX_SHUTDOWN,?0
End?Sub
Private?Sub?Command15_Click()?'退出網絡登陸
ExitWindowsEx?EWX_LOGOFF,?0
End?Sub
Private?Sub?Command16_Click()?'顯示關機對話框
ExitWindowsEx?EWX_FORCE,?0
End?Sub
Private?Sub?Command2_Click()?'顯示開始菜單
Dim?Handle?As?Long,?FindClass?As?Long
FindClass?=?FindWindow("Shell_TrayWnd",?"")
Handle?=?FindWindowEx(FindClass,?0,?"Button",?vbNullString)
ShowWindow?Handle,?1
End?Sub
Private?Sub?Command5_Click()?'隱藏系統托盤
Dim?FindClass?As?Long,?Handle?As?Long
FindClass?=?FindWindow("Shell_TrayWnd",?"")
Handle?=?FindWindowEx(FindClass,?0,?"TrayNotifyWnd",?vbNullString)
ShowWindow?Handle,?0
End?Sub
Private?Sub?Command6_Click()?'顯示系統托盤
Dim?FindClass?As?Long,?Handle?As?Long
FindClass?=?FindWindow("Shell_TrayWnd",?"")
Handle?=?FindWindowEx(FindClass,?0,?"TrayNotifyWnd",?vbNullString)
ShowWindow?Handle,?1
End?Sub
Private?Sub?Command7_Click()?'顯示任務欄
Dim?FindClass?As?Long,?FindClass2?As?Long,?Parent?As?Long,?Handle?As?Long
FindClass?=?FindWindow("Shell_TrayWnd",?"")
FindClass2?=?FindWindowEx(FindClass,?0,?"ReBarWindow32",?vbNullString)
Parent?=?FindWindowEx(FindClass2,?0,?"MSTaskSwWClass",?vbNullString)
Handle?=?FindWindowEx(Parent,?0,?"SysTabControl32",?vbNullString)
ShowWindow?Handle,?0
End?Sub
Private?Sub?Command8_Click()?'隱藏任務欄
Dim?FindClass?As?Long,?FindClass2?As?Long,?Parent?As?Long,?Handle?As?Long
FindClass?=?FindWindow("Shell_TrayWnd",?"")
FindClass2?=?FindWindowEx(FindClass,?0,?"ReBarWindow32",?vbNullString)
Parent?=?FindWindowEx(FindClass2,?0,?"MSTaskSwWClass",?vbNullString)
Handle?=?FindWindowEx(Parent,?0,?"SysTabControl32",?vbNullString)
ShowWindow?Handle,?1
End?Sub
Private?Sub?Form_QueryUnload(Cancel?As?Integer,?UnloadMode?As?Integer)
End
End?Sub
鄒剛
VB無疑是最先進的編程工具之一,但在涉及windows?32位系統的核心編程方面——譬如一些高級功能的實現上,它仍然顯得有些力不從心,這需要我們充分利用vb的強大的Windows?API函數調用能力來彌補。以下代碼將向您展示如何利用Windows?API控制系統托盤(圖標)區、開始菜單、任務欄以及禁用/起用ctrl-alt-del、退出網絡登錄、立即關機、重新啟動等系統高級功能。
啟動VB6,建立一個標準exe工程,添加14個command按鈕,caption屬性依次為?“隱藏開始菜單”、“顯示開始菜單”、“隱藏系統托盤”、“顯示系統托盤”、“禁用?ctrl-alt-del”、“起用?ctrl-alt-del”、“隱藏任務欄”、“顯示任務欄”、“立即重新啟動”、“關機”、“退出網絡登陸”、“顯示關機對話框”,調整上述控件到適當位置,雙擊窗體,寫入以下代碼:?
Option?Explicit
Private?Declare?Function?FindWindow?Lib?"user32"?Alias?"FindWindowA"?(ByVal?lpClassName?As?String,?ByVal?lpWindowName?As?String)?As?Long
'尋找窗口列表中第一個符合指定條件的頂級窗口
'lpClassName指向包含了窗口類名的空中止(C語言)字串的指針;或設為零,'表示接收任何類
'lpWindowName指向包含了窗口文本(或標簽)的空中止(C語言)字串的指針;'或設為零,表示接收任何窗口標題
Private?Declare?Function?ShowWindow?Lib?"user32"?(ByVal?hwnd?As?Long,?ByVal?nCmdShow?As?Long)?As?Long
'控制窗口的可見性
'hwnd窗口句柄,要向這個窗口應用由nCmdShow指定的命令
'nCmdShow為窗口指定可視性方面的一個命令
Private?Declare?Function?FindWindowEx?Lib?"user32"?Alias?"FindWindowExA"?(ByVal?hWnd1?As?Long,?ByVal?hWnd2?As?Long,?ByVal?lpsz1?As?String,?ByVal?lpsz2?As?String)?As?Long
'在窗口列表中尋找與指定條件相符的第一個子窗口?
'hWnd1在其中查找子的父窗口
'hWnd2從這個窗口后開始查找。這樣便可利用對FindWindowEx的多次調用找到符合條件的所有子窗口。如設為零,表示從第一個子窗口開始搜索
Private?Declare?Function?ExitWindowsEx?Lib?"user32"?(ByVal?uFlags?As?Long,?ByVal?dwReserved?As?Long)
'退出windows,并用特定的選項重新啟動
Private?Declare?Function?SystemParametersInfo?Lib?"user32"?Alias?"SystemParametersInfoA"?(ByVal?uAction?As?Long,?ByVal?uParam?As?Long,?ByRef?lpvParam?As?Any,?ByVal?fuWinIni?As?Long)?As?Long
'允許獲取和設置數量眾多的windows系統參數
'uAction指定要設置的參數
Private?Const?EWX_LOGOFF?=?0'中止進程,然后注銷
Private?Const?EWX_SHUTDOWN?=?1'關掉系統電源
Private?Const?EWX_REBOOT?=?2'重新引導系統
Private?Const?EWX_FORCE?=?4'強迫中止沒有響應的進程
Private?Const?SPI_SCREENSAVERRUNNING?=?97
Private?Sub?Command1_Click()?'隱藏開始菜單
Dim?Handle?As?Long,?FindClass?As?Long
FindClass?=?FindWindow("Shell_TrayWnd",?"")
Handle?=?FindWindowEx(FindClass,?0,?"Button",?vbNullString)
ShowWindow?Handle,?0
End?Sub
Private?Sub?Command11_Click()?'起用?ctrl-alt-del
Dim?ret?As?Integer
Dim?pOld?As?Boolean
ret?=?SystemParametersInfo(SPI_SCREENSAVERRUNNING,?True,?pOld,?0)
End?Sub
Private?Sub?Command12_Click()?'禁用?ctrl-alt-del
Dim?ret?As?Integer
Dim?pOld?As?Boolean
ret?=?SystemParametersInfo(SPI_SCREENSAVERRUNNING,?False,?pOld,?0)
End?Sub
Private?Sub?Command13_Click()?'立即重新啟動
ExitWindowsEx?EWX_REBOOT,?0
End?Sub
Private?Sub?Command14_Click()?'關機
ExitWindowsEx?EWX_SHUTDOWN,?0
End?Sub
Private?Sub?Command15_Click()?'退出網絡登陸
ExitWindowsEx?EWX_LOGOFF,?0
End?Sub
Private?Sub?Command16_Click()?'顯示關機對話框
ExitWindowsEx?EWX_FORCE,?0
End?Sub
Private?Sub?Command2_Click()?'顯示開始菜單
Dim?Handle?As?Long,?FindClass?As?Long
FindClass?=?FindWindow("Shell_TrayWnd",?"")
Handle?=?FindWindowEx(FindClass,?0,?"Button",?vbNullString)
ShowWindow?Handle,?1
End?Sub
Private?Sub?Command5_Click()?'隱藏系統托盤
Dim?FindClass?As?Long,?Handle?As?Long
FindClass?=?FindWindow("Shell_TrayWnd",?"")
Handle?=?FindWindowEx(FindClass,?0,?"TrayNotifyWnd",?vbNullString)
ShowWindow?Handle,?0
End?Sub
Private?Sub?Command6_Click()?'顯示系統托盤
Dim?FindClass?As?Long,?Handle?As?Long
FindClass?=?FindWindow("Shell_TrayWnd",?"")
Handle?=?FindWindowEx(FindClass,?0,?"TrayNotifyWnd",?vbNullString)
ShowWindow?Handle,?1
End?Sub
Private?Sub?Command7_Click()?'顯示任務欄
Dim?FindClass?As?Long,?FindClass2?As?Long,?Parent?As?Long,?Handle?As?Long
FindClass?=?FindWindow("Shell_TrayWnd",?"")
FindClass2?=?FindWindowEx(FindClass,?0,?"ReBarWindow32",?vbNullString)
Parent?=?FindWindowEx(FindClass2,?0,?"MSTaskSwWClass",?vbNullString)
Handle?=?FindWindowEx(Parent,?0,?"SysTabControl32",?vbNullString)
ShowWindow?Handle,?0
End?Sub
Private?Sub?Command8_Click()?'隱藏任務欄
Dim?FindClass?As?Long,?FindClass2?As?Long,?Parent?As?Long,?Handle?As?Long
FindClass?=?FindWindow("Shell_TrayWnd",?"")
FindClass2?=?FindWindowEx(FindClass,?0,?"ReBarWindow32",?vbNullString)
Parent?=?FindWindowEx(FindClass2,?0,?"MSTaskSwWClass",?vbNullString)
Handle?=?FindWindowEx(Parent,?0,?"SysTabControl32",?vbNullString)
ShowWindow?Handle,?1
End?Sub
Private?Sub?Form_QueryUnload(Cancel?As?Integer,?UnloadMode?As?Integer)
End
End?Sub
轉載于:https://www.cnblogs.com/kevinGao/archive/2011/11/06/2238883.html
總結
以上是生活随笔為你收集整理的利用Windows API获得系统高级功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DotNetTextBox V2.0 W
- 下一篇: [置顶] C#中Socke