关于Shell.Application的使用


关于Shell.Application的使用
-----------------------------------------------------------------------------------

1、创建 Shell 对象
 var Shell = new ActiveXObject("Shell.Application");
 
2、使用 Shell 属性及方法

 Shell.Application
 Shell.Parent

 Shell.CascadeWindows()
 Shell.TileHorizontally()
 Shell.TileVertically()
 Shell.ControlPanelItem(sDir) /* 比如:sysdm.cpl */
 Shell.EjectPC()
 Shell.Explore(vDir)
 Shell.Open(vDir)
 Shell.FileRun()
 Shell.FindComputer()
 Shell.FindFiles()
 Shell.Help()
 Shell.MinimizeAll()
 Shell.UndoMinimizeALL()
 Shell.RefreshMenu()
 Shell.SetTime()
 Shell.TrayProperties()
 Shell.ShutdownWindows()
 Shell.Suspend()
 oWindows = Shell.Windows() /* 返回ShellWindows对象 */
 fFolder = Shell.NameSpace(vDir) /* 返回所打开的vDir的Folder对象 */
 oFolder = Shell.BrowseForFolder(Hwnd, sTitle, iOptions [, vRootFolder]) /* 选择文件夹对话框 */
  /*示例:
  function BrowseFolder()
  {
   var Message = "清选择文件夹";

   var Shell  = new ActiveXObject( "Shell.Application" );
   var Folder = Shell.BrowseForFolder(0,Message,0x0040,0x11);
   if(Folder != null)
   {
    Folder = Folder.items(); // 返回 FolderItems 对象
    Folder = Folder.item();  // 返回 Folderitem 对象
    Folder = Folder.Path;  // 返回路径
    if(Folder.charAt(varFolder.length-1) != "//"){
     Folder = varFolder + "//";
    }
    return Folder;
   }
  }
  */

  /*示例:
  var Folder = Shell.NameSpace("C://");  // 返回 Folder对象
  */ 
 
3、使用 Folder 对象
 
 [ oApplication = ] Folder.Application   // Contains the Application object.
 [ oParentFolder= ] Folder.ParentFolder   // Contains the parent Folder object.
 [    oTitle    = ] Folder.Title    // Contains the title of the folder.

 Folder.CopyHere(vItem [, vOptions])   // Copies an item or items to a folder.
 Folder.MoveHere(vItem [, vOptions])   // Moves an item or items to this folder.
  /*
  vItem:  Required. Specifies the item or items to move. This can be a string that represents a file name, a FolderItem object, or a FolderItems object.
    vOptions Optional. Specifies options for the move operation. This value can be zero or a combination of the following values. These values are based upon flags defined for use with the fFlags member of the C++ SHFILEOPSTRUCT structure. These flags are not defined as such for Microsoft? Visual Basic?, Visual Basic Scripting Edition (VBScript), or Microsoft JScript?, so you must define them yourself or use their numeric equivalents.
   4  Do not display a progress dialog box. 
   8  Give the file being operated on a new name in a move, copy, or rename operation if a file with the target name already exists. 
   16  Respond with "Yes to All" for any dialog box that is displayed. 
   64  Preserve undo information, if possible.
   128 Perform the operation on files only if a wildcard file name (*.*) is specified. 
   256  Display a progress dialog box but do not show the file names. 
   512  Do not confirm the creation of a new directory if the operation requires one to be created. 
   1024 Do not display a user interface if an error occurs. 
   2048  Version 4.71. Do not copy the security attributes of the file.
   4096  Only operate in the local directory. Don't operate recursively into subdirectories.
   9182 Version 5.0. Do not move connected files as a group. Only move the specified files. 
  */
 

 Folder.NewFolder(bName)     // Creates a new folder.
 ppid = Folder.ParseName(bName)    // Creates and returns a FolderItem object that represents a specified item.
  /*
  bName:  Required. A string that specifies the name of the item.
  */

 oFolderItems = Folder.Items()    // Retrieves a FolderItems object that represents the collection of items in the folder.
 sDetail = Folder.GetDetailsOf(vItem, iColumn)  // Retrieves details about an item in a folder. For example, its size, type, or the time of its last modification.
  /*
  vItem:  Required. Specifies the item for which to retrieve the information. This must be a FolderItem object.
  iColumn: Required. An Integer value that specifies the information to be retrieved. The information available for an item depends on the folder in which it is displayed. This value corresponds to the zero-based column number that is displayed in a Shell view. For an item in the file system, this can be one of the following values:0 Retrieves the name of the item.
   1  Retrieves the size of the item.
   2  Retrieves the type of the item.
   3  Retrieves the date and time that the item was last modified.
   4  Retrieves the attributes of the item.
   -1 Retrieves the info tip information for the item.
  */
 
