王永耀
很多系統(tǒng)安全軟件如“超級兔子”、“系統(tǒng)優(yōu)化大師”等,都有一項(xiàng)很酷的功能,就是能夠自由隱藏和顯示“開始”選單中“程序”各項(xiàng)的功能。假如在我們的程序中也能實(shí)現(xiàn)這樣的功能,是不是會為程序添色不少呢?其實(shí),我們用VB可以輕松實(shí)現(xiàn)這樣的功能。
實(shí)現(xiàn)方法 其中最重要的一點(diǎn)就是:在Win 98中,“程序”項(xiàng)的顯示和隱藏可以通過改變c:WindowsStart Menuprograms(注:這里假設(shè)您的Windows安裝在c盤)文件夾下各文件或文件夾的屬性來實(shí)現(xiàn)。要隱藏“程序”中的項(xiàng)目,只要相應(yīng)的文件或文件夾屬性設(shè)成“隱藏”;要顯示項(xiàng)目,也只要去掉相應(yīng)對象的“隱藏”屬性即可。那么,怎樣控制文件的屬性呢?在VB中,API函數(shù)有很重要的作用,可以實(shí)現(xiàn)很多強(qiáng)大的功能。其中,GetFileAttributes函數(shù)可以得到文件的屬性、SetFileAttributes函數(shù)可以更改文件屬性、GetWindowsDirectory函數(shù)可以得到系統(tǒng)目錄,有了這三個API“法寶”坐鎮(zhèn),程序?qū)崿F(xiàn)就很容易了。當(dāng)程序啟動時調(diào)用GetWindowsDirectory函數(shù)得到系統(tǒng)目錄的路徑,再用Dir函數(shù)在一個列表框中列出“系統(tǒng)目錄Start Menuprograms ”目錄下的所有文件和文件夾,并調(diào)用GetFileAttributes函數(shù)來獲得各文件和文件夾的屬性,若屬性為“隱藏”,就把相應(yīng)的列表項(xiàng)勾選(表示此項(xiàng)已隱藏)。在列表框中勾選你想要隱藏的項(xiàng)目,接著調(diào)用SetFileAttributes函數(shù),將勾選項(xiàng)相應(yīng)的文件或文件夾的屬性改為“隱藏”(表示將其隱藏),去掉未勾選項(xiàng)相應(yīng)的文件或文件夾的“隱藏”屬性。這樣,一切就搞定了。
程序代碼及講解 首先新建一個Project工程,并在Form1中建立一個列表框list1,其style屬性為:Checkbox(復(fù)選框式樣);四個命令按鈕:command1、command2、command3和command4。
具體程序代碼如下:
'declarations部分,聲明API函數(shù) Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpfilename As String) As Long Private Declare Function SetFileAttributes Lib "kernel32" Alias "SetFileAttributesA" (ByVal lpfilename As String, ByVal dwFileAttributes As Long) As Long Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nsize As Long) As Long '聲明變量 Dim i As Integer Dim lngpath As Long Dim tmppath As String Dim strpath As String Dim strdir As String '定義子過程1,用于顯示“程序”選單各項(xiàng),并確定是否已經(jīng)隱藏 Sub getfileattr() i = 0 tmppath = Space(50) lngpath = GetWindowsDirectory(tmppath, Len(tmppath)) strpath = Left(tmppath, lngpath) && "Start MenuPrograms" 'programs路徑 strdir = Dir(strpath, vbDirectory + vbNormal + vbHidden + vbArchive + vbReadOnly + vbSystem) '將所有程序項(xiàng)目添加到列表框中 Do While strdir <> "" If strdir <> "." And strdir <> ".." Then List1.AddItem strdir i = i + 1 If (GetFileAttributes(strpath && strdir) And vbHidden) Then '得到文件或文件夾屬性,若為隱藏則勾選 List1.Selected(i - 1) = True End If End If strdir = Dir Loop '下一個文件或路徑 End Sub '定義子過程2 Sub setfileattr() tmppath = Space(50) lngpath = GetWindowsDirectory(tmppath, Len(tmppath)) strpath = Left(tmppath, lngpath) && "Start MenuPrograms" '得到“programs”路徑 For i = 0 To (List1.ListCount - 1) If List1.Selected(i) = True Then '勾選則隱藏,反之則顯示 SetFileAttributes strpath + List1.List(i), vbHidden Else SetFileAttributes strpath + List1.List(i), vbNormal End If Next i End Sub Private Sub Command1_Click() Call setfileattr '調(diào)用子過程2改變文件屬性 End Sub Private Sub Command2_Click() End End Sub Private Sub Command3_Click() For i = 0 To List1.ListCount - 1 '全選 List1.Selected(i) = True Next i End Sub Private Sub Command4_Click() For i = 0 To List1.ListCount - 1 '全否 List1.Selected(i) = False Next i End Sub Private Sub Form_Load() Form1.Caption = "隱藏和顯示程序選單" Command1.Caption = "確定" Command2.Caption = "退出" Command3.Caption = "全選" Command4.Caption = "全否" Call getfileattr '調(diào)用子過程1,得到文件屬性并初始化列表框各項(xiàng) End Sub
按F5運(yùn)行后,程序下的文件和文件夾會一個不漏地顯示在列表框里,再勾選幾個,按“確認(rèn)”,打開“開始選單”的“程序”,剛才勾選的幾個不見了。再次運(yùn)行程序,看看列表框里,是不是剛才勾選的現(xiàn)在依然勾選著呢?那就是告訴你,“程序”選單中已經(jīng)隱藏了這些項(xiàng)。通過修改文件屬性還可以完成許多的功能,如管理“發(fā)送”(send to)、“收藏夾”(favorites)等,就看你如何靈活運(yùn)用了。
以上程序在Windows 98、VB 6.0企業(yè)版下調(diào)試通過。
|