VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > vb >
  • vb教程之用VB6实现隐藏和显示"程序"选单

王永耀

很多系统安全软件如“超级兔子”、“系统优化大师”等,都有一项很酷的功能,就是能够自由隐藏和显示“开始”选单中“程序”各项的功能。假如在我们的程序中也能实现这样的功能,是不是会为程序添色不少呢?其实,我们用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企业版下调试通过。 

相关教程