王永耀
很多系統安全軟件如“超級兔子”、“系統優化大師”等,都有一項很酷的功能,就是能夠自由隱藏和顯示“開始”選單中“程序”各項的功能。假如在我們的程序中也能實現這樣的功能,是不是會為程序添色不少呢?其實,我們用VB可以輕松實現這樣的功能。
實現方法 其中最重要的一點就是:在Win 98中,“程序”項的顯示和隱藏可以通過改變c:WindowsStart Menuprograms(注:這里假設您的Windows安裝在c盤)文件夾下各文件或文件夾的屬性來實現。要隱藏“程序”中的項目,只要相應的文件或文件夾屬性設成“隱藏”;要顯示項目,也只要去掉相應對象的“隱藏”屬性即可。那么,怎樣控制文件的屬性呢?在VB中,API函數有很重要的作用,可以實現很多強大的功能。其中,GetFileAttributes函數可以得到文件的屬性、SetFileAttributes函數可以更改文件屬性、GetWindowsDirectory函數可以得到系統目錄,有了這三個API“法寶”坐鎮,程序實現就很容易了。當程序啟動時調用GetWindowsDirectory函數得到系統目錄的路徑,再用Dir函數在一個列表框中列出“系統目錄Start Menuprograms ”目錄下的所有文件和文件夾,并調用GetFileAttributes函數來獲得各文件和文件夾的屬性,若屬性為“隱藏”,就把相應的列表項勾選(表示此項已隱藏)。在列表框中勾選你想要隱藏的項目,接著調用SetFileAttributes函數,將勾選項相應的文件或文件夾的屬性改為“隱藏”(表示將其隱藏),去掉未勾選項相應的文件或文件夾的“隱藏”屬性。這樣,一切就搞定了。
程序代碼及講解 首先新建一個Project工程,并在Form1中建立一個列表框list1,其style屬性為:Checkbox(復選框式樣);四個命令按鈕:command1、command2、command3和command4。
具體程序代碼如下:
'declarations部分,聲明API函數 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,用于顯示“程序”選單各項,并確定是否已經隱藏 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) '將所有程序項目添加到列表框中 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 '調用子過程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 '調用子過程1,得到文件屬性并初始化列表框各項 End Sub
按F5運行后,程序下的文件和文件夾會一個不漏地顯示在列表框里,再勾選幾個,按“確認”,打開“開始選單”的“程序”,剛才勾選的幾個不見了。再次運行程序,看看列表框里,是不是剛才勾選的現在依然勾選著呢?那就是告訴你,“程序”選單中已經隱藏了這些項。通過修改文件屬性還可以完成許多的功能,如管理“發送”(send to)、“收藏夾”(favorites)等,就看你如何靈活運用了。
以上程序在Windows 98、VB 6.0企業版下調試通過。
|