4、使用 FolderItems 对象

  /*示例:
  var FolderItems = Shell.NameSpace("C://").Items(); // 返回 FolderItems 对象
  */

 [ oApplication = ] FolderItems.Application
 [    iCount    = ] FolderItems.Count
 [    oParent   = ] FolderItems.Parent

 oFolderItem = FolderItems.Item([iIndex])  // 返回 FolderItem 对象

5、使用 FolderItem 对象

  /*示例:
  var FolderItem = Shell.NameSpace("C://").Items().Item(iIndex); // 返回 FolderItems 对象
  */

 [ oApplication = ] FolderItem.Application
 [    oParent   = ] FolderItem.Parent
 [ sName = ] FolderItem.Name(sName) [ = sName ]
 [ sPath = ] FolderItem.Path
 [ iSize = ] FolderItem.Size
 [ sType = ] FolderItem.Type
 [ bIsLink = ] FolderItem.IsLink
 [ bIsFolder = ] FolderItem.IsFolder
 [ bIsFileSystem = ] FolderItem.IsFileSystem
 [ bIsBrowsable = ] FolderItem.IsBrowsable
 [  oGetLink  = ] FolderItem.GetLink   // 返回 ShellLinkObject 对象
 [ oGetFolder = ] FolderItem.GetFolder   // 返回 Folder 对象
 [ oModifyDate= ] FolderItem.ModifyDate(oModifyDate) [ = oModifyDate ] // Sets or retrieves the date and time that the item was last modified.

 vVerb = FolderItem.Verbs()    // 返回 FolderItemVerbs 对象. This object is the collection of verbs that can be executed on the item.
 FolderItem.InvokeVerb( [vVerb])    // Executes a verb on the item.


6、使用 FolderItemVerbs 对象

  /*示例:
  var FolderItem = Shell.NameSpace("C://").Items().Item(iIndex).Verbs(); // 返回 FolderItems 对象
  */
 
 [ oApplication = ] FolderItemVerbs.Application
 [ oParent = ] FolderItemVerbs.Parent
 [ iCount = ] FolderItemVerbs.Count

 oVerb = FolderItemVerbs.Item( [iIndex])   // 返回 FolderItemVerb 对象.

7、使用 FolderItemVerb 对象

  /*示例:
  var FolderItem = Shell.NameSpace("C://").Items().Item(iIndex).Verbs().Item(iIndex); // 返回 FolderItems 对象
  */

 [ oApplication = ] FolderItemVerbs.Application
 [ oParent = ] FolderItemVerbs.Parent
 [ oName = ] FolderItemVerbs.Name
 
 FolderItemVerb.DoIt()     // Executes a verb on the FolderItem associated with the verb.

8、使用 ShellLinkObject 对象

 [ sWorkingDirectory = ]ShellLinkObject.WorkingDirectory(sWorkingDirectory) [ = sWorkingDirectory ]
 [ intShowCommand = ]ShellLinkObject.ShowCommand(intShowCommand) [ = intShowCommand ]
  /*
  intShowCommand  Integer that specifies or receives the link's show state. This can be one of the following values.
    1  Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position.
    2  Activates the window and displays it as a minimized window.
    3  Activates the window and displays it as a maximized window.
   */
 [ sArguments = ] ShellLinkObject.Arguments(sArguments) [ = sArguments ]
 [ sDescription = ] ShellLinkObject.Description(sDescription) [ = sDescription ]
 [ iHotkey = ] ShellLinkObject.Hotkey(iHotkey) [ = iHotkey ]
  /*
  iHotkey   Integer that specifies or receives the link's hot key code. The virtual key code is in the low-order byte, and the modifier flags are in the high-order byte. The modifier flags can be a combination of the following values.
   1 SHIFT key
   2 CTRL key
   4 ALT key
   8 Extended key
   */
 [ sPath = ] ShellLinkObject.Path(sPath) [ = sPath ]

 iIcon = ShellLinkObject.GetIconLocation(sPath)
 ShellLinkObject.Resolve(fFlags)
  /*
  fFlags   Required. Flags that specify the action to be taken. This can be a combination of the following values.
   1  Do not display a dialog box if the link cannot be resolved. When this flag is set, the high-order word of fFlags specifies a time-out duration, in milliseconds. The method returns if the link cannot be resolved within the time-out duration. If the high-order word is set to zero, the time-out duration defaults to 3000 milliseconds (3 seconds).
   4  If the link has changed, update its path and list of identifiers.
   8  Do not update the link information.
   16  Do not execute the search heuristics.
   32 Do not use distributed link tracking.
   64  Disable distributed link tracking. By default, distributed link tracking tracks removable media across multiple devices based on the volume name. It also uses the Universal Naming Convention (UNC) path to track remote file systems whose drive letter has changed. Setting this flag disables both types of tracking.
   128  Call the Microsoft? Windows? Installer.
   */
 ShellLinkObject.Save( [sFile])
 ShellLinkObject.SetIconLocation(sPath, iIndex)
  /*
  sPath   Required. String value that contains the fully qualified path of the file that contains the icon.
  iIndex   Required. Integer that is set to the index of the icon in the file specified by sPath.
  */

