Windows Shell 为编程带来了什么?
Posted goodlifesantook
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Windows Shell 为编程带来了什么?相关的知识,希望对你有一定的参考价值。
您是否希望在你的程序中调用一些Windows功能呢?
例如,当你希望调用Windows 的DirectoryBrowser对话框的时候你却发现Microsoft Commom Dialog并没有这个功能,那该如何处理呢?
其实,在Windows中,它已经为我们提供了一个可复用的COM类库,Microsoft Shell Controls And Automation。调用它,我们可以轻易地实现以下功能:
1)调用目录浏览对话框。
2)启动停止服务。
3)排序Windows窗口。
4)调用控制面板项目。
5)打开资源管理器。
6)打开URL
7)调用系统运行对话框。
8)打开查找计算机对话框
9)打开查找文件对话框
10)打开查找打印机对话框
当然,还有更多的功能,
注意:Shell32.dll中的COM引用在Windows XP和Windows Vista上是不通用的,所以你必须为你的程序做不同系统上的打包。
希望这些功能对你有用,~好运~~~~@!!
以下以Visual Basic .NET来作为例子给出相关的代码。
'测试平台:Windows Server 2008 SP1
'开发平台:Visual Studio Team System 2008 SP1 90days Try
'引用:Microsoft Shell Controls And Automation
Public Class Form1
Public shl As New Shell32.Shell
'目录浏览功能
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fl As Shell32.Folder
fl = shl.BrowseForFolder(Me.Handle, "请选择一个要浏览的目录", Shell32.ShellFolderViewOptions.SFVVO_SHOWEXTENSIONS, Shell32.ShellSpecialFolderConstants.ssfDRIVES)
End Sub
'资源管理器
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
shl.Explore("D:/")
End Sub
'运行
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
shl.FileRun()
End Sub
'查找文件
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
shl.FindFiles()
End Sub
'查找计算机
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
shl.FindComputer()
End Sub
'查找打印机
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
shl.FindPrinter()
End Sub
'获取Hash值
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
MsgBox(shl.GetHashCode)
End Sub
'层叠窗口
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
shl.CascadeWindows()
End Sub
'水平平铺
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
shl.TileHorizontally()
End Sub
'垂直平铺
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
shl.TileVertically()
End Sub
'全部最小化
Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
shl.MinimizeAll()
End Sub
'打开控制面板项
Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
shl.ControlPanelItem("main.cpl")
End Sub
'设置时间
Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
shl.SetTime()
End Sub
'关闭计算机
Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
shl.ShutdownWindows()
End Sub
'显示桌面
Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
shl.ToggleDesktop()
End Sub
'WIndows Vista中的Win+Tab组合键效果
Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
shl.WindowSwitcher()
End Sub
End Class
以上是关于Windows Shell 为编程带来了什么?的主要内容,如果未能解决你的问题,请参考以下文章
《跟老男孩学Linux运维之shell编程实战》-第五章 shell脚本的条件测试