9、使用 ShellWindows 对象
 [ intCount = ] ShellWindows.Count

 oShellWindows = ShellWindows._NewEnum()  // Creates and returns a new ShellWindows object that is a copy of this ShellWindows object.
 oFolder = ShellWindows.Item( [iIndex])  // Retrieves an InternetExplorer object that represents the Shell window.

 

10、说明
 通过第一步创建 Shell 对象,并进行相关函数调用,就可以返回以上各种对象,并进行相关操作。
 另外,在学习的过程中,发现了两个在msdn中提及却没相关的函数:
  ShellApp.ShellExecute("cmd.exe");
  ShellApp.NameSpace(vDir).Items().InvokeVerbEx(vVerb); /*vVerb:如delete*/

 还有些特殊的用法:
                //var myprinterfolder = Shell.NameSpace("shell:PrintersFolder");
                //var mydocsfolder = Shell.NameSpace("shell:personal");
                //var mycompfolder = Shell.NameSpace("shell:drivefolder");

             //Shell.ShellExecute( "wiaacmgr.exe","/SelectDevice" );
  //Shell.ShellExecute( "rundll32.exe", "shell32.dll,Control_RunDLL sysdm.cpl,,1" )
  //Shell.ShellExecute( "rundll32.exe", "shell32.dll,Control_RunDLL netcpl.cpl,,1" );
  //Shell.ShellExecute( "rundll32.exe", "shell32.dll,Control_RunDLL sysdm.cpl,,1" );

  The following command will run Rundll32.exe.
  Rundll32.exe <dllname>,<entrypoint>,<optional arguments>

  The following code sample shows how to use the command.
  Rundll32.exe Setupx.dll,InstallHinfSection 132 C:/Windows/Inf/Shell.inf


  //Shell.ShowBrowserBar("{C4EE31F3-4768-11D2-BE5C-00A0C9A83DA1}", true);

 真不知道,没有公开的函数调用还有多少,而msdn给我们的使用的只是九牛一毛而已!


11、使用 Shell.UIHelper.1 对象

        ShellUI = new ActiveXObject("Shell.UIHelper.1");

 ShellUI.AddChannel(sURL)
 ShellUI.AddFavorite(sURL [, vTitle])
 bBool = ShellUI.IsSubscribed(sURL)  // Indicates whether or not a URL is subscribed to.
 ShellUI.AddDesktopComponent(sURL, sType [, Left] [, Top] [, Width] [, Height])
  /*
  sURL   Required. A String value that specifies the URL of the new favorite item.
  sType   Required. A String value that specifies the type of item being added. This can be one of the following values:
    image   The component is an image.
    website  The component is a web site.
 
  Left   Optional. Specifies the position of the left edge of the component, in screen coordinates.
  Top   Optional. Specifies the position of the top edge of the component, in screen coordinates.
  Width   Optional. Specifies the width of the component, in screen units.
  Height   Optional. Specifies the height of the component, in screen units.
  */

 
<think>首先,用户的问题主要集中在Windows ShellShell.Application对象上。用户想知道两者的区别,以及在Windows Shell编程和VBA中引用Shell.Application对象的区别。用户还提到了一些之前的引用,但那似乎是与VBScript相关的讨论,我需要注意不要混淆。 用户的问题分解: 1. Windows ShellShell.Application 对象的区别。 2. Windows Shell 编程 与 VBA 中引用 Shell.Application 对象的区别。 我需要基于我的知识来回答。Shell.Application对象是Windows Shell的一个组件,提供接口来访问Shell功能,如文件操作、文件夹浏览等。 关键点: - Windows Shell:这是Windows操作系统的用户界面层,处理桌面、文件管理器等。它提供了一系列API和对象模型,允许开发者与系统交互。 - Shell.Application对象:这是通过COMComponent Object Model)提供的对象,通常在脚本语言如VBScript或VBA中使用。它提供了方法和属性来控制Shell功能,例如打开文件夹、启动应用程序等。 区别: - Windows Shell 编程:这指的是使用C++、C#等语言调用Shell API(如ShellExecute, SHGetFolderPath),或者使用Shell扩展。这是底层的编程。 - VBA 中引用 Shell.Application对象:这是在VBA(Visual Basic for Applications)中使用COM对象来简化Shell操作。例如,在Excel、Word的宏中创建Shell.Application实例来操作文件系统。 在回答中,我需要: - 使用中文回答。 - 遵守LaTeX格式要求:行内数学表达式用$...$,独立公式用$$...$$(但这里可能不涉及数学公式,所以可能不需要)。 - 结构清晰,逐步解决问题。 - 在末尾生成相关问题。 - 引用相关部分时添加引用标识,但用户提供的引用是关于VBScript的,与当前问题无关,我应该忽略它,除非直接相关。 用户提到的引用是来自站内的,内容是@softbug讨论VBScript的类型问题,与Shell无关。所以,我应该专注于用户的核心问题。 我的回答结构: 1. **介绍Windows ShellShell.Application对象:** 解释两者是什么。 2. **区别Windows ShellShell.Application对象:** 对比他们的本质、使用方式和范围。 3. **Windows Shell编程:** 描述如何使用编程语言进行Shell编程。 4. **VBA中引用Shell.Application对象:** 解释在VBA中的具体用法和区别。 5. **总结区别:** 汇总关键点。 6. **生成相关问题:** 在末尾添加相关问题。 确保回答真实可靠:基于我对Windows编程的知识。 最终,生成相关问题:例如,关于Shell.Application的其他用法、VBA中的示例等。 </think>### Windows ShellShell.Application 对象的区别 Windows Shell 是 Windows 操作系统的核心用户界面组件,负责桌面环境、文件资源管理器、任务栏等功能的实现。它提供了一系列底层 API(如 Shell API),允许开发者通过编程语言(如 C++、C#)直接与系统交互,例如执行文件操作、启动应用程序或创建 Shell 扩展[^1]。Shell 的本质是一个系统服务层,涵盖了文件管理、用户界面集成等广泛功能。 Shell.Application 对象则是 Windows Shell 的一个高层 COMComponent Object Model)接口,提供简化的方法和属性来访问 Shell 功能。它通常在脚本语言或宏环境中使用,如 VBScript、VBA 或 JavaScript。通过 Shell.Application 对象,开发者可以不必处理底层 API 的复杂性,就能执行常见任务,例如打开文件夹、枚举文件或调用系统对话框。两者的主要区别如下: | 特性 | Windows Shell | Shell.Application 对象 | |------|---------------|------------------------| | **本质** | 系统级的用户界面服务(包含 API 和内核功能) | COM 对象的封装接口(提供高层抽象) | | **使用场景** | 需要在原生代码(C++、C#)中调用 Shell API 的低级编程 | 在脚本或宏环境(如 VBA、VBScript)中快速执行 Shell 操作 | | **功能范围** | 涵盖整个 Shell 生态系统,包括自定义扩展、钩子函数和系统集成 | 专注于常用任务(如文件浏览、窗口操作),功能受限 | | **编程复杂度** | 高(需处理内存管理、错误处理等) | 低(通过简单方法如 `Shell.Application` 的 `BrowseForFolder` 实现) | | **典型用途** | 开发 Shell 扩展、系统工具或深度集成应用 | 自动化办公任务(如 Excel 宏中打开文件夹) | ### Windows Shell 编程 与 VBA 中引用 Shell.Application 对象的区别 Windows Shell 编程涉及直接使用 Windows API 或 .NET Framework 中的 Shell 相关类(如 `ShellExecute` 或 `IShellFolder`),通常在原生语言中实现。这需要深入了解系统架构,例如通过调用 `SHGetKnownFolderIDList` 获取特殊文件夹路径或创建自定义 Shell 处理器[^1]。这种方法提供最高灵活性和性能,但开发难度大,适合系统级应用。 在 VBA 中引用 Shell.Application 对象则是一种高级抽象,专为自动化环境设计。VBA(Visual Basic for Applications)是 Office 应用程序的内置宏语言,通过 COM 接口简化操作。例如,在 Excel 宏中创建 Shell.Application 实例: ```vba Dim objShell As Object Set objShell = CreateObject("Shell.Application") objShell.Open "C:\Windows" ' 打开指定文件夹 ``` 关键区别点: - **抽象层级**:Windows Shell 编程是底层接口操作,而 VBA 中的 Shell.Application 是对底层 Shell API 的封装,隐藏了复杂性。 - **依赖环境**:Shell 编程不依赖特定宿主(可在独立应用中运行),但 VBA 引用 Shell.Application 必须在 Office 环境(如 Word、Excel)中执行,且受 VBA 安全策略限制(例如宏信任设置)。 - **功能限制**:Shell.Application 对象在 VBA 中只暴露部分功能(如文件浏览、窗口控制),而完整 Shell 编程可以访问更多系统资源(如注册表钩子或后台服务)。 - **错误处理**:在 VBA 中,Shell.Application 错误通常通过 `On Error Resume Next` 处理,而 Shell 编程需要手动处理 HRESULT 错误码。 总结:Windows Shell 是基础框架,Shell.Application 是其易用性封装;Shell 编程适合系统开发,VBA 引用则适合快速脚本自动化。两者互补,但后者在 Office 环境中更常见[^